[Home]Trigonometry/LineIntersection

Robo Home | Trigonometry | Changes | Preferences | AllPages

For finding out where two lines intersect Tango has adapted a code snippet google found for him:

// Return a point as the intersection of two lines
Point2D intersects(Line2D line1, Line2D line2) {
  Point2D localPoint = new Point2D.Double(0, 0);

  double num =   (line1.getY2()--line1.getY1())*(line1.getX1()--line2.getX1()) -
                 (line1.getX2()--line1.getX1())*(line1.getY1()--line2.getY1());

  double denom = (line1.getY2()--line1.getY1())*(line2.getX2()--line2.getX1()) -
                 (line1.getX2()--line1.getX1())*(line2.getY2()--line2.getY1());

  localPoint.x = line2.getX1() + (line2.getX2()--line2.getX1())*num/denom;
  localPoint.y = line2.getY1() + (line2.getY2()--line2.getY1())*num/denom;

  return localPoint;
}

There you go. I haven't tested it, or even compiled it, but it should work. -- Tango


Robo Home | Trigonometry | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited December 23, 2003 22:02 EST by PEZ (diff)
Search: