/// <summary>
/// Calculate the intersection point of two coplanar lines
/// Source: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline3d/
/// </summary>
/// <param name="l1">First LineMagnet</param>
/// <param name="l2">Second LineMagnet</param>
/// <returns>The intersection point magnet or null if none found</returns>
private PointMagnet createIntersection(LineMagnet l1, LineMagnet l2, GraphicView activeView, System.Windows.Forms.MouseEventArgs e)
{
if (l1 != null && l2 != null)
{
float numer, denom;
float d1, d2, d3, d4, d5;
Vector3 p13 = l1.Position - l2.Position;
Vector3 p21 = l1.Direction;
Vector3 p43 = l2.Direction;
d1 = p13.X * p43.X + p13.Y * p43.Y + p13.Z * p43.Z;
d2 = p43.X * p21.X + p43.Y * p21.Y + p43.Z * p21.Z;
d3 = p13.X * p21.X + p13.Y * p21.Y + p13.Z * p21.Z;
d4 = p43.X * p43.X + p43.Y * p43.Y + p43.Z * p43.Z;
d5 = p21.X * p21.X + p21.Y * p21.Y + p21.Z * p21.Z;
denom = d5 * d4 - d2 * d2;
if (Math.Abs(denom) < float.Epsilon)
{
return(null);
}
numer = d1 * d2 - d3 * d4;
float r = numer / denom;
float s = (d1 + d2 * r) / d4;
Vector3 pa = l1.Position + Vector3.Scale(p21, r);
Vector3 pb = l2.Position + Vector3.Scale(p43, s);
if ((pa - pb).Length() > 0.0001)
{
return(null);
}
// Create magnet
PointMagnet intPtMagnet = new PointMagnet(pa,
PointMagnetType.Intersection);
if (intPtMagnet.Snap(activeView, e.Location) < SnapViewDistance)
{
intPtMagnet.RelatedMagnets.Add(l1);
intPtMagnet.RelatedMagnets.Add(l2);
return(intPtMagnet);
}
}
return(null);
}