venice
Class Intersections

java.lang.Object
  |
  +--venice.Intersections

public class Intersections
extends java.lang.Object

Class containing static methods which can be used to calculate intersection points for lines against lines, rectangles and ellipses.

This is highly unoptimized code. Performance can be improved.


Constructor Summary
Intersections()
           
 
Method Summary
static java.awt.geom.Point2D lineIntersectLine(java.awt.geom.Line2D inLine1, java.awt.geom.Line2D inLine2)
          Calculates the intersection between two line segments.
static java.awt.geom.Point2D lineIntersectsEllipse(java.awt.geom.Line2D inLine, java.awt.geom.RectangularShape inEllipse)
          Checks whether the given line intersects with the given ellipse.
static java.awt.geom.Point2D lineIntersectsRectangle(java.awt.geom.Line2D inLine, java.awt.geom.RectangularShape inRectangle)
          Calculates whether the given line and rectangle intersect and returns the intersection point.
static void main(java.lang.String[] argv)
          Public main to test this class and its correctness.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Constructor Detail

Intersections

public Intersections()
Method Detail

lineIntersectLine

public static java.awt.geom.Point2D lineIntersectLine(java.awt.geom.Line2D inLine1,
                                                      java.awt.geom.Line2D inLine2)
Calculates the intersection between two line segments. The intersection point is returned. If the segments do not intersect, then null is returned.

Caution! Current implementation does not return an intersection for two collinear segments. This may be fixed in a later version.

Algorithm from comp.graphics.algorithm FAQ

Parameters:
inLine1 - first line
inLine2 - second line
Returns:
the intersection point or null if the lines don't intersect

lineIntersectsRectangle

public static java.awt.geom.Point2D lineIntersectsRectangle(java.awt.geom.Line2D inLine,
                                                            java.awt.geom.RectangularShape inRectangle)
Calculates whether the given line and rectangle intersect and returns the intersection point. If they do not intersect, null is returned.

Caution! A line segment can have two intersection points with a rectangle. This method returns at most one intersection point. This method will return the intersection point, which will cut the line segment to the shortest possible length.

Parameters:
inLine - line segment which to check
inRectangle - rectangle against which to check intersection
Returns:
intersection point with the given rectangle

lineIntersectsEllipse

public static java.awt.geom.Point2D lineIntersectsEllipse(java.awt.geom.Line2D inLine,
                                                          java.awt.geom.RectangularShape inEllipse)
Checks whether the given line intersects with the given ellipse. Returns the point of intersection or null, if they do not intersect. A line segment and an ellipse may have two intersection points, but this method will return only one. There is no guarantee which one is returned.

Caution! Currently this code treats the ellipse as a circle. It assumes it has the same height as width.

Parameters:
inLine - line to check
inEllipse - ellipse to check against
Returns:
point of intersection or null

main

public static void main(java.lang.String[] argv)
Public main to test this class and its correctness.