public Vector3 Intersection(StraightLine l) { if (Math.Abs(Vector3.ScalarMultiplication(l.DirVector, N)) > 0.0001) { double t = (-D - C * l.point.Z - B * l.point.Y - A * l.point.X) / Vector3.ScalarMultiplication(l.DirVector, N); return(new Vector3((l.point.X + t * l.DirVector.X), (l.point.Y + t * l.DirVector.Y), (l.point.Z + t * l.DirVector.Z))); } else { return(new Vector3()); } }
public static bool LineSegmentContainsPoint(Vector3 a, Vector3 b, Vector3 p) { return(( StraightLine.StraightLineContainsPoint(new StraightLine(a, b), p)) && (Math.Min(a.X, b.X) <= p.X && p.X <= Math.Max(a.X, b.X)) && (Math.Min(a.Y, b.Y) <= p.Y && p.Y <= Math.Max(a.Y, b.Y)) && (Math.Min(a.Z, b.Z) <= p.Z && p.Z <= Math.Max(a.Z, b.Z))); }
public static bool StraightLineContainsPoint(StraightLine l, Vector3 a) { if (!a.IsEmpty()) { return ( Math.Abs((a.X - l.point.X) * l.DirVector.Y - (a.Y - l.point.Y) * l.DirVector.X) < 0.0001 && Math.Abs((a.Y - l.point.Y) * l.DirVector.Z - (a.Z - l.point.Z) * l.DirVector.Y) < 0.0001 && Math.Abs((a.X - l.point.X) * l.DirVector.Z - (a.Z - l.point.Z) * l.DirVector.X) < 0.0001 ); } return(false); }
public Vector3 Intersection(StraightLine l)//если знаем, что они в одной плоскости { if (Math.Abs(l.DirVector.X * DirVector.Y - l.DirVector.Y * DirVector.X) < 0.0001 || Math.Abs(l.DirVector.Z * DirVector.Y - l.DirVector.Y * DirVector.Z) < 0.0001 || Math.Abs(l.DirVector.X * DirVector.Z - l.DirVector.Z * DirVector.X) < 0.0001) { return(new Vector3()); } else { //избавиться от деления //но как? double t = (l.point.X - point.X) / (DirVector.X - l.DirVector.X); return(new Vector3(point.X + t * DirVector.X, point.Y + t * DirVector.Y, point.Z + t * DirVector.Z)); } }
public static List <Vector3> TriangleIntersection(Triangle t1, Triangle t2) { //плоскости не параллельны if (t1.plane.A * t2.plane.B != t1.plane.B * t2.plane.A || t1.plane.B * t2.plane.C != t1.plane.C * t2.plane.B || t1.plane.A * t2.plane.C != t1.plane.C * t2.plane.A) { List <Vector3> I1 = new List <Vector3>(), I2 = new List <Vector3>(); Vector3 temp = t1.plane.Intersection(t2.Points[0], t2.Points[1]); if (LineSegmentContainsPoint(t2.Points[0], t2.Points[1], temp)) { I2.Add(temp); } temp = t1.plane.Intersection(t2.Points[1], t2.Points[2]); if (LineSegmentContainsPoint(t2.Points[1], t2.Points[2], temp)) { I2.Add(temp); } temp = t1.plane.Intersection(t2.Points[0], t2.Points[2]); if (LineSegmentContainsPoint(t2.Points[0], t2.Points[2], temp)) { I2.Add(temp); } temp = t2.plane.Intersection(t1.Points[0], t1.Points[1]); if (LineSegmentContainsPoint(t1.Points[0], t1.Points[1], temp)) { I1.Add(temp); } temp = t2.plane.Intersection(t1.Points[1], t1.Points[2]); if (LineSegmentContainsPoint(t1.Points[1], t1.Points[2], temp)) { I1.Add(temp); } temp = t2.plane.Intersection(t1.Points[0], t1.Points[2]); if (LineSegmentContainsPoint(t1.Points[0], t1.Points[2], temp)) { I1.Add(temp); } DeleteRepetitions(I1); DeleteRepetitions(I2); return(LineSegmentsOverlap(I1, I2)); } else if (t1.plane == t2.plane) { List <Vector3> output = new List <Vector3>(); //List<Vector3> I1 = new List<Vector3>(), I2 = new List<Vector3>(); //StraightLine a = new StraightLine(t1.Points[0], t1.Points[1]), // b = new StraightLine(t2.Points[0], t2.Points[1]); //Vector3 temp = a.Intersection(b); //if (StraightLine.StraightLineContainsPoint(a, temp) // && StraightLine.StraightLineContainsPoint(b, temp)) // output.Add(temp); //a = new StraightLine(t1.Points[1], t1.Points[2]); //if (StraightLine.StraightLineContainsPoint(a, temp) // && StraightLine.StraightLineContainsPoint(b, temp)) // output.Add(temp); //a = new StraightLine(t1.Points[0], t1.Points[2]); //if (StraightLine.StraightLineContainsPoint(a, temp) // && StraightLine.StraightLineContainsPoint(b, temp)) // output.Add(temp); StraightLine a, b; Vector3 temp; for (int i = 1; i < 3; i++) { for (int j = 1; j < 3; j++) { a = new StraightLine(t1.Points[i - 1], t1.Points[i]); b = new StraightLine(t2.Points[j - 1], t2.Points[j]); temp = a.Intersection(b); if (StraightLine.StraightLineContainsPoint(a, temp) && StraightLine.StraightLineContainsPoint(b, temp)) { output.Add(temp); } } } for (int j = 1; j < 3; j++) { a = new StraightLine(t1.Points[0], t1.Points[2]); b = new StraightLine(t2.Points[j - 1], t2.Points[j]); temp = a.Intersection(b); if (StraightLine.StraightLineContainsPoint(a, temp) && StraightLine.StraightLineContainsPoint(b, temp)) { output.Add(temp); } } a = new StraightLine(t1.Points[0], t1.Points[2]); b = new StraightLine(t2.Points[0], t2.Points[2]); temp = a.Intersection(b); if (StraightLine.StraightLineContainsPoint(a, temp) && StraightLine.StraightLineContainsPoint(b, temp)) { output.Add(temp); } DeleteRepetitions(output); //DeleteRepetitions(I2); return(output); } else //параллельны { return(new List <Vector3>()); } }