Skip to content

Commit

Permalink
another triangulation bugfix, points should be considered inside the …
Browse files Browse the repository at this point in the history
…polygon if they are on a line, with a small amount of tolerance
  • Loading branch information
tfriedel6 committed Feb 1, 2020
1 parent 3f85d64 commit 50c7747
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ func lineIntersection(a0, a1, b0, b1 vec) (vec, float64, float64) {
return a0.add(va.mulf(p)), p, q
}

func linePointDistSqr(a, b, p vec) float64 {
v := b.sub(a)
vl := v.len()
vn := v.divf(vl)
d := p.sub(a).dot(vn)
c := a.add(vn.mulf(d))
return p.sub(c).lenSqr()
}

// Fill fills the current path with the current FillStyle
func (cv *Canvas) Fill() {
cv.fillPath(&cv.path, matIdentity())
Expand Down
15 changes: 15 additions & 0 deletions triangulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ func triangleContainsPoint(a, b, c, p vec) bool {
return count == 1
}

const parallelTolerance = 1e-10

func parallel(a1, b1, a2, b2 vec) bool {
ang := b1.sub(a1).angleTo(b2.sub(a2))
return math.Abs(ang) < parallelTolerance || math.Abs(ang-math.Pi) < parallelTolerance
}

func polygonContainsLine(polygon []vec, ia, ib int, a, b vec) bool {
for i := range polygon {
if i == ia || i == ib {
Expand All @@ -89,13 +96,18 @@ func polygonContainsLine(polygon []vec, ia, ib int, a, b vec) bool {
return true
}

const onLineToleranceSqr = 1e-20

func polygonContainsPoint(polygon []vec, p vec) bool {
a := polygon[len(polygon)-1]
count := 0
for _, b := range polygon {
if r, _ := pointIsRightOfLine(a, b, p); r {
count++
}
if linePointDistSqr(a, b, p) < onLineToleranceSqr {
return true
}
a = b
}
return count%2 == 1
Expand Down Expand Up @@ -129,6 +141,9 @@ func triangulatePath(path []pathPoint, mat mat, target [][2]float64) [][2]float6
if !polygonContainsLine(polygon, i, ic, a, c) {
continue
}
if parallel(a, b, b, c) {
continue
}
target = append(target, a, b, c)
break
}
Expand Down

0 comments on commit 50c7747

Please sign in to comment.