Documentation
¶
Overview ¶
Package line implements a 1D line in 2D ambient space.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type L ¶
func (L) Distance ¶
Distance finds the distance between the line l and a point p.
The distance from a line L to a point Q is given by
d := || D x (Q - P) || / || D ||
See https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line#Another_vector_formulation for more information.
func (L) Intersect ¶
Intersect returns the intersection point between two lines.
Returns error if the lines are parallel.
Find the intersection between the two lines as a function of the constraint parameter t.
Given two constraints L, M, we need to find their intersection; WLOG, let's project the intersection point onto L.
We know the parametric equation form of these lines -- that is,
L = P + tD M = Q + uE
At their intersection, we know that L meets M:
L = M => P + tD = Q + uE
We want to find the projection onto L, which means we need to find a concrete value for t. the other parameter u doesn't matter so much -- let's try to get rid of it.
uE = P - Q + tD
Here, we know P, D, Q, and E are vectors, and we can decompose these into a system of equations by isolating their orthogonal (e.g. horizontal and vertical) components.
uEx = Px - Qx + tDx uEy = Py - Qy + tDy
Solving for u, we get
(Px - Qx + tDx) / Ex = (Py - Qy + tDy) / Ey => Ey (Px - Qx + tDx) = Ex (Py - Qy + tDy)
We leave the task of simplifying the above terms as an exercise to the reader. Isolating t, and noting some common substitutions, we get
t = || E x (P - Q) || / || D x E ||
See https://gamedev.stackexchange.com/a/44733 for more information.
func (L) IntersectCircle ¶
IntersectCircle returns the intersection points between a line and a circle. If the line does not intersect a circle, the function will return not successful.
As a line stretches to infinity in both directions, it is not possible for a line to intersect the circle partway.
If the line lies tangent to the circle, then the returned t-values are the same.
See https://stackoverflow.com/a/1084899 for more information.