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() {
return l.A
}
return V(x, y)
return l.B
}
perpendicularM := -1 / m
@ -320,14 +320,16 @@ func (l Line) Intersect(k Line) (Vec, bool) {
// One line is vertical
intersectM := lm
intersectB := lb
verticalLine := k
if math.IsInf(math.Abs(lm), 1) {
intersectM = km
intersectB = kb
verticalLine = l
}
y = intersectM*l.A.X + intersectB
x = l.A.X
y = intersectM*verticalLine.A.X + intersectB
x = verticalLine.A.X
} else {
// Coordinates of intersect
x = (kb - lb) / (lm - km)
@ -614,10 +616,7 @@ func (r Rect) IntersectionPoints(l Line) []Vec {
pointMap := make(map[Vec]struct{})
for _, edge := range r.Edges() {
if intersect, ok := edge.Intersect(l); ok {
fmt.Println(edge)
fmt.Println(l)
fmt.Println(intersect)
if intersect, ok := l.Intersect(edge); ok {
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))},
want: []pixel.Vec{},
},
// {
// name: "One intersection point",
// 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))},
// want: []pixel.Vec{pixel.V(2, 1)},
// },
// {
// name: "Two intersection points",
// 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))},
// want: []pixel.Vec{pixel.V(1, 2), pixel.V(5, 2)},
// },
{
name: "One intersection point",
fields: fields{Min: pixel.V(1, 1), Max: pixel.V(5, 5)},
args: args{l: pixel.L(pixel.V(2, 0), pixel.V(2, 3))},
want: []pixel.Vec{pixel.V(2, 1)},
},
{
name: "Two intersection points",
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))},
want: []pixel.Vec{pixel.V(1, 2), pixel.V(5, 2)},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -1085,10 +1085,10 @@ func TestLine_Intersect(t *testing.T) {
},
{
name: "Lines 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,
fields: fields{A: pixel.V(5, 1), B: pixel.V(1, 1)},
args: args{k: pixel.L(pixel.V(2, 0), pixel.V(2, 3))},
want: pixel.V(2, 1),
want1: true,
},
{
name: "Line intersect with vertical",
@ -1111,6 +1111,20 @@ func TestLine_Intersect(t *testing.T) {
want: pixel.ZV,
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",
fields: fields{A: pixel.V(0, 0), B: pixel.V(10, 10)},