|
| 1 | +package render |
| 2 | + |
| 3 | +import ( |
| 4 | + "image" |
| 5 | + "image/color" |
| 6 | + "math" |
| 7 | + |
| 8 | + "github.com/tronbyt/gg" |
| 9 | +) |
| 10 | + |
| 11 | +// Arc draws an arc. The arc is centered at (x, y). |
| 12 | +// |
| 13 | +// DOC(X): The x-coordinate of the center of the arc. |
| 14 | +// DOC(Y): The y-coordinate of the center of the arc. |
| 15 | +// DOC(Radius): The radius of the arc. |
| 16 | +// DOC(StartAngle): The starting angle of the arc, in radians. |
| 17 | +// DOC(EndAngle): The ending angle of the arc, in radians. |
| 18 | +// DOC(Color): The color of the arc. |
| 19 | +// DOC(Width): The width of the arc. |
| 20 | +type Arc struct { |
| 21 | + Widget |
| 22 | + X float64 `starlark:"x,required"` |
| 23 | + Y float64 `starlark:"y,required"` |
| 24 | + Radius float64 `starlark:"radius,required"` |
| 25 | + StartAngle float64 `starlark:"start_angle,required"` |
| 26 | + EndAngle float64 `starlark:"end_angle,required"` |
| 27 | + Color color.Color `starlark:"color,required"` |
| 28 | + Width float64 `starlark:"width,required"` |
| 29 | +} |
| 30 | + |
| 31 | +func (a Arc) getBounds() (float64, float64, float64, float64) { |
| 32 | + // Start with endpoints |
| 33 | + x1 := a.X + a.Radius*math.Cos(a.StartAngle) |
| 34 | + y1 := a.Y + a.Radius*math.Sin(a.StartAngle) |
| 35 | + x2 := a.X + a.Radius*math.Cos(a.EndAngle) |
| 36 | + y2 := a.Y + a.Radius*math.Sin(a.EndAngle) |
| 37 | + |
| 38 | + minX := math.Min(x1, x2) |
| 39 | + maxX := math.Max(x1, x2) |
| 40 | + minY := math.Min(y1, y2) |
| 41 | + maxY := math.Max(y1, y2) |
| 42 | + |
| 43 | + // Check cardinal points (0, 90, 180, 270 degrees) |
| 44 | + // We need to normalize angles to [0, 2*pi) |
| 45 | + start := a.StartAngle |
| 46 | + end := a.EndAngle |
| 47 | + |
| 48 | + // If start > end, we are crossing 0 (e.g. 350 to 10 degrees) |
| 49 | + // But gg uses "draw from start to end". If start > end, it generally draws clockwise or "the long way"? |
| 50 | + // Wait, gg documentation says: "Angles are specified in radians and go clockwise." |
| 51 | + // Actually, standard math is counter-clockwise. |
| 52 | + // Let's assume standard behavior: from Start to End. |
| 53 | + // If Start < End, it's simple interval [Start, End]. |
| 54 | + // If Start > End, it's [Start, 2*pi] U [0, End]. (Crossing 0). |
| 55 | + |
| 56 | + // Normalize angles to 0-2pi for comparison |
| 57 | + normalize := func(angle float64) float64 { |
| 58 | + angle = math.Mod(angle, 2*math.Pi) |
| 59 | + if angle < 0 { |
| 60 | + angle += 2 * math.Pi |
| 61 | + } |
| 62 | + return angle |
| 63 | + } |
| 64 | + |
| 65 | + normStart := normalize(start) |
| 66 | + normEnd := normalize(end) |
| 67 | + |
| 68 | + // If the original sweep was meant to be > 2pi (full circle), or specific winding, |
| 69 | + // checking just normalized values might be ambiguous. |
| 70 | + // But for bounding box, we just need to know if the cardinal directions are covered. |
| 71 | + |
| 72 | + // We check each cardinal direction: 0, pi/2, pi, 3pi/2 |
| 73 | + cardinals := []float64{0, math.Pi / 2, math.Pi, 3 * math.Pi / 2} |
| 74 | + |
| 75 | + for _, angle := range cardinals { |
| 76 | + inArc := false |
| 77 | + if normStart <= normEnd { |
| 78 | + // Normal range |
| 79 | + if angle >= normStart && angle <= normEnd { |
| 80 | + inArc = true |
| 81 | + } |
| 82 | + } else { |
| 83 | + // Crossing 0 |
| 84 | + if angle >= normStart || angle <= normEnd { |
| 85 | + inArc = true |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if inArc { |
| 90 | + x := a.X + a.Radius*math.Cos(angle) |
| 91 | + y := a.Y + a.Radius*math.Sin(angle) |
| 92 | + |
| 93 | + if x < minX { |
| 94 | + minX = x |
| 95 | + } |
| 96 | + if x > maxX { |
| 97 | + maxX = x |
| 98 | + } |
| 99 | + if y < minY { |
| 100 | + minY = y |
| 101 | + } |
| 102 | + if y > maxY { |
| 103 | + maxY = y |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Expand by half width (stroke width) |
| 109 | + halfWidth := a.Width / 2.0 |
| 110 | + minX -= halfWidth |
| 111 | + maxX += halfWidth |
| 112 | + minY -= halfWidth |
| 113 | + maxY += halfWidth |
| 114 | + |
| 115 | + return minX, maxX, minY, maxY |
| 116 | +} |
| 117 | + |
| 118 | +func (a Arc) PaintBounds(bounds image.Rectangle, frameIdx int) image.Rectangle { |
| 119 | + minX, maxX, minY, maxY := a.getBounds() |
| 120 | + return image.Rect( |
| 121 | + 0, |
| 122 | + 0, |
| 123 | + int(math.Ceil(maxX-minX)), |
| 124 | + int(math.Ceil(maxY-minY)), |
| 125 | + ) |
| 126 | +} |
| 127 | + |
| 128 | +func (a Arc) Paint(dc *gg.Context, bounds image.Rectangle, frameIdx int) { |
| 129 | + minX, _, minY, _ := a.getBounds() |
| 130 | + |
| 131 | + dc.Push() |
| 132 | + dc.Translate(-minX, -minY) |
| 133 | + dc.SetColor(a.Color) |
| 134 | + dc.SetLineWidth(a.Width) |
| 135 | + dc.DrawArc(a.X, a.Y, a.Radius, a.StartAngle, a.EndAngle) |
| 136 | + dc.Stroke() |
| 137 | + dc.Pop() |
| 138 | +} |
| 139 | + |
| 140 | +func (a Arc) FrameCount(bounds image.Rectangle) int { |
| 141 | + return 1 |
| 142 | +} |
0 commit comments