Java Problem - line intersecting GeneralPath

Started by
1 comment, last by Antheus 16 years, 10 months ago
I am trying to create a method that will allow me to determine if a given straight line will intersect a given GeneralPath. Basically, I'm trying to create the body for this method: public boolean intersects(GeneralPath P, double x1, double y1, double x2, double y2) { ... } where the straight line is from points (x1,y1) to (x2.y2). Any help would be much appreciated
Advertisement
EDIT:

I should read more carefully, you are talking about line segments. In that case, I believe GeneralPath implements an intersects() method.

I don't know if it is mathematically feasible to check if a line intersects a complex shape such as a GeneralPath. That is why games generally resort to bounding boxes to make collision detection easier. The solution depends on what you are trying to accomplish and how accurate you need the method to be. If converting your GeneralPath to a rectangle sounds acceptable, check the Rectangle2D class and specifically the intersectsLine() method.

http://java.sun.com/javase/6/docs/api/java/awt/geom/Rectangle2D.html
Get PathIterator from GeneralPath.

Test every segment of the path iterator to your line (Line2D class).

Line2D has intersects() method which accepts your format.

I presume that there is no general solution, since intersections can be complex, and getting segment-wise intersections is only one possible case. It gets much more complicated if your path is made up of non-linear segments.

Although, in typical over-engineered Java style, there is no direct implementation of this overly common case, just everything else.

This topic is closed to new replies.

Advertisement