public static void Solve() { for (double aimedAtX = pointL.X + inc; aimedAtX < pointR.X; aimedAtX += inc) { LineD firstShot = new LineD(Origin, new PointD(aimedAtX, (triangleHeight + 1))); long numBounces = CountBounces(firstShot); } //PointD hit = firstShot.GetIntersection(B); }
private static long CountBounces(LineD shot) { long numBounces = 0; LineD reflectedFrom = null; LineD lastReflectedFrom = null; PointD p = null; LineDIntersection intersect; do { // find intersection (try each of three lines) p = null; // reset reflectedFrom = null; if (p == null && lastReflectedFrom != TopLine) { intersect = shot.GetIntersection(TopLine); if (intersect.LinesIntersect) { p = intersect.Intersection; reflectedFrom = TopLine; } } if (p == null && lastReflectedFrom != LefLine) { intersect = shot.GetIntersection(LefLine); if (intersect.LinesIntersect) { p = intersect.Intersection; reflectedFrom = LefLine; } } if (p == null && lastReflectedFrom != RigLine) { intersect = shot.GetIntersection(RigLine); if (intersect.LinesIntersect) { p = intersect.Intersection; reflectedFrom = RigLine; } } // compute attack deltas. Reverse the "horizontal" determined by orientation!!! // negate the "horizontal" element // compute the new line // increaseshot count numBounces++; lastReflectedFrom = reflectedFrom; } while (p != Origin); return numBounces; }
public LineDIntersection GetIntersection(LineD b) { // http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/ double x1 = A.X; double x2 = B.X; double x3 = b.A.X; double x4 = b.B.X; double y1 = A.Y; double y2 = B.Y; double y3 = b.A.Y; double y4 = b.B.Y; double numua = (x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3); double numub = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3); double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); //double denom = (b.B.Y - b.A.Y) * (B.X - A.X) - (b.B.X - b.A.X) * (B.Y - A.Y); //double uanum = (b.B.X - b.A.X) * (A.Y - b.A.Y) - (b.B.Y - b.A.Y) * (A.X - b.A.X); //double ubnum = (B.X - A.X) * (A.Y - b.A.Y) - (B.Y - A.Y) * (B.Y - A.Y); LineDIntersection intersection = new LineDIntersection(); if (denom == 0) { if (numua == 0 && numub == 0) { // lines are coincident! intersection.LinesIntersect = false; intersection.LinesAreCoincident = true; return intersection; } // lines are parallel! intersection.LinesIntersect = false; intersection.LinesAreParallel = true; return intersection; } else { double ua = (numua / denom); double ub = (numub / denom); intersection.ua = ua; intersection.ub = ub; if (ua >= 0.0d && ua <= 1.0d && ub >= 0.0d && ub <= 1.0d) { double x = x1 + ua*(x2 - x1); double y = y1 + ua*(y2 - y1); intersection.LinesIntersect = true; intersection.Intersection = new PointD(x, y); } else { intersection.LinesIntersect = false; } return intersection; } }
private static void TestAndReportOnIntersection(LineD Line1, LineD Line2) { LineDIntersection hit = Line1.GetIntersection(Line2); Console.WriteLine(); Console.WriteLine("Line1 {0}", Line1); Console.WriteLine("Line2 {0}", Line2); if (hit.LinesIntersect) { Console.WriteLine("Found Point {0}", hit.Intersection); Console.WriteLine("ua {0} ub {1}", hit.ua, hit.ub); } else { if (hit.LinesAreParallel) Console.WriteLine("Parallel"); else if (hit.LinesAreCoincident) Console.WriteLine("Coincident"); else { Console.WriteLine("ua {0} ub {1}", hit.ua, hit.ub); Console.WriteLine("May have hit, but not long enough!"); } } }