Public Group

# Java Problem - line intersecting GeneralPath

This topic is 4140 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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

##### Share on other sites
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.

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 9
• 13
• 85
• 11
• 10
• ### Forum Statistics

• Total Topics
632969
• Total Posts
3009599
• ### Who's Online (See full list)

There are no registered users currently online

×