Polygon merging or substracting

BlitzMax Forums/BlitzMax Programming/Polygon merging or substracting

Space_guy(Posted 2011) [#1]
example link:
http://www.cs.man.ac.uk/~toby/alan/software/

Is there any module or tips on how to merge two polygon shapes together.

Thankfull for any tips!


beanage(Posted 2011) [#2]
This is awefully complicated, and there wont be a trivial way to do this (unless you feel more like a genius than me :).. couldnt you port some parts of the GPC?


Space_guy(Posted 2011) [#3]
Yes it is hard. Ive been thinking about this many times the last few years but i always gave up on it. Im not experienced in porting code either. :(

But perhaps someone has something?


Oddball(Posted 2011) [#4]
Here's something to get you started

It's not perfect but it's a start.


Oddball(Posted 2011) [#5]
How about this one?

It's a little better. It can handle multiple line crosses along a single line.

Last edited 2011


Oddball(Posted 2011) [#6]
Ok, now it has a limited form of polygon subtraction

Still far from perfect, but not bad for 20 minutes coding. I'm sure you can take it from here and customise it to your needs.

Have fun!

Last edited 2011


Oddball(Posted 2011) [#7]
I should add that the points in the polygons should run clockwise for this to work correctly.


Warpy(Posted 2011) [#8]
To detect if the points in a triangle are clockwise:
Function makeClockwise(tri#[])
   Local cp# = (tri[2]-tri[0])*(tri[5]-tri[1]) - (tri[4]-tri[0])*(tri[3]-tri[1])
   If cp > 0
     Local x# = tri[2], y# = tri[3]
     tri[2] = tri[4]
     tri[4] = x
     tri[3] = tri[5]
     tri[5] = y
   EndIf
End Function


I'm sure I had a page bookmarked which explained the algorithm for polygon operations, but I can't find it. This livejournal post seems to give a brief summary of the process.


Oddball(Posted 2011) [#9]
Do you have a version of that for polygons of any number of points? It would help make the function more robust. Anyway there is a serious bug in my last example so here it is all fixed up

That's the last one I promise. I shouldn't really be messing with this stuff anyway, but it piqued my interest and I couldn't help myself.

Last edited 2011


Space_guy(Posted 2011) [#10]
:) This is awsome! Thanks alot!
I hope to have a play with this tonight


ImaginaryHuman(Posted 2011) [#11]
You could do it in realtime using the stencil buffer in opengl.


Warpy(Posted 2011) [#12]
Oddball: just look at the first three points. Assuming the polygon doesn't self-intersect, if the first three points are clockwise the whole polygon has to be. If the polygon does self-intersect, being clockwise or anticlockwise has no meaning.


Oddball(Posted 2011) [#13]
D'oh! Good call Warpy. Makes perfect sense, if only I'd taken a moment to think about it.


Warpy(Posted 2011) [#14]
I was wrong: if you choose a concave bit it won't work.


Oddball(Posted 2011) [#15]
That's nothing a quick tweak won't fix.

Now works with polygons running in either direction. Far from perfect, but still pretty good I'd say.


Space_guy(Posted 2011) [#16]
I did not have the time i hoped for last night but in the time i had i must say it works very good! Thanks alot for sharing this Oddball!


Space_guy(Posted 2011) [#17]
As Im back into this polygon code i discovered a bug.
Whenever i try add or substract from the final line in the p1 polygon it just ignores it.

Any thougths?

Also im thinking if a polygon gets split in 2 parts by the second poly it would be very cool to be able to get both polygons :) Any ideas where I could start?

And thanks again for this code. Its almost perfect for my needs :)


Warpy(Posted 2011) [#18]
The algorithm on the livejournal blog I posted before is the only proper algorithm to do this. I wish I could find the much more detailed page I can remember seeing, but I can't find it.


Difference(Posted 2011) [#19]
This might be what he came up with : https://github.com/revarbat/TkCAD/blob/master/tksrc/lib/geometry.tcl ?

It does seem to be his github: https://github.com/revarbat

Last edited 2011


Space_guy(Posted 2011) [#20]
If you use these polys you will see what the bug I mentioned.
I tried to find the bug my self but nothing i change seems to fix this.


Global poly1:Float[]=[ 200.0,200.0, 200.0,450.0, 400.0,450.0, 400.0,200.0 ]
Global poly2:Float[]=[ 250.0,100.0, 300.0,250.0, 500.0,100.0 ]

And that livejournal is very interesting. I just wish i could this code though as it works good enough for me. Exept that bug ofcourse :)


Oddball(Posted 2011) [#21]
I'm just making this stuff up as I'm going along so I doubt it's the best way of doing it. It's just the problem piqued my interest and I couldn't rest until I'd given it a go.

The last line bug should be fixed now, but I'm sure the function is still far from totally bug free.

Last edited 2011


Space_guy(Posted 2011) [#22]
Thanks but when i move the polygon to the right of the box then it wont work again :(


Global poly1:Float[]=[ 200.0,200.0, 200.0,450.0, 400.0,450.0, 400.0,200.0 ]
Global poly2:Float[]=[ 450.0,240.0, 250.0,300.0, 450.0,380.0 ]


Oddball(Posted 2011) [#23]
Yep, I kinda messed that fix up. Try this one on for size.

Hopefully that should fix the issue without creating ay new ones... I hope.


GW(Posted 2011) [#24]
Awesome!


Space_guy(Posted 2011) [#25]
Seems to work perfectly :)

Thank you very much!


Space_guy(Posted 2011) [#26]
Thanks again for the code! Its really usefull!

However I noticed one more bug that I tried to fix but was not yet able to.
There seems to be a way for it to get stuck in a loop that ends with a crash.

What happens is atleast that countm counter keeps going up and up but it will never exit. This seems to be rare. But too frequent for me to ignore.


GW(Posted 2011) [#27]
This seems help illustrate the bug.

Global poly1:Float[]=[ 200.0,200.0, 200.0,450.0, 400.0,450.0, 400.0,200.0 ]
Global poly2:Float[]=[ 250.0-110,100.0, 300.0-110,250.0, 500.0-110,100.0 ]



Oddball(Posted 2011) [#28]
Those polys add and subtract flawlessly for me.


Last edited 2011