fixed line intersect and added rect intersection points

This commit is contained in:
Ben Cragg 2019-04-03 16:58:30 +01:00
parent 83c62a0313
commit 4795a92b41
2 changed files with 36 additions and 23 deletions

View File

@ -248,7 +248,7 @@ func (l Line) Closest(v Vec) Vec {
if l.A.To(v).Len() < l.B.To(v).Len() { if l.A.To(v).Len() < l.B.To(v).Len() {
return l.A return l.A
} }
return V(x, y) return l.B
} }
perpendicularM := -1 / m perpendicularM := -1 / m
@ -320,14 +320,16 @@ func (l Line) Intersect(k Line) (Vec, bool) {
// One line is vertical // One line is vertical
intersectM := lm intersectM := lm
intersectB := lb intersectB := lb
verticalLine := k
if math.IsInf(math.Abs(lm), 1) { if math.IsInf(math.Abs(lm), 1) {
intersectM = km intersectM = km
intersectB = kb intersectB = kb
verticalLine = l
} }
y = intersectM*l.A.X + intersectB y = intersectM*verticalLine.A.X + intersectB
x = l.A.X x = verticalLine.A.X
} else { } else {
// Coordinates of intersect // Coordinates of intersect
x = (kb - lb) / (lm - km) x = (kb - lb) / (lm - km)
@ -614,10 +616,7 @@ func (r Rect) IntersectionPoints(l Line) []Vec {
pointMap := make(map[Vec]struct{}) pointMap := make(map[Vec]struct{})
for _, edge := range r.Edges() { for _, edge := range r.Edges() {
if intersect, ok := edge.Intersect(l); ok { if intersect, ok := l.Intersect(edge); ok {
fmt.Println(edge)
fmt.Println(l)
fmt.Println(intersect)
pointMap[intersect] = struct{}{} pointMap[intersect] = struct{}{}
} }
} }

View File

@ -808,18 +808,18 @@ func TestRect_IntersectionPoints(t *testing.T) {
args: args{l: pixel.L(pixel.V(-5, 0), pixel.V(-2, 2))}, args: args{l: pixel.L(pixel.V(-5, 0), pixel.V(-2, 2))},
want: []pixel.Vec{}, want: []pixel.Vec{},
}, },
// { {
// name: "One intersection point", name: "One intersection point",
// fields: fields{Min: pixel.V(1, 1), Max: pixel.V(5, 5)}, fields: fields{Min: pixel.V(1, 1), Max: pixel.V(5, 5)},
// args: args{l: pixel.L(pixel.V(2, 0), pixel.V(2, 2))}, args: args{l: pixel.L(pixel.V(2, 0), pixel.V(2, 3))},
// want: []pixel.Vec{pixel.V(2, 1)}, want: []pixel.Vec{pixel.V(2, 1)},
// }, },
// { {
// name: "Two intersection points", name: "Two intersection points",
// fields: fields{Min: pixel.V(1, 1), Max: pixel.V(5, 5)}, fields: fields{Min: pixel.V(1, 1), Max: pixel.V(5, 5)},
// args: args{l: pixel.L(pixel.V(0, 2), pixel.V(6, 2))}, args: args{l: pixel.L(pixel.V(0, 2), pixel.V(6, 2))},
// want: []pixel.Vec{pixel.V(1, 2), pixel.V(5, 2)}, want: []pixel.Vec{pixel.V(1, 2), pixel.V(5, 2)},
// }, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
@ -1085,10 +1085,10 @@ func TestLine_Intersect(t *testing.T) {
}, },
{ {
name: "Lines intersect 2", name: "Lines intersect 2",
fields: fields{A: pixel.V(1, 1), B: pixel.V(1, 5)}, fields: fields{A: pixel.V(5, 1), B: pixel.V(1, 1)},
args: args{k: pixel.L(pixel.V(-5, 0), pixel.V(-2, 2))}, args: args{k: pixel.L(pixel.V(2, 0), pixel.V(2, 3))},
want: pixel.ZV, want: pixel.V(2, 1),
want1: false, want1: true,
}, },
{ {
name: "Line intersect with vertical", name: "Line intersect with vertical",
@ -1111,6 +1111,20 @@ func TestLine_Intersect(t *testing.T) {
want: pixel.ZV, want: pixel.ZV,
want1: false, want1: false,
}, },
{
name: "Lines don't intersect 2",
fields: fields{A: pixel.V(1, 1), B: pixel.V(1, 5)},
args: args{k: pixel.L(pixel.V(-5, 0), pixel.V(-2, 2))},
want: pixel.ZV,
want1: false,
},
{
name: "Lines don't intersect 3",
fields: fields{A: pixel.V(2, 0), B: pixel.V(2, 3)},
args: args{k: pixel.L(pixel.V(1, 5), pixel.V(5, 5))},
want: pixel.ZV,
want1: false,
},
{ {
name: "Lines parallel", name: "Lines parallel",
fields: fields{A: pixel.V(0, 0), B: pixel.V(10, 10)}, fields: fields{A: pixel.V(0, 0), B: pixel.V(10, 10)},