| 1 |
package drawing |
| 2 |
|
| 3 |
import "math" |
| 4 |
|
| 5 |
const ( |
| 6 |
|
| 7 |
CurveRecursionLimit = 32 |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
func SubdivideCubic(c, c1, c2 []float64) { |
| 16 |
|
| 17 |
c1[0], c1[1] = c[0], c[1] |
| 18 |
|
| 19 |
c2[6], c2[7] = c[6], c[7] |
| 20 |
|
| 21 |
|
| 22 |
c1[2] = (c[0] + c[2]) / 2 |
| 23 |
c1[3] = (c[1] + c[3]) / 2 |
| 24 |
|
| 25 |
midX := (c[2] + c[4]) / 2 |
| 26 |
midY := (c[3] + c[5]) / 2 |
| 27 |
|
| 28 |
c2[4] = (c[4] + c[6]) / 2 |
| 29 |
c2[5] = (c[5] + c[7]) / 2 |
| 30 |
|
| 31 |
c1[4] = (c1[2] + midX) / 2 |
| 32 |
c1[5] = (c1[3] + midY) / 2 |
| 33 |
|
| 34 |
c2[2] = (midX + c2[4]) / 2 |
| 35 |
c2[3] = (midY + c2[5]) / 2 |
| 36 |
|
| 37 |
c1[6] = (c1[4] + c2[2]) / 2 |
| 38 |
c1[7] = (c1[5] + c2[3]) / 2 |
| 39 |
|
| 40 |
|
| 41 |
c2[0], c2[1] = c1[6], c1[7] |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
func TraceCubic(t Liner, cubic []float64, flatteningThreshold float64) { |
| 47 |
|
| 48 |
var curves [CurveRecursionLimit * 8]float64 |
| 49 |
copy(curves[0:8], cubic[0:8]) |
| 50 |
i := 0 |
| 51 |
|
| 52 |
|
| 53 |
var c []float64 |
| 54 |
|
| 55 |
var dx, dy, d2, d3 float64 |
| 56 |
|
| 57 |
for i >= 0 { |
| 58 |
c = curves[i*8:] |
| 59 |
dx = c[6] - c[0] |
| 60 |
dy = c[7] - c[1] |
| 61 |
|
| 62 |
d2 = math.Abs((c[2]-c[6])*dy - (c[3]-c[7])*dx) |
| 63 |
d3 = math.Abs((c[4]-c[6])*dy - (c[5]-c[7])*dx) |
| 64 |
|
| 65 |
|
| 66 |
if (d2+d3)*(d2+d3) < flatteningThreshold*(dx*dx+dy*dy) || i == len(curves)-1 { |
| 67 |
t.LineTo(c[6], c[7]) |
| 68 |
i-- |
| 69 |
} else { |
| 70 |
|
| 71 |
SubdivideCubic(c, curves[(i+1)*8:], curves[i*8:]) |
| 72 |
i++ |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func SubdivideQuad(c, c1, c2 []float64) { |
| 83 |
|
| 84 |
c1[0], c1[1] = c[0], c[1] |
| 85 |
|
| 86 |
c2[4], c2[5] = c[4], c[5] |
| 87 |
|
| 88 |
|
| 89 |
c1[2] = (c[0] + c[2]) / 2 |
| 90 |
c1[3] = (c[1] + c[3]) / 2 |
| 91 |
c2[2] = (c[2] + c[4]) / 2 |
| 92 |
c2[3] = (c[3] + c[5]) / 2 |
| 93 |
c1[4] = (c1[2] + c2[2]) / 2 |
| 94 |
c1[5] = (c1[3] + c2[3]) / 2 |
| 95 |
c2[0], c2[1] = c1[4], c1[5] |
| 96 |
return |
| 97 |
} |
| 98 |
|
| 99 |
func traceWindowIndices(i int) (startAt, endAt int) { |
| 100 |
startAt = i * 6 |
| 101 |
endAt = startAt + 6 |
| 102 |
return |
| 103 |
} |
| 104 |
|
| 105 |
func traceCalcDeltas(c []float64) (dx, dy, d float64) { |
| 106 |
dx = c[4] - c[0] |
| 107 |
dy = c[5] - c[1] |
| 108 |
d = math.Abs(((c[2]-c[4])*dy - (c[3]-c[5])*dx)) |
| 109 |
return |
| 110 |
} |
| 111 |
|
| 112 |
func traceIsFlat(dx, dy, d, threshold float64) bool { |
| 113 |
return (d * d) < threshold*(dx*dx+dy*dy) |
| 114 |
} |
| 115 |
|
| 116 |
func traceGetWindow(curves []float64, i int) []float64 { |
| 117 |
startAt, endAt := traceWindowIndices(i) |
| 118 |
return curves[startAt:endAt] |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
func TraceQuad(t Liner, quad []float64, flatteningThreshold float64) { |
| 124 |
const curveLen = CurveRecursionLimit * 6 |
| 125 |
const curveEndIndex = curveLen - 1 |
| 126 |
const lastIteration = CurveRecursionLimit - 1 |
| 127 |
|
| 128 |
|
| 129 |
curves := make([]float64, curveLen) |
| 130 |
|
| 131 |
|
| 132 |
copy(curves[0:6], quad[0:6]) |
| 133 |
|
| 134 |
var i int |
| 135 |
var c []float64 |
| 136 |
var dx, dy, d float64 |
| 137 |
|
| 138 |
for i >= 0 { |
| 139 |
c = traceGetWindow(curves, i) |
| 140 |
dx, dy, d = traceCalcDeltas(c) |
| 141 |
|
| 142 |
|
| 143 |
if d == 0 { |
| 144 |
return |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
if traceIsFlat(dx, dy, d, flatteningThreshold) || i == lastIteration { |
| 149 |
t.LineTo(c[4], c[5]) |
| 150 |
i-- |
| 151 |
} else { |
| 152 |
SubdivideQuad(c, traceGetWindow(curves, i+1), traceGetWindow(curves, i)) |
| 153 |
i++ |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func TraceArc(t Liner, x, y, rx, ry, start, angle, scale float64) (lastX, lastY float64) { |
| 160 |
end := start + angle |
| 161 |
clockWise := true |
| 162 |
if angle < 0 { |
| 163 |
clockWise = false |
| 164 |
} |
| 165 |
ra := (math.Abs(rx) + math.Abs(ry)) / 2 |
| 166 |
da := math.Acos(ra/(ra+0.125/scale)) * 2 |
| 167 |
|
| 168 |
if !clockWise { |
| 169 |
da = -da |
| 170 |
} |
| 171 |
angle = start + da |
| 172 |
var curX, curY float64 |
| 173 |
for { |
| 174 |
if (angle < end-da/4) != clockWise { |
| 175 |
curX = x + math.Cos(end)*rx |
| 176 |
curY = y + math.Sin(end)*ry |
| 177 |
return curX, curY |
| 178 |
} |
| 179 |
curX = x + math.Cos(angle)*rx |
| 180 |
curY = y + math.Sin(angle)*ry |
| 181 |
|
| 182 |
angle += da |
| 183 |
t.LineTo(curX, curY) |
| 184 |
} |
| 185 |
} |