WIP line tests
This commit is contained in:
parent
04c3ef72a3
commit
997f23dfb5
17
geometry.go
17
geometry.go
|
@ -222,11 +222,18 @@ func (l Line) Closest(v Vec) Vec {
|
||||||
m, b := l.Formula()
|
m, b := l.Formula()
|
||||||
perpendicularM := -1 / m
|
perpendicularM := -1 / m
|
||||||
perpendicularB := v.Y - (perpendicularM * v.X)
|
perpendicularB := v.Y - (perpendicularM * v.X)
|
||||||
|
fmt.Println(m)
|
||||||
|
fmt.Println(b)
|
||||||
|
fmt.Println(perpendicularM)
|
||||||
|
fmt.Println(perpendicularB)
|
||||||
|
|
||||||
// Coordinates of intersect (of infinite lines)
|
// Coordinates of intersect (of infinite lines)
|
||||||
x := (perpendicularB - b) / (m - perpendicularM)
|
x := (perpendicularB - b) / (m - perpendicularM)
|
||||||
y := m*x - b
|
y := m*x - b
|
||||||
|
|
||||||
|
fmt.Println(x)
|
||||||
|
fmt.Println(y)
|
||||||
|
|
||||||
return V(x, y)
|
return V(x, y)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,10 +253,13 @@ func (l Line) Formula() (m, b float64) {
|
||||||
// Intersect will return the point of intersection for the two line segments. If the line segments do not intersect,
|
// Intersect will return the point of intersection for the two line segments. If the line segments do not intersect,
|
||||||
// this function will return the zero-vector and `false`.
|
// this function will return the zero-vector and `false`.
|
||||||
func (l Line) Intersect(k Line) (Vec, bool) {
|
func (l Line) Intersect(k Line) (Vec, bool) {
|
||||||
|
fmt.Printf("%v, %v\n", l, k)
|
||||||
// Check if the lines are parallel
|
// Check if the lines are parallel
|
||||||
lDir := l.A.To(l.B)
|
lDir := l.A.To(l.B)
|
||||||
kDir := k.A.To(k.B)
|
kDir := k.A.To(k.B)
|
||||||
if math.Abs(lDir.X) == math.Abs(kDir.X) && math.Abs(lDir.Y) == math.Abs(kDir.Y) {
|
fmt.Printf("%v, %v\n", lDir, kDir)
|
||||||
|
if lDir.X == kDir.X && lDir.Y == kDir.Y {
|
||||||
|
fmt.Println("p")
|
||||||
return ZV, false
|
return ZV, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,11 +267,14 @@ func (l Line) Intersect(k Line) (Vec, bool) {
|
||||||
// Get the intersection point for the lines if they were infinitely long, check if the point exists on both of the
|
// Get the intersection point for the lines if they were infinitely long, check if the point exists on both of the
|
||||||
// segments
|
// segments
|
||||||
lm, lb := l.Formula()
|
lm, lb := l.Formula()
|
||||||
km, kb := l.Formula()
|
km, kb := k.Formula()
|
||||||
|
fmt.Printf("%.2f, %.2f -- %.2f, %.2f\n", lm, lb, km, kb)
|
||||||
|
|
||||||
// Coordinates of intersect
|
// Coordinates of intersect
|
||||||
x := (kb - lb) / (lm - km)
|
x := (kb - lb) / (lm - km)
|
||||||
y := lm*x + lb
|
y := lm*x + lb
|
||||||
|
fmt.Printf("(%.2f, %.2f)\n", x, y)
|
||||||
|
fmt.Printf("%t %t\n", l.Contains(V(x, y)), k.Contains(V(x, y)))
|
||||||
|
|
||||||
if l.Contains(V(x, y)) && k.Contains(V(x, y)) {
|
if l.Contains(V(x, y)) && k.Contains(V(x, y)) {
|
||||||
// The intersect point is on both line segments, they intersect.
|
// The intersect point is on both line segments, they intersect.
|
||||||
|
|
|
@ -893,6 +893,12 @@ func TestLine_Contains(t *testing.T) {
|
||||||
args: args{v: pixel.V(5, 5)},
|
args: args{v: pixel.V(5, 5)},
|
||||||
want: true,
|
want: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "Point on negative sloped line",
|
||||||
|
fields: fields{A: pixel.V(0, 10), B: pixel.V(10, 0)},
|
||||||
|
args: args{v: pixel.V(5, 5)},
|
||||||
|
want: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "Point not on line",
|
name: "Point not on line",
|
||||||
fields: fields{A: pixel.V(0, 0), B: pixel.V(10, 10)},
|
fields: fields{A: pixel.V(0, 0), B: pixel.V(10, 10)},
|
||||||
|
|
Loading…
Reference in New Issue