Skip to content

Commit

Permalink
Merge pull request #236 from bcvery1/master
Browse files Browse the repository at this point in the history
Fixing line intersection for lines through origin
  • Loading branch information
delp authored May 10, 2020
2 parents d2d1f03 + 6b71245 commit 9a9f831
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
- Gamepad API?
- Fixed Line intersects failing on lines passing through (0, 0)

## [v0.10.0-alpha] 2020-05-08
- Upgrade to GLFW 3.3! :tada:
Expand Down
6 changes: 3 additions & 3 deletions geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,12 +406,12 @@ func (l Line) IntersectRect(r Rect) Vec {
// - the point is contained by the rectangle
// - the point is not the corner itself
corners := r.Vertices()
closest := ZV
var closest *Vec
closestCorner := corners[0]
for _, c := range corners {
cc := l.Closest(c)
if closest == ZV || (closest.Len() > cc.Len() && r.Contains(cc)) {
closest = cc
if closest == nil || (closest.Len() > cc.Len() && r.Contains(cc)) {
closest = &cc
closestCorner = c
}
}
Expand Down
18 changes: 18 additions & 0 deletions geometry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,18 @@ func TestLine_Closest(t *testing.T) {
args: args{v: pixel.V(20, 20)},
want: pixel.V(10, 10),
},
{
name: "Vertical line",
fields: fields{A: pixel.V(0, -10), B: pixel.V(0, 10)},
args: args{v: pixel.V(-1, 0)},
want: pixel.V(0, 0),
},
{
name: "Horizontal line",
fields: fields{A: pixel.V(-10, 0), B: pixel.V(10, 0)},
args: args{v: pixel.V(0, -1)},
want: pixel.V(0, 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -1297,6 +1309,12 @@ func TestLine_IntersectRect(t *testing.T) {
args: args{r: pixel.R(20, 20, 21, 21)},
want: pixel.ZV,
},
{
name: "Line intersects at 0,0",
fields: fields{A: pixel.V(0, -10), B: pixel.V(0, 10)},
args: args{r: pixel.R(-1, 0, 2, 2)},
want: pixel.V(-1, 0),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 9a9f831

Please sign in to comment.