public IIntersection Intersects(ILine3 ray2) { try { var result = new Intersection3(); var ptOut = new Vector2(); if (SlopeXY == ray2.SlopeXY) { result.Intersects = false; } else { double denom = (Point1.X - Point2.X) * (ray2.Point1.Y - ray2.Point2.Y) - (ray2.Point1.X - ray2.Point2.X) * (Point1.Y - Point2.Y); double xOut = 0; double yOut = 0; double line1Det = Point1.X * Point2.Y - Point2.X * Point1.Y; double line2Det = ray2.Point1.X * ray2.Point2.Y - ray2.Point2.X * ray2.Point1.Y; xOut = (line1Det * (ray2.Point1.X - ray2.Point2.X) - line2Det * (Point1.X - Point2.X)) / denom; yOut = (line1Det * (ray2.Point1.Y - ray2.Point2.Y) - line2Det * (Point1.Y - Point2.Y)) / denom; result = new Intersection3(new Vector3(xOut, yOut, 0), true); } return(result); } catch (Exception) { throw; } }
public IIntersection Intersects(ILine3 ray2) { try { IIntersection result = new Intersection3(); Vector3 Point1; Intersection3 ir = new Intersection3(); //transform arc and line to center arc at origin IVector3 trans = new Vector3(-1 * Center.X, -1 * Center.Y, 0); IArc arcT = Translate(trans); ILine3 lineT = ray2.Translate(trans); //parameterize line double dy = lineT.Point2.Y - lineT.Point1.Y; double dx = lineT.Point2.X - lineT.Point1.X; //calc quadratic solution double a = Math.Pow(dx, 2) + Math.Pow(dy, 2); double b = 2 * (dx) * (lineT.Point1.X - arcT.Center.X) + 2 * (dy) * (lineT.Point1.Y - arcT.Center.Y); double c = Math.Pow(lineT.Point1.X - arcT.Center.X, 2) + Math.Pow(lineT.Point1.Y - arcT.Center.Y, 2) - Math.Pow(arcT.Radius, 2); double discrim = Math.Pow(b, 2) - 4 * a * c; if (discrim >= 0)//check solution is real { double tPos = (-b + Math.Sqrt(discrim)) / (2 * a); double tNeg = (-b - Math.Sqrt(discrim)) / (2 * a); double xpos = dx * tPos + lineT.Point1.X; double ypos = dy * tPos + lineT.Point1.Y; double xneg = dx * tNeg + lineT.Point1.X; double yneg = dy * tNeg + lineT.Point1.Y; double angneg = Math.Atan2(yneg, xneg); double angpos = Math.Atan2(ypos, xpos); if (angneg < 0) { angneg += 2 * Math.PI; } if (angpos < 0) { angpos += 2 * Math.PI; } //transform point back before returning if ((angneg >= arcT.StartAngleRad) && (angneg <= arcT.EndAngleRad)) { Point1 = new Vector3(xneg, yneg, 0); var pt = Point1.Translate(new Vector3(Center.X, Center.Y, 0.0)); result = new Intersection3(pt, true); result.Intersects = true; } if ((angpos >= arcT.StartAngleRad) && (angpos <= arcT.EndAngleRad)) { Point1 = new Vector3(xpos, ypos, 0); var pt = Point1.Translate(new Vector3(Center.X, Center.Y, 0.0)); result = new Intersection3(pt, true); result.Intersects = true; } } return(result); } catch (Exception) { throw; } }