# Intersection of Line and Polygon and the Area of Polygon

Hi all,

It seems that the intersct() of Line2D will only return a boolean value. I'm looking for some sample code to calculate the intersection of a line and a polygon (or 2 lines) and return the point. In addition, I need to calculate the area of a regular polygon. I guess the the source files of Line2D.intersect() and Graphics2D.fill() will help a lot. I'll look into those files later in the afternoon. But I hope I can get some suggestions from you. Thank you in advance.

My own personal favorite site for questions of this sort is the collection of geometry algorithms posted by Paul Bourke of the University of Western Australia. See http://local.wasp.uwa.edu.au/~pbourke/geometry/.

The particular algorithms you will be most interested in are "Polygon area and centroid calculation" and "Clipping a line with a polygon", which are both linked from this page.

Even though the code examples are mostly in C, this is a terrific source for working with the Java geometry classes.

Thank you both for the great help!

You can also try using Java built in functions. This works for intersecting line with polygon or other shape as well as for finding polygon intersection, or in other words intersecting polygons.

Area area1= new Area(line2d_polygon_or_some_other_shape);

Area area2= new Area(ine2d_polygon_or_some_other_shape);

Area intersection = new Area();

intersection.add(area1);

intersection.intersect(area2);

//here intersection is the intersected area

Be carefull with Area, their performances are very poor.

JÃ©rÃ´me ThiÃ¨vre

java2d@JAVADESKTOP.ORG a Ã©crit :

> You can also try using Java built in functions. This works for intersecting line with polygon or other shape as well as for finding polygon intersection, or in other words intersecting polygons.

>

> Area area1= new Area(line2d_polygon_or_some_other_shape);

> Area area2= new Area(ine2d_polygon_or_some_other_shape);

>

> Area intersection = new Area();

> intersection.add(area1);

> intersection.intersect(area2);

> //here intersection is the intersected area

> [Message sent by forum member 'ginte' (ginte)]

>

> http://forums.java.net/jive/thread.jspa?messageID=261986

>

> ===========================================================================

> To unsubscribe, send email to listserv@java.sun.com and include in the body

> of the message "signoff JAVA2D-INTEREST". For general help, send email to

> listserv@java.sun.com and include in the body of the message "help".

>

>

===========================================================================

To unsubscribe, send email to listserv@java.sun.com and include in the body

of the message "signoff JAVA2D-INTEREST". For general help, send email to

listserv@java.sun.com and include in the body of the message "help".

[jthievre.vcf]

Hi,

These methods are not implemented in Java standard API.

I give you the line intersection routine :

public static Point2D lineIntersection(Line2D line1, Line2D line2)

{

Point2D cp = null;

double a1,b1,c1,a2,b2,c2,denom;

a1 = line1.getY2()-line1.getY1();

b1 = line1.getX1()-line1.getX2();

c1 = line1.getX2()*line1.getY1()-line1.getX1()*line1.getY2();

// a1x + b1y + c1 = 0 line1 eq

a2 = line2.getY2()-line2.getY1();

b2 = line2.getX1()-line2.getX2();

c2 = line2.getX2()*line2.getY1()-line2.getX1()*line2.getY2();

// a2x + b2y + c2 = 0 line2 eq

denom = a1*b2 - a2*b1;

if(denom != 0)

cp = new Point2D.Double((b1*c2 - b2*c1)/denom,(a2*c1 -

a1*c2)/denom);

else

{

// lines are parallel

}

return cp;

}

You should read the state of the art books on Computational Geomatry to

find this kind of algorithms.

JÃ©rÃ´me ThiÃ¨vre

java2d@JAVADESKTOP.ORG a Ã©crit :

> Hi all,

>

> It seems that the intersct() of Line2D will only return a boolean value. I'm looking for some sample code to calculate the intersection of a line and a polygon (or 2 lines) and return the point. In addition, I need to calculate the area of a regular polygon. I guess the the source files of Line2D.intersect() and Graphics2D.fill() will help a lot. I'll look into those files later in the afternoon. But I hope I can get some suggestions from you. Thank you in advance.

> [Message sent by forum member 'lcmeng' (lcmeng)]

>

> http://forums.java.net/jive/thread.jspa?messageID=254428

>

> ===========================================================================

> To unsubscribe, send email to listserv@java.sun.com and include in the body

> of the message "signoff JAVA2D-INTEREST". For general help, send email to

> listserv@java.sun.com and include in the body of the message "help".

>

>

===========================================================================

To unsubscribe, send email to listserv@java.sun.com and include in the body

of the message "signoff JAVA2D-INTEREST". For general help, send email to

listserv@java.sun.com and include in the body of the message "help".

[jthievre.vcf]

JÃ©rÃ´me ThiÃ¨vre wrote:

>

> These methods are not implemented in Java standard API.

>

I've had very similar problem with intersection of two polygons and just

recently found out that Java has built in procedures for that. I wanted to

share with you.

Area area1= new Area(line2d_or_polygon);

Area area2= new Area(line2d_or_polygon);

Area intersection = new Area();

intersection.add(area2);

intersection.intersect(area1);

//here intersection is the intersected area

--

View this message in context: http://www.nabble.com/Intersection-of-Line-and-Polygon-and-the-Area-of-P...

Sent from the Sun - Java2D-Interest mailing list archive at Nabble.com.

===========================================================================

To unsubscribe, send email to listserv@java.sun.com and include in the body

of the message "signoff JAVA2D-INTEREST". For general help, send email to

listserv@java.sun.com and include in the body of the message "help".

this wouldn't work for lines. an Area defined by a line is empty.