# How to merge two polygons into one

Hi,

Can someone tell me how to merge two polygons into one when they have overlap using Java?

In java.awt.Polygon :
Polygon(int[] xPoints, int[] yPoints, int nPoints)
I saw it has addpoint method as:   void       addPoint(int x, int y)
but don't see merge method.

LVL 2
###### Who is Participating?

Commented:
Use the Line class, and it's intersectsLine() method to determine if two lines intersect.
0

Commented:
there is no merge method that i'm aware of you'll need to implement it yourself.
0

Commented:
So how would you merge polygons of different types (if that's what you are saying), such as an acute triangle and a large square? Would you open up the angles of the triangle to meet the corners of the square? Or would you ahlve each object, scale them both up to the same relations, and then join them?
0

Commented:
>> ahlve = halve.
0

Author Commented:
To keep question simple, to merge two rectangle see below: (a) -> (b)

----------
|            |
|      -----|-----
|       |       |       |
|___|___|___|
(a)

----------
|            |
|              |-----
|                        |
|__________|
(b)

I think the key is how to find the cross points for two rectangles, then build a new polygon. The same as I need to find all cross points for two polygons, and merge to one.

thanks.
0

Commented:
I dont think I can be of much more help, since your diagrams are all to pot, but if the postion of the amalgamated polygon is not important, and the size neither is, then why not just draw a completely new one somewhere on the screen, rather than do all the calculations involved in this? If they are not the same proportions, I dont see how they can be joined together anyway, it doesnt make any sense.
0

Commented:
you need to locate the polygon line segments that intersect, and create your new polygon based on those intercepts.
0

Author Commented:
Hi Object,

>> you need to locate the polygon line segments that intersect, and create your new polygon based on those intercepts.

Can you give more explaination  based on coding perspective? It will be great if you can show as example. Since I still  can't get to how "to locate the polygon line segments that intersect"

thanks!
0

Author Commented:
I don't think it is an accepted answer.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.