This commit is contained in:
Will Charczuk 2017-05-16 17:50:17 -07:00
parent 5936b89e89
commit 04a4edcb46
7 changed files with 135 additions and 69 deletions

View file

@ -251,3 +251,17 @@ func (m mathUtil) RotateCoordinate(cx, cy, x, y int, thetaRadians float64) (rx,
ry = int(rotatedY) + cy
return
}
func (m mathUtil) LinesIntersect(l0x0, l0y0, l0x1, l0y1, l1x0, l1y0, l1x1, l1y1 float64) bool {
var s0x, s0y, s1x, s1y float64
s0x = l0x1 - l0x0
s0y = l0y1 - l0y0
s1x = l1x1 - l1x0
s1y = l1y1 - l1y0
var s, t float64
s = (-s0y*(l0x0-l1x0) + s0x*(l0y0-l1y0)) / (-s1x*s0y + s0x*s1y)
t = (s1x*(l0y0-l1y0) - s1y*(l0x0-l1x0)) / (-s1x*s0y + s0x*s1y)
return s >= 0 && s <= 1 && t >= 0 && t <= 1
}

View file

@ -182,3 +182,27 @@ func TestRotateCoordinate45(t *testing.T) {
assert.Equal(7, rx)
assert.Equal(7, ry)
}
func TestLinesIntersect(t *testing.T) {
assert := assert.New(t)
p0x := 1.0
p0y := 1.0
p1x := 3.0
p1y := 1.0
p2x := 2.0
p2y := 2.0
p3x := 2.0
p3y := 0.0
p4x := 2.0
p4y := 2.0
p5x := 3.0
p5y := 2.0
assert.True(Math.LinesIntersect(p0x, p0y, p1x, p1y, p2x, p2y, p3x, p3y))
assert.False(Math.LinesIntersect(p0x, p0y, p1x, p1y, p4x, p4y, p5x, p5y))
}