// Package imdraw implements a basic primitive geometry shape and pictured polygon drawing for Pixel // with a nice immediate-mode-like API. package imdraw import ( "image/color" "math" "github.com/faiface/pixel" ) // IMDraw is an immediate-mode-like shape drawer and BasicTarget. IMDraw supports TrianglesPosition, // TrianglesColor, TrianglesPicture and PictureColor. // // IMDraw, other than a regular BasicTarget, is used to draw shapes. To draw shapes, you first need // to Push some points to IMDraw: // // imd := pixel.NewIMDraw(pic) // use nil pic if you only want to draw primitive shapes // imd.Push(pixel.V(100, 100)) // imd.Push(pixel.V(500, 100)) // // Once you have Pushed some points, you can use them to draw a shape, such as a line: // // imd.Line(20) // draws a 20 units thick line // // Use various methods to change properties of Pushed points: // // imd.Color(pixel.RGB(1, 0, 0)) // imd.Push(pixel.V(200, 200)) // imd.Circle(400, 0) // // Here is the list of all available point properties (need to be set before Pushing a point): // - Color - applies to all // - Picture - coordinates, only applies to filled polygons // - Intensity - picture intensity, only applies to filled polygons // - Precision - curve drawing precision, only applies to circles and ellipses // - EndShape - shape of the end of a line, only applies to lines and outlines // // And here's the list of all shapes that can be drawn (all, except for line, can be filled or // outlined): // - Line // - Polygon // - Circle // - Circle arc // - Ellipse // - Ellipse arc type IMDraw struct { points []point opts point matrix pixel.Matrix mask pixel.RGBA tri *pixel.TrianglesData batch *pixel.Batch } var _ pixel.BasicTarget = (*IMDraw)(nil) type point struct { pos pixel.Vec col pixel.RGBA pic pixel.Vec in float64 precision int endshape EndShape } // EndShape specifies the shape of an end of a line or a curve. type EndShape int const ( // NoEndShape leaves a line point with no special end shape. NoEndShape EndShape = iota // SharpEndShape is a sharp triangular end shape. SharpEndShape // RoundEndShape is a circular end shape. RoundEndShape ) // New creates a new empty IMDraw. An optional Picture can be used to draw with a Picture. // // If you just want to draw primitive shapes, pass nil as the Picture. func New(pic pixel.Picture) *IMDraw { tri := &pixel.TrianglesData{} im := &IMDraw{ tri: tri, batch: pixel.NewBatch(tri, pic), } im.SetMatrix(pixel.IM) im.SetColorMask(pixel.Alpha(1)) im.Reset() return im } // Clear removes all drawn shapes from the IM. This does not remove Pushed points. func (imd *IMDraw) Clear() { imd.tri.SetLen(0) imd.batch.Dirty() } // Reset restores all point properties to defaults and removes all Pushed points. // // This does not affect matrix and color mask set by SetMatrix and SetColorMask. func (imd *IMDraw) Reset() { imd.points = nil imd.opts = point{} imd.Precision(64) } // Draw draws all currently drawn shapes inside the IM onto another Target. // // Note, that IMDraw's matrix and color mask have no effect here. func (imd *IMDraw) Draw(t pixel.Target) { imd.batch.Draw(t) } // Push adds some points to the IM queue. All Pushed points will have the same properties except for // the position. func (imd *IMDraw) Push(pts ...pixel.Vec) { for _, pt := range pts { imd.pushPt(pt, imd.opts) } } func (imd *IMDraw) pushPt(pos pixel.Vec, pt point) { pt.pos = pos imd.points = append(imd.points, pt) } // Color sets the color of the next Pushed points. func (imd *IMDraw) Color(color color.Color) { imd.opts.col = pixel.ToRGBA(color) } // Picture sets the Picture coordinates of the next Pushed points. func (imd *IMDraw) Picture(pic pixel.Vec) { imd.opts.pic = pic } // Intensity sets the picture Intensity of the next Pushed points. func (imd *IMDraw) Intensity(in float64) { imd.opts.in = in } // Precision sets the curve/circle drawing precision of the next Pushed points. // // It is the number of segments per 360 degrees. func (imd *IMDraw) Precision(p int) { imd.opts.precision = p } // EndShape sets the endshape of the next Pushed points. func (imd *IMDraw) EndShape(es EndShape) { imd.opts.endshape = es } // SetMatrix sets a Matrix that all further points will be transformed by. func (imd *IMDraw) SetMatrix(m pixel.Matrix) { imd.matrix = m imd.batch.SetMatrix(imd.matrix) } // SetColorMask sets a color that all further point's color will be multiplied by. func (imd *IMDraw) SetColorMask(color color.Color) { imd.mask = pixel.ToRGBA(color) imd.batch.SetColorMask(imd.mask) } // MakeTriangles returns a specialized copy of the provided Triangles that draws onto this IMDraw. func (imd *IMDraw) MakeTriangles(t pixel.Triangles) pixel.TargetTriangles { return imd.batch.MakeTriangles(t) } // MakePicture returns a specialized copy of the provided Picture that draws onto this IMDraw. func (imd *IMDraw) MakePicture(p pixel.Picture) pixel.TargetPicture { return imd.batch.MakePicture(p) } // Line draws a polyline of the specified thickness between the Pushed points. func (imd *IMDraw) Line(thickness float64) { imd.polyline(thickness, false) } // Rectangle draws a rectangle between each two subsequent Pushed points. Drawing a rectangle // between two points means drawing a rectangle with sides parallel to the axes of the coordinate // system, where the two points specify it's two opposite corners. // // If the thickness is 0, rectangles will be filled, otherwise will be outlined with the given // thickness. func (imd *IMDraw) Rectangle(thickness float64) { if thickness == 0 { imd.fillRectangle() } else { imd.outlineRectangle(thickness) } } // Polygon draws a polygon from the Pushed points. If the thickness is 0, the convex polygon will be // filled. Otherwise, an outline of the specified thickness will be drawn. The outline does not have // to be convex. // // Note, that the filled polygon does not have to be strictly convex. The way it's drawn is that a // triangle is drawn between each two adjacent points and the first Pushed point. You can use this // property to draw certain kinds of concave polygons. func (imd *IMDraw) Polygon(thickness float64) { if thickness == 0 { imd.fillPolygon() } else { imd.polyline(thickness, true) } } // Circle draws a circle of the specified radius around each Pushed point. If the thickness is 0, // the circle will be filled, otherwise a circle outline of the specified thickness will be drawn. func (imd *IMDraw) Circle(radius, thickness float64) { if thickness == 0 { imd.fillEllipseArc(pixel.V(radius, radius), 0, 2*math.Pi) } else { imd.outlineEllipseArc(pixel.V(radius, radius), 0, 2*math.Pi, thickness, false) } } // CircleArc draws a circle arc of the specified radius around each Pushed point. If the thickness // is 0, the arc will be filled, otherwise will be outlined. The arc starts at the low angle and // continues to the high angle. If low high { orientation = -1.0 } switch pt.endshape { case NoEndShape: // nothing case SharpEndShape: thick := pixel.X(thickness / 2).Rotated(normalLow) imd.pushPt(lowCenter+thick, pt) imd.pushPt(lowCenter-thick, pt) imd.pushPt(lowCenter-thick.Rotated(math.Pi/2*orientation), pt) imd.fillPolygon() thick = pixel.X(thickness / 2).Rotated(normalHigh) imd.pushPt(highCenter+thick, pt) imd.pushPt(highCenter-thick, pt) imd.pushPt(highCenter+thick.Rotated(math.Pi/2*orientation), pt) imd.fillPolygon() case RoundEndShape: imd.pushPt(lowCenter, pt) imd.fillEllipseArc(pixel.V(thickness, thickness)/2, normalLow, normalLow-math.Pi*orientation) imd.pushPt(highCenter, pt) imd.fillEllipseArc(pixel.V(thickness, thickness)/2, normalHigh, normalHigh+math.Pi*orientation) } } } } func (imd *IMDraw) polyline(thickness float64, closed bool) { points := imd.getAndClearPoints() if len(points) == 0 { return } if len(points) == 1 { // one point special case points = append(points, points[0]) } // first point j, i := 0, 1 normal := (points[i].pos - points[j].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2) if !closed { switch points[j].endshape { case NoEndShape: // nothing case SharpEndShape: imd.pushPt(points[j].pos+normal, points[j]) imd.pushPt(points[j].pos-normal, points[j]) imd.pushPt(points[j].pos+normal.Rotated(math.Pi/2), points[j]) imd.fillPolygon() case RoundEndShape: imd.pushPt(points[j].pos, points[j]) imd.fillEllipseArc(pixel.V(thickness, thickness)/2, normal.Angle(), normal.Angle()+math.Pi) } } imd.pushPt(points[j].pos+normal, points[j]) imd.pushPt(points[j].pos-normal, points[j]) // middle points for i := 0; i < len(points); i++ { j, k := i+1, i+2 closing := false if j >= len(points) { j %= len(points) closing = true } if k >= len(points) { if !closed { break } k %= len(points) } ijNormal := (points[j].pos - points[i].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2) jkNormal := (points[k].pos - points[j].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2) orientation := 1.0 if ijNormal.Cross(jkNormal) > 0 { orientation = -1.0 } imd.pushPt(points[j].pos-ijNormal, points[j]) imd.pushPt(points[j].pos+ijNormal, points[j]) imd.fillPolygon() switch points[j].endshape { case NoEndShape: // nothing case SharpEndShape: imd.pushPt(points[j].pos, points[j]) imd.pushPt(points[j].pos+ijNormal.Scaled(orientation), points[j]) imd.pushPt(points[j].pos+jkNormal.Scaled(orientation), points[j]) imd.fillPolygon() case RoundEndShape: imd.pushPt(points[j].pos, points[j]) imd.fillEllipseArc(pixel.V(thickness, thickness)/2, ijNormal.Angle(), ijNormal.Angle()-math.Pi) imd.pushPt(points[j].pos, points[j]) imd.fillEllipseArc(pixel.V(thickness, thickness)/2, jkNormal.Angle(), jkNormal.Angle()+math.Pi) } if !closing { imd.pushPt(points[j].pos+jkNormal, points[j]) imd.pushPt(points[j].pos-jkNormal, points[j]) } } // last point i, j = len(points)-2, len(points)-1 normal = (points[j].pos - points[i].pos).Rotated(math.Pi / 2).Unit().Scaled(thickness / 2) imd.pushPt(points[j].pos-normal, points[j]) imd.pushPt(points[j].pos+normal, points[j]) imd.fillPolygon() if !closed { switch points[j].endshape { case NoEndShape: // nothing case SharpEndShape: imd.pushPt(points[j].pos+normal, points[j]) imd.pushPt(points[j].pos-normal, points[j]) imd.pushPt(points[j].pos+normal.Rotated(-math.Pi/2), points[j]) imd.fillPolygon() case RoundEndShape: imd.pushPt(points[j].pos, points[j]) imd.fillEllipseArc(pixel.V(thickness, thickness)/2, normal.Angle(), normal.Angle()-math.Pi) } } }