# Intersection of Line and Polygon and the Area of Polygon

7 replies [Last post]
lcmeng
Offline
Joined: 2007-11-26

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.

cichlasoma
Offline
Joined: 2008-06-09

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

rpollack
Offline
Joined: 2007-06-12

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.

lcmeng
Offline
Joined: 2007-11-26

Thank you both for the great help!

ginte
Offline
Joined: 2008-03-03

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.intersect(area2);
//here intersection is the intersected area

Be carefull with Area, their performances are very poor.

> 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.intersect(area2);
> //here intersection is the intersected area
> [Message sent by forum member 'ginte' (ginte)]
>
>
> ===========================================================================
> 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.

> 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)]
>
>
> ===========================================================================
> 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]

Truth

>
> 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();