path.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. // Copyright 2019 The Ebiten Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Package vector provides functions for vector graphics rendering.
  15. //
  16. // This package is under experiments and the API might be changed with breaking backward compatibility.
  17. package vector
  18. import (
  19. "math"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. )
  22. // Direction represents clockwise or counterclockwise.
  23. type Direction int
  24. const (
  25. Clockwise Direction = iota
  26. CounterClockwise
  27. )
  28. type opType int
  29. const (
  30. opTypeMoveTo opType = iota
  31. opTypeLineTo
  32. opTypeQuadTo
  33. opTypeCubicTo
  34. opTypeClose
  35. )
  36. type op struct {
  37. typ opType
  38. p1 point
  39. p2 point
  40. p3 point
  41. }
  42. func abs(x float32) float32 {
  43. if x < 0 {
  44. return -x
  45. }
  46. return x
  47. }
  48. type point struct {
  49. x float32
  50. y float32
  51. }
  52. type subpath struct {
  53. points []point
  54. closed bool
  55. }
  56. // reset resets the subpath.
  57. // reset doesn't release the allocated memory so that the memory can be reused.
  58. func (s *subpath) reset() {
  59. s.points = s.points[:0]
  60. s.closed = false
  61. }
  62. func (s subpath) pointCount() int {
  63. return len(s.points)
  64. }
  65. func (s subpath) lastPoint() point {
  66. return s.points[len(s.points)-1]
  67. }
  68. func (s *subpath) appendPoint(pt point) {
  69. if s.closed {
  70. panic("vector: a closed subpathment cannot append a new point")
  71. }
  72. if len(s.points) > 0 {
  73. // Do not add a too close point to the last point.
  74. // This can cause unexpected rendering results.
  75. if lp := s.lastPoint(); abs(lp.x-pt.x) < 1e-2 && abs(lp.y-pt.y) < 1e-2 {
  76. return
  77. }
  78. }
  79. s.points = append(s.points, pt)
  80. }
  81. func (s *subpath) close() {
  82. if s.closed {
  83. return
  84. }
  85. s.appendPoint(s.points[0])
  86. s.closed = true
  87. }
  88. // Path represents a collection of path subpathments.
  89. type Path struct {
  90. ops []op
  91. // subpaths is a cached actual rendering positions.
  92. subpaths []subpath
  93. }
  94. // Reset resets the path.
  95. // Reset doesn't release the allocated memory so that the memory can be reused.
  96. func (p *Path) Reset() {
  97. p.ops = p.ops[:0]
  98. p.subpaths = p.subpaths[:0]
  99. }
  100. func (p *Path) appendNewSubpath(pt point) {
  101. if cap(p.subpaths) > len(p.subpaths) {
  102. // Reuse the last subpath since the last subpath might have an already allocated slice.
  103. p.subpaths = p.subpaths[:len(p.subpaths)+1]
  104. p.subpaths[len(p.subpaths)-1].reset()
  105. p.subpaths[len(p.subpaths)-1].appendPoint(pt)
  106. return
  107. }
  108. p.subpaths = append(p.subpaths, subpath{
  109. points: []point{pt},
  110. })
  111. }
  112. func (p *Path) ensureSubpaths() []subpath {
  113. if len(p.subpaths) > 0 || len(p.ops) == 0 {
  114. return p.subpaths
  115. }
  116. var cur point
  117. for _, op := range p.ops {
  118. switch op.typ {
  119. case opTypeMoveTo:
  120. p.appendNewSubpath(op.p1)
  121. cur = op.p1
  122. case opTypeLineTo:
  123. p.lineTo(op.p1)
  124. cur = op.p1
  125. case opTypeQuadTo:
  126. p.quadTo(cur, op.p1, op.p2, 0)
  127. cur = op.p2
  128. case opTypeCubicTo:
  129. p.cubicTo(cur, op.p1, op.p2, op.p3, 0)
  130. cur = op.p3
  131. case opTypeClose:
  132. p.close()
  133. cur = point{}
  134. }
  135. }
  136. return p.subpaths
  137. }
  138. // MoveTo starts a new subpath with the given position (x, y) without adding a subpath,
  139. func (p *Path) MoveTo(x, y float32) {
  140. p.subpaths = p.subpaths[:0]
  141. p.ops = append(p.ops, op{
  142. typ: opTypeMoveTo,
  143. p1: point{x: x, y: y},
  144. })
  145. }
  146. // LineTo adds a line segment to the path, which starts from the last position of the current subpath
  147. // and ends to the given position (x, y).
  148. // If p doesn't have any subpaths or the last subpath is closed, LineTo sets (x, y) as the start position of a new subpath.
  149. func (p *Path) LineTo(x, y float32) {
  150. p.subpaths = p.subpaths[:0]
  151. p.ops = append(p.ops, op{
  152. typ: opTypeLineTo,
  153. p1: point{x: x, y: y},
  154. })
  155. }
  156. // QuadTo adds a quadratic Bézier curve to the path.
  157. // (x1, y1) is the control point, and (x2, y2) is the destination.
  158. func (p *Path) QuadTo(x1, y1, x2, y2 float32) {
  159. p.subpaths = p.subpaths[:0]
  160. p.ops = append(p.ops, op{
  161. typ: opTypeQuadTo,
  162. p1: point{x: x1, y: y1},
  163. p2: point{x: x2, y: y2},
  164. })
  165. }
  166. // CubicTo adds a cubic Bézier curve to the path.
  167. // (x1, y1) and (x2, y2) are the control points, and (x3, y3) is the destination.
  168. func (p *Path) CubicTo(x1, y1, x2, y2, x3, y3 float32) {
  169. p.subpaths = p.subpaths[:0]
  170. p.ops = append(p.ops, op{
  171. typ: opTypeCubicTo,
  172. p1: point{x: x1, y: y1},
  173. p2: point{x: x2, y: y2},
  174. p3: point{x: x3, y: y3},
  175. })
  176. }
  177. // Close adds a new line from the last position of the current subpath to the first position of the current subpath,
  178. // and marks the current subpath closed.
  179. // Following operations for this path will start with a new subpath.
  180. func (p *Path) Close() {
  181. p.subpaths = p.subpaths[:0]
  182. p.ops = append(p.ops, op{
  183. typ: opTypeClose,
  184. })
  185. }
  186. func (p *Path) lineTo(pt point) {
  187. if len(p.subpaths) == 0 || p.subpaths[len(p.subpaths)-1].closed {
  188. p.appendNewSubpath(pt)
  189. return
  190. }
  191. p.subpaths[len(p.subpaths)-1].appendPoint(pt)
  192. }
  193. // lineForTwoPoints returns parameters for a line passing through p0 and p1.
  194. func lineForTwoPoints(p0, p1 point) (a, b, c float32) {
  195. // Line passing through p0 and p1 in the form of ax + by + c = 0
  196. a = p1.y - p0.y
  197. b = -(p1.x - p0.x)
  198. c = (p1.x-p0.x)*p0.y - (p1.y-p0.y)*p0.x
  199. return
  200. }
  201. // isPointCloseToSegment detects the distance between a segment (x0, y0)-(x1, y1) and a point (x, y) is less than allow.
  202. // If p0 and p1 are the same, isPointCloseToSegment returns true when the distance between p0 and p is less than allow.
  203. func isPointCloseToSegment(p, p0, p1 point, allow float32) bool {
  204. if p0 == p1 {
  205. return allow*allow >= (p0.x-p.x)*(p0.x-p.x)+(p0.y-p.y)*(p0.y-p.y)
  206. }
  207. a, b, c := lineForTwoPoints(p0, p1)
  208. // The distance between a line ax+by+c=0 and (x0, y0) is
  209. // |ax0 + by0 + c| / √(a² + b²)
  210. return allow*allow*(a*a+b*b) >= (a*p.x+b*p.y+c)*(a*p.x+b*p.y+c)
  211. }
  212. // crossingPointForTwoLines returns a crossing point for two lines.
  213. func crossingPointForTwoLines(p00, p01, p10, p11 point) point {
  214. a0, b0, c0 := lineForTwoPoints(p00, p01)
  215. a1, b1, c1 := lineForTwoPoints(p10, p11)
  216. det := a0*b1 - a1*b0
  217. return point{
  218. x: (b0*c1 - b1*c0) / det,
  219. y: (a1*c0 - a0*c1) / det,
  220. }
  221. }
  222. func (p *Path) quadTo(p0, p1, p2 point, level int) {
  223. if level > 10 {
  224. return
  225. }
  226. if isPointCloseToSegment(p1, p0, p2, 0.5) {
  227. p.lineTo(p2)
  228. return
  229. }
  230. p01 := point{
  231. x: (p0.x + p1.x) / 2,
  232. y: (p0.y + p1.y) / 2,
  233. }
  234. p12 := point{
  235. x: (p1.x + p2.x) / 2,
  236. y: (p1.y + p2.y) / 2,
  237. }
  238. p012 := point{
  239. x: (p01.x + p12.x) / 2,
  240. y: (p01.y + p12.y) / 2,
  241. }
  242. p.quadTo(p0, p01, p012, level+1)
  243. p.quadTo(p012, p12, p2, level+1)
  244. }
  245. func (p *Path) cubicTo(p0, p1, p2, p3 point, level int) {
  246. if level > 10 {
  247. return
  248. }
  249. if isPointCloseToSegment(p1, p0, p3, 0.5) && isPointCloseToSegment(p2, p0, p3, 0.5) {
  250. p.lineTo(p3)
  251. return
  252. }
  253. p01 := point{
  254. x: (p0.x + p1.x) / 2,
  255. y: (p0.y + p1.y) / 2,
  256. }
  257. p12 := point{
  258. x: (p1.x + p2.x) / 2,
  259. y: (p1.y + p2.y) / 2,
  260. }
  261. p23 := point{
  262. x: (p2.x + p3.x) / 2,
  263. y: (p2.y + p3.y) / 2,
  264. }
  265. p012 := point{
  266. x: (p01.x + p12.x) / 2,
  267. y: (p01.y + p12.y) / 2,
  268. }
  269. p123 := point{
  270. x: (p12.x + p23.x) / 2,
  271. y: (p12.y + p23.y) / 2,
  272. }
  273. p0123 := point{
  274. x: (p012.x + p123.x) / 2,
  275. y: (p012.y + p123.y) / 2,
  276. }
  277. p.cubicTo(p0, p01, p012, p0123, level+1)
  278. p.cubicTo(p0123, p123, p23, p3, level+1)
  279. }
  280. func normalize(p point) point {
  281. len := float32(math.Hypot(float64(p.x), float64(p.y)))
  282. return point{x: p.x / len, y: p.y / len}
  283. }
  284. func cross(p0, p1 point) float32 {
  285. return p0.x*p1.y - p1.x*p0.y
  286. }
  287. func (p *Path) currentPosition() (point, bool) {
  288. if len(p.ops) == 0 {
  289. return point{}, false
  290. }
  291. op := p.ops[len(p.ops)-1]
  292. switch op.typ {
  293. case opTypeMoveTo:
  294. return op.p1, true
  295. case opTypeLineTo:
  296. return op.p1, true
  297. case opTypeQuadTo:
  298. return op.p2, true
  299. case opTypeCubicTo:
  300. return op.p3, true
  301. case opTypeClose:
  302. return point{}, false
  303. }
  304. return point{}, false
  305. }
  306. // ArcTo adds an arc curve to the path.
  307. // (x1, y1) is the first control point, and (x2, y2) is the second control point.
  308. func (p *Path) ArcTo(x1, y1, x2, y2, radius float32) {
  309. p0, ok := p.currentPosition()
  310. if !ok {
  311. p0 = point{x: x1, y: y1}
  312. }
  313. d0 := point{
  314. x: p0.x - x1,
  315. y: p0.y - y1,
  316. }
  317. d1 := point{
  318. x: x2 - x1,
  319. y: y2 - y1,
  320. }
  321. if d0 == (point{}) || d1 == (point{}) {
  322. p.LineTo(x1, y1)
  323. return
  324. }
  325. d0 = normalize(d0)
  326. d1 = normalize(d1)
  327. // theta is the angle between two vectors d0 and d1.
  328. theta := math.Acos(float64(d0.x*d1.x + d0.y*d1.y))
  329. // TODO: When theta is bigger than π/2, the arc should be split into two.
  330. // dist is the distance between the control point and the arc's beginning and ending points.
  331. dist := radius / float32(math.Tan(theta/2))
  332. // TODO: What if dist is too big?
  333. // (ax0, ay0) is the start of the arc.
  334. ax0 := x1 + d0.x*dist
  335. ay0 := y1 + d0.y*dist
  336. var cx, cy, a0, a1 float32
  337. var dir Direction
  338. if cross(d0, d1) >= 0 {
  339. cx = ax0 - d0.y*radius
  340. cy = ay0 + d0.x*radius
  341. a0 = float32(math.Atan2(float64(-d0.x), float64(d0.y)))
  342. a1 = float32(math.Atan2(float64(d1.x), float64(-d1.y)))
  343. dir = CounterClockwise
  344. } else {
  345. cx = ax0 + d0.y*radius
  346. cy = ay0 - d0.x*radius
  347. a0 = float32(math.Atan2(float64(d0.x), float64(-d0.y)))
  348. a1 = float32(math.Atan2(float64(-d1.x), float64(d1.y)))
  349. dir = Clockwise
  350. }
  351. p.Arc(cx, cy, radius, a0, a1, dir)
  352. }
  353. // Arc adds an arc to the path.
  354. // (x, y) is the center of the arc.
  355. func (p *Path) Arc(x, y, radius, startAngle, endAngle float32, dir Direction) {
  356. // Adjust the angles.
  357. var da float64
  358. if dir == Clockwise {
  359. for startAngle > endAngle {
  360. endAngle += 2 * math.Pi
  361. }
  362. da = float64(endAngle - startAngle)
  363. } else {
  364. for startAngle < endAngle {
  365. startAngle += 2 * math.Pi
  366. }
  367. da = float64(startAngle - endAngle)
  368. }
  369. if da >= 2*math.Pi {
  370. da = 2 * math.Pi
  371. if dir == Clockwise {
  372. endAngle = startAngle + 2*math.Pi
  373. } else {
  374. startAngle = endAngle + 2*math.Pi
  375. }
  376. }
  377. // If the angle is big, splict this into multiple Arc calls.
  378. if da > math.Pi/2 {
  379. const delta = math.Pi / 3
  380. a := float64(startAngle)
  381. if dir == Clockwise {
  382. for {
  383. p.Arc(x, y, radius, float32(a), float32(math.Min(a+delta, float64(endAngle))), dir)
  384. if a+delta >= float64(endAngle) {
  385. break
  386. }
  387. a += delta
  388. }
  389. } else {
  390. for {
  391. p.Arc(x, y, radius, float32(a), float32(math.Max(a-delta, float64(endAngle))), dir)
  392. if a-delta <= float64(endAngle) {
  393. break
  394. }
  395. a -= delta
  396. }
  397. }
  398. return
  399. }
  400. sin0, cos0 := math.Sincos(float64(startAngle))
  401. x0 := x + radius*float32(cos0)
  402. y0 := y + radius*float32(sin0)
  403. sin1, cos1 := math.Sincos(float64(endAngle))
  404. x1 := x + radius*float32(cos1)
  405. y1 := y + radius*float32(sin1)
  406. p.LineTo(x0, y0)
  407. // Calculate the control points for an approximated Bézier curve.
  408. // See https://learn.microsoft.com/en-us/previous-versions/xamarin/xamarin-forms/user-interface/graphics/skiasharp/curves/beziers.
  409. l := radius * float32(math.Tan(da/4)*4/3)
  410. var cx0, cy0, cx1, cy1 float32
  411. if dir == Clockwise {
  412. cx0 = x0 + l*float32(-sin0)
  413. cy0 = y0 + l*float32(cos0)
  414. cx1 = x1 + l*float32(sin1)
  415. cy1 = y1 + l*float32(-cos1)
  416. } else {
  417. cx0 = x0 + l*float32(sin0)
  418. cy0 = y0 + l*float32(-cos0)
  419. cx1 = x1 + l*float32(-sin1)
  420. cy1 = y1 + l*float32(cos1)
  421. }
  422. p.CubicTo(cx0, cy0, cx1, cy1, x1, y1)
  423. }
  424. func (p *Path) close() {
  425. if len(p.subpaths) == 0 {
  426. return
  427. }
  428. p.subpaths[len(p.subpaths)-1].close()
  429. }
  430. // AppendVerticesAndIndicesForFilling appends vertices and indices to fill this path and returns them.
  431. // AppendVerticesAndIndicesForFilling works in a similar way to the built-in append function.
  432. // If the arguments are nils, AppendVerticesAndIndicesForFilling returns new slices.
  433. //
  434. // The returned vertice's SrcX and SrcY are 0, and ColorR, ColorG, ColorB, and ColorA are 1.
  435. //
  436. // The returned values are intended to be passed to DrawTriangles or DrawTrianglesShader with FileRuleNonZero or FillRuleEvenOdd
  437. // in order to render a complex polygon like a concave polygon, a polygon with holes, or a self-intersecting polygon.
  438. //
  439. // The returned vertices and indices should be rendered with a solid (non-transparent) color with the default Blend (source-over).
  440. // Otherwise, there is no guarantee about the rendering result.
  441. func (p *Path) AppendVerticesAndIndicesForFilling(vertices []ebiten.Vertex, indices []uint16) ([]ebiten.Vertex, []uint16) {
  442. // TODO: Add tests.
  443. base := uint16(len(vertices))
  444. for _, subpath := range p.ensureSubpaths() {
  445. if subpath.pointCount() < 3 {
  446. continue
  447. }
  448. for i, pt := range subpath.points {
  449. vertices = append(vertices, ebiten.Vertex{
  450. DstX: pt.x,
  451. DstY: pt.y,
  452. SrcX: 0,
  453. SrcY: 0,
  454. ColorR: 1,
  455. ColorG: 1,
  456. ColorB: 1,
  457. ColorA: 1,
  458. })
  459. if i < 2 {
  460. continue
  461. }
  462. indices = append(indices, base, base+uint16(i-1), base+uint16(i))
  463. }
  464. base += uint16(subpath.pointCount())
  465. }
  466. return vertices, indices
  467. }
  468. // ApplyGeoM applies the given GeoM to the path and returns a new path.
  469. func (p *Path) ApplyGeoM(geoM ebiten.GeoM) *Path {
  470. // subpaths are not copied.
  471. np := &Path{
  472. ops: make([]op, len(p.ops)),
  473. }
  474. for i, o := range p.ops {
  475. x1, y1 := geoM.Apply(float64(o.p1.x), float64(o.p1.y))
  476. x2, y2 := geoM.Apply(float64(o.p2.x), float64(o.p2.y))
  477. x3, y3 := geoM.Apply(float64(o.p3.x), float64(o.p3.y))
  478. np.ops[i] = op{
  479. typ: o.typ,
  480. p1: point{x: float32(x1), y: float32(y1)},
  481. p2: point{x: float32(x2), y: float32(y2)},
  482. p3: point{x: float32(x3), y: float32(y3)},
  483. }
  484. }
  485. return np
  486. }
  487. // LineCap represents the way in which how the ends of the stroke are rendered.
  488. type LineCap int
  489. const (
  490. LineCapButt LineCap = iota
  491. LineCapRound
  492. LineCapSquare
  493. )
  494. // LineJoin represents the way in which how two segments are joined.
  495. type LineJoin int
  496. const (
  497. LineJoinMiter LineJoin = iota
  498. LineJoinBevel
  499. LineJoinRound
  500. )
  501. // StrokeOptions is options to render a stroke.
  502. type StrokeOptions struct {
  503. // Width is the stroke width in pixels.
  504. //
  505. // The default (zero) value is 0.
  506. Width float32
  507. // LineCap is the way in which how the ends of the stroke are rendered.
  508. // Line caps are not rendered when the subpath is marked as closed.
  509. //
  510. // The default (zero) value is LineCapButt.
  511. LineCap LineCap
  512. // LineJoin is the way in which how two segments are joined.
  513. //
  514. // The default (zero) value is LineJoiMiter.
  515. LineJoin LineJoin
  516. // MiterLimit is the miter limit for LineJoinMiter.
  517. // For details, see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-miterlimit.
  518. //
  519. // The default (zero) value is 0.
  520. MiterLimit float32
  521. }
  522. // AppendVerticesAndIndicesForStroke appends vertices and indices to render a stroke of this path and returns them.
  523. // AppendVerticesAndIndicesForStroke works in a similar way to the built-in append function.
  524. // If the arguments are nils, AppendVerticesAndIndicesForStroke returns new slices.
  525. //
  526. // The returned vertice's SrcX and SrcY are 0, and ColorR, ColorG, ColorB, and ColorA are 1.
  527. //
  528. // The returned values are intended to be passed to DrawTriangles or DrawTrianglesShader with a solid (non-transparent) color
  529. // with FillRuleFillAll or FillRuleNonZero, not FileRuleEvenOdd.
  530. func (p *Path) AppendVerticesAndIndicesForStroke(vertices []ebiten.Vertex, indices []uint16, op *StrokeOptions) ([]ebiten.Vertex, []uint16) {
  531. if op == nil {
  532. return vertices, indices
  533. }
  534. var rects [][4]point
  535. var tmpPath Path
  536. for _, subpath := range p.ensureSubpaths() {
  537. if subpath.pointCount() < 2 {
  538. continue
  539. }
  540. rects = rects[:0]
  541. for i := 0; i < subpath.pointCount()-1; i++ {
  542. pt := subpath.points[i]
  543. nextPt := subpath.points[i+1]
  544. dx := nextPt.x - pt.x
  545. dy := nextPt.y - pt.y
  546. dist := float32(math.Sqrt(float64(dx*dx + dy*dy)))
  547. extX := (dy) * op.Width / 2 / dist
  548. extY := (-dx) * op.Width / 2 / dist
  549. rects = append(rects, [4]point{
  550. {
  551. x: pt.x + extX,
  552. y: pt.y + extY,
  553. },
  554. {
  555. x: nextPt.x + extX,
  556. y: nextPt.y + extY,
  557. },
  558. {
  559. x: pt.x - extX,
  560. y: pt.y - extY,
  561. },
  562. {
  563. x: nextPt.x - extX,
  564. y: nextPt.y - extY,
  565. },
  566. })
  567. }
  568. for i, rect := range rects {
  569. idx := uint16(len(vertices))
  570. for _, pt := range rect {
  571. vertices = append(vertices, ebiten.Vertex{
  572. DstX: pt.x,
  573. DstY: pt.y,
  574. SrcX: 0,
  575. SrcY: 0,
  576. ColorR: 1,
  577. ColorG: 1,
  578. ColorB: 1,
  579. ColorA: 1,
  580. })
  581. }
  582. // All the triangles are rendered in clockwise order to enable FillRuleNonZero (#2833).
  583. indices = append(indices, idx, idx+1, idx+2, idx+1, idx+3, idx+2)
  584. // Add line joints.
  585. var nextRect [4]point
  586. if i < len(rects)-1 {
  587. nextRect = rects[i+1]
  588. } else if subpath.closed {
  589. nextRect = rects[0]
  590. } else {
  591. continue
  592. }
  593. // c is the center of the 'end' edge of the current rect (= the second point of the segment).
  594. c := point{
  595. x: (rect[1].x + rect[3].x) / 2,
  596. y: (rect[1].y + rect[3].y) / 2,
  597. }
  598. // Note that the Y direction and the angle direction are opposite from math's.
  599. a0 := float32(math.Atan2(float64(rect[1].y-c.y), float64(rect[1].x-c.x)))
  600. a1 := float32(math.Atan2(float64(nextRect[0].y-c.y), float64(nextRect[0].x-c.x)))
  601. da := a1 - a0
  602. for da < 0 {
  603. da += 2 * math.Pi
  604. }
  605. if da == 0 {
  606. continue
  607. }
  608. switch op.LineJoin {
  609. case LineJoinMiter:
  610. delta := math.Pi - da
  611. exceed := float32(math.Abs(1/math.Sin(float64(delta/2)))) > op.MiterLimit
  612. // Quadrilateral
  613. tmpPath.Reset()
  614. tmpPath.MoveTo(c.x, c.y)
  615. if da < math.Pi {
  616. tmpPath.LineTo(rect[1].x, rect[1].y)
  617. if !exceed {
  618. pt := crossingPointForTwoLines(rect[0], rect[1], nextRect[0], nextRect[1])
  619. tmpPath.LineTo(pt.x, pt.y)
  620. }
  621. tmpPath.LineTo(nextRect[0].x, nextRect[0].y)
  622. } else {
  623. tmpPath.LineTo(rect[3].x, rect[3].y)
  624. if !exceed {
  625. pt := crossingPointForTwoLines(rect[2], rect[3], nextRect[2], nextRect[3])
  626. tmpPath.LineTo(pt.x, pt.y)
  627. }
  628. tmpPath.LineTo(nextRect[2].x, nextRect[2].y)
  629. }
  630. vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
  631. case LineJoinBevel:
  632. // Triangle
  633. tmpPath.Reset()
  634. tmpPath.MoveTo(c.x, c.y)
  635. if da < math.Pi {
  636. tmpPath.LineTo(rect[1].x, rect[1].y)
  637. tmpPath.LineTo(nextRect[0].x, nextRect[0].y)
  638. } else {
  639. tmpPath.LineTo(rect[3].x, rect[3].y)
  640. tmpPath.LineTo(nextRect[2].x, nextRect[2].y)
  641. }
  642. vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
  643. case LineJoinRound:
  644. // Arc
  645. tmpPath.Reset()
  646. tmpPath.MoveTo(c.x, c.y)
  647. if da < math.Pi {
  648. tmpPath.Arc(c.x, c.y, op.Width/2, a0, a1, Clockwise)
  649. } else {
  650. tmpPath.Arc(c.x, c.y, op.Width/2, a0+math.Pi, a1+math.Pi, CounterClockwise)
  651. }
  652. vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
  653. }
  654. }
  655. if len(rects) == 0 {
  656. continue
  657. }
  658. // If the subpath is closed, do not render line caps.
  659. if subpath.closed {
  660. continue
  661. }
  662. switch op.LineCap {
  663. case LineCapButt:
  664. // Do nothing.
  665. case LineCapRound:
  666. startR, endR := rects[0], rects[len(rects)-1]
  667. {
  668. c := point{
  669. x: (startR[0].x + startR[2].x) / 2,
  670. y: (startR[0].y + startR[2].y) / 2,
  671. }
  672. a := float32(math.Atan2(float64(startR[0].y-startR[2].y), float64(startR[0].x-startR[2].x)))
  673. // Arc
  674. tmpPath.Reset()
  675. tmpPath.MoveTo(startR[0].x, startR[0].y)
  676. tmpPath.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, CounterClockwise)
  677. vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
  678. }
  679. {
  680. c := point{
  681. x: (endR[1].x + endR[3].x) / 2,
  682. y: (endR[1].y + endR[3].y) / 2,
  683. }
  684. a := float32(math.Atan2(float64(endR[1].y-endR[3].y), float64(endR[1].x-endR[3].x)))
  685. // Arc
  686. tmpPath.Reset()
  687. tmpPath.MoveTo(endR[1].x, endR[1].y)
  688. tmpPath.Arc(c.x, c.y, op.Width/2, a, a+math.Pi, Clockwise)
  689. vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
  690. }
  691. case LineCapSquare:
  692. startR, endR := rects[0], rects[len(rects)-1]
  693. {
  694. a := math.Atan2(float64(startR[0].y-startR[1].y), float64(startR[0].x-startR[1].x))
  695. s, c := math.Sincos(a)
  696. dx, dy := float32(c)*op.Width/2, float32(s)*op.Width/2
  697. // Quadrilateral
  698. tmpPath.Reset()
  699. tmpPath.MoveTo(startR[0].x, startR[0].y)
  700. tmpPath.LineTo(startR[0].x+dx, startR[0].y+dy)
  701. tmpPath.LineTo(startR[2].x+dx, startR[2].y+dy)
  702. tmpPath.LineTo(startR[2].x, startR[2].y)
  703. vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
  704. }
  705. {
  706. a := math.Atan2(float64(endR[1].y-endR[0].y), float64(endR[1].x-endR[0].x))
  707. s, c := math.Sincos(a)
  708. dx, dy := float32(c)*op.Width/2, float32(s)*op.Width/2
  709. // Quadrilateral
  710. tmpPath.Reset()
  711. tmpPath.MoveTo(endR[1].x, endR[1].y)
  712. tmpPath.LineTo(endR[1].x+dx, endR[1].y+dy)
  713. tmpPath.LineTo(endR[3].x+dx, endR[3].y+dy)
  714. tmpPath.LineTo(endR[3].x, endR[3].y)
  715. vertices, indices = tmpPath.AppendVerticesAndIndicesForFilling(vertices, indices)
  716. }
  717. }
  718. }
  719. return vertices, indices
  720. }