vector/internal/math: Optimization: Make InTriangle faster

This commit is contained in:
Hajime Hoshi 2019-12-29 02:37:53 +09:00
parent 08be478dfc
commit 7860df37e1
2 changed files with 15 additions and 3 deletions

View File

@ -31,6 +31,18 @@ func adjacentIndices(indices []uint16, idx int) (uint16, uint16, uint16) {
}
func InTriangle(pt, pt0, pt1, pt2 Point) bool {
if pt.X <= pt0.X && pt.X <= pt1.X && pt.X <= pt2.X {
return false
}
if pt.X >= pt0.X && pt.X >= pt1.X && pt.X >= pt2.X {
return false
}
if pt.Y <= pt0.Y && pt.Y <= pt1.Y && pt.Y <= pt2.Y {
return false
}
if pt.Y >= pt0.Y && pt.Y >= pt1.Y && pt.Y >= pt2.Y {
return false
}
c0 := cross(Vector{pt.X - pt0.X, pt.Y - pt0.Y}, Vector{pt1.X - pt0.X, pt1.Y - pt0.Y})
c1 := cross(Vector{pt.X - pt1.X, pt.Y - pt1.Y}, Vector{pt2.X - pt1.X, pt2.Y - pt1.Y})
c2 := cross(Vector{pt.X - pt2.X, pt.Y - pt2.Y}, Vector{pt0.X - pt2.X, pt0.Y - pt2.Y})

View File

@ -187,7 +187,7 @@ func TestTriangulate(t *testing.T) {
{3, 4},
{3, 3},
},
Out: []uint16{9, 0, 1, 1, 2, 3, 1, 3, 4, 1, 4, 5, 5, 6, 7, 5, 7, 8, 9, 1, 5},
Out: []uint16{9, 0, 1, 1, 2, 3, 1, 3, 4, 9, 1, 4, 8, 5, 6, 8, 6, 7},
},
{
In: []Point{
@ -199,7 +199,7 @@ func TestTriangulate(t *testing.T) {
{3, 1},
{2, 0},
},
Out: []uint16{6, 0, 1, 1, 2, 3, 1, 3, 4, 6, 1, 4, 6, 4, 5},
Out: []uint16{6, 0, 1, 6, 1, 2, 6, 2, 3, 4, 5, 6, 6, 3, 4},
},
{
In: []Point{
@ -291,7 +291,7 @@ func TestTriangulate(t *testing.T) {
{1, 3},
{1, 4},
},
Out: []uint16{7, 8, 9, 7, 9, 10, 12, 7, 10, 12, 10, 11, 6, 13, 14, 6, 14, 15, 6, 15, 16, 17, 0, 1, 17, 1, 2, 16, 17, 2, 16, 2, 3, 16, 3, 4, 16, 4, 5, 6, 16, 5},
Out: []uint16{7, 8, 9, 7, 9, 10, 12, 7, 10, 12, 10, 11, 6, 13, 14, 6, 14, 15, 6, 15, 16, 6, 16, 17, 5, 0, 1, 1, 2, 3, 5, 1, 3, 5, 3, 4},
},
}
for _, tc := range tests {