MCPcopy Create free account
hub / github.com/SolarLune/masterplan / IntersectionPointsLine

Method IntersectionPointsLine

intersectingLine.go:17–46  ·  view source on GitHub ↗

IntersectionPointsLine returns the intersection point of a Line with another Line as a Vector, and if the intersection was found.

(other collidingLine)

Source from the content-addressed store, hash-verified

15
16// IntersectionPointsLine returns the intersection point of a Line with another Line as a Vector, and if the intersection was found.
17func (line collidingLine) IntersectionPointsLine(other collidingLine) (Vector, bool) {
18
19 det := (line.End.X-line.Start.X)*(other.End.Y-other.Start.Y) - (other.End.X-other.Start.X)*(line.End.Y-line.Start.Y)
20
21 if det != 0 {
22
23 // MAGIC MATH; the extra + 1 here makes it so that corner cases (literally, lines going through corners) works.
24
25 // lambda := (float32(((line.Y-b.Y)*(b.X2-b.X))-((line.X-b.X)*(b.Y2-b.Y))) + 1) / float32(det)
26 lambda := (((line.Start.Y - other.Start.Y) * (other.End.X - other.Start.X)) - ((line.Start.X - other.Start.X) * (other.End.Y - other.Start.Y)) + 1) / det
27
28 // gamma := (float32(((line.Y-b.Y)*(line.X2-line.X))-((line.X-b.X)*(line.Y2-line.Y))) + 1) / float32(det)
29 gamma := (((line.Start.Y - other.Start.Y) * (line.End.X - line.Start.X)) - ((line.Start.X - other.Start.X) * (line.End.Y - line.Start.Y)) + 1) / det
30
31 if (0 <= lambda && lambda <= 1) && (0 <= gamma && gamma <= 1) {
32
33 // Delta
34 dx := line.End.X - line.Start.X
35 dy := line.End.Y - line.Start.Y
36
37 // dx, dy := line.GetDelta()
38
39 return Vector{line.Start.X + (lambda * dx), line.Start.Y + (lambda * dy)}, true
40 }
41
42 }
43
44 return Vector{}, false
45
46}

Calls

no outgoing calls

Tested by

no test coverage detected