@@ -82,18 +82,20 @@ func (p *Path2D) lineTo(x, y float64, checkSelfIntersection bool) {
8282 newp .flags |= pathIsConvex
8383 } else if prev .flags & pathIsConvex > 0 {
8484 cuts := false
85+ var cutPoint vec
8586 if checkSelfIntersection && ! Performance .IgnoreSelfIntersections {
8687 b0 , b1 := prev .pos , vec {x , y }
8788 for i := 1 ; i < count ; i ++ {
8889 a0 , a1 := p .p [i - 1 ].pos , p .p [i ].pos
89- _ , r1 , r2 := lineIntersection (a0 , a1 , b0 , b1 )
90+ var r1 , r2 float64
91+ cutPoint , r1 , r2 = lineIntersection (a0 , a1 , b0 , b1 )
9092 if r1 > 0 && r1 < 1 && r2 > 0 && r2 < 1 {
9193 cuts = true
9294 break
9395 }
9496 }
9597 }
96- if cuts {
98+ if cuts && ! isSamePoint ( cutPoint , vec { x , y }, samePointTolerance ) {
9799 newp .flags |= pathSelfIntersects
98100 } else {
99101 prev2 := & p .p [len (p .p )- 3 ]
@@ -288,24 +290,39 @@ func (p *Path2D) Rect(x, y, w, h float64) {
288290// func (p *Path2D) Ellipse(...) {
289291// }
290292
291- func runSubPaths (path []pathPoint , fn func (subPath []pathPoint ) bool ) {
293+ func runSubPaths (path []pathPoint , close bool , fn func (subPath []pathPoint ) bool ) {
292294 start := 0
293295 for i , p := range path {
294296 if p .flags & pathMove == 0 {
295297 continue
296298 }
297299 if i >= start + 3 {
298- if fn (path [start :i ]) {
300+ end := i
301+ if runSubPath (path [start :end ], close , fn ) {
299302 return
300303 }
301304 }
302305 start = i
303306 }
304307 if len (path ) >= start + 3 {
305- fn (path [start :])
308+ runSubPath (path [start :], close , fn )
306309 }
307310}
308311
312+ func runSubPath (path []pathPoint , close bool , fn func (subPath []pathPoint ) bool ) bool {
313+ if ! close || path [0 ].pos == path [len (path )- 1 ].pos {
314+ return fn (path )
315+ }
316+
317+ var buf [64 ]pathPoint
318+ path2 := Path2D {
319+ p : append (buf [:0 ], path ... ),
320+ move : path [0 ].pos ,
321+ }
322+ path2 .lineTo (path [0 ].pos [0 ], path [0 ].pos [1 ], true )
323+ return fn (path2 .p )
324+ }
325+
309326type pathRule uint8
310327
311328// Path rule constants. See https://en.wikipedia.org/wiki/Nonzero-rule
@@ -319,7 +336,7 @@ const (
319336// to the given rule
320337func (p * Path2D ) IsPointInPath (x , y float64 , rule pathRule ) bool {
321338 inside := false
322- runSubPaths (p .p , func (sp []pathPoint ) bool {
339+ runSubPaths (p .p , false , func (sp []pathPoint ) bool {
323340 num := 0
324341 prev := sp [len (sp )- 1 ].pos
325342 for _ , pt := range p .p {
0 commit comments