import ( "image" "image/color" "image/draw" ) func main() { myImage := image.NewRGBA(image.Rect(0, 0, 100, 100)) drawLine(myImage, 0, 50, 100, 50, color.RGBA{255, 0, 0, 255}) } func drawLine(img draw.Image, x0, y0, x1, y1 int, clr color.Color) { dx := x1 - x0 dy := y1 - y0 x := x0 y := y0 if dx < 0 { dx = -dx x = x1 x1 = x0 } if dy < 0 { dy = -dy y = y1 y1 = y0 } sx := -1 if x < x1 { sx = 1 } sy := -1 if y < y1 { sy = 1 } err := dx - dy for { img.Set(x, y, clr) if x == x1 && y == y1 { break } e2 := 2 * err if e2 > -dy { err -= dy x += sx } if e2 < dx { err += dx y += sy } } }
import ( "image" "image/color" "image/draw" ) func main() { myImage := image.NewRGBA(image.Rect(0, 0, 100, 100)) drawRect(myImage, 10, 10, 50, 50, color.RGBA{0, 0, 255, 255}) } func drawRect(img draw.Image, x0, y0, x1, y1 int, clr color.Color) { rect := image.Rect(x0, y0, x1, y1) draw.Draw(img, rect, &image.Uniform{clr}, image.Point{}, draw.Src) }In the above code, the `drawRect()` function first defines the rectangle region within which the rectangle is to be drawn. The function then uses the `draw.Draw()` function to draw the rectangle with the specified color. The `draw.Src` parameter indicates that the image has only one layer that is the foreground. In conclusion, the `image/draw` package in Go is a powerful library that you can use to manipulate images and draw graphics. The package provides a variety of functions that can help you create beautiful and compelling visualizations.