Path2D.intersect() Similar to Area.intersect()

712 views Asked by At

I have played around with Area.intersect() and was wondering if there is a way to create a method much like this one using Path2D because I noticed a performance jump when using Path2D as a shape. In other words take a portion of a large Path2D and create a smaller Path2D from that portion.

Map Drawing MapDraw In-Game View In-Game

Note: Using the below hashmap a I render tiled shapes to the viewing area according to each "Object" which in this case would be the different image types : Ocean, Grass, Obsidian, Rock, Sand, & Dirt...

LinkedHashMap<Point, LinkedHashMap<Object, Path2D.Double>>

EDIT : Each image type has an entire map area of its own that is 10000px by 100000px my tiles that intersect are 100px by 100px which are shoved into the linked hash map by point as its given type as a Path2D.Double and rendered onto the screen by the points in the current viewing area.

1

There are 1 answers

2
Gilthans On

It's not clear what SDK you're working with which offers Area.intersect(). Depanding on what you intend to intersect your path with, however, it may be a complex problem - notice that a path2D intersected with a polygon may turn into several paths!

However, there are some known algorithm for intersecting a path with a polygon, such as Cyrus-Beck or Sutherland-Cohen.

I found this piece of code for Cohen-Sutherland in Java:

http://worldofenggcodes.blogspot.co.il/2013/10/cohen-sutherland-line-clipping-algorithm.html

Which seems OK, although you might need to extract the code into a more usable function. Cyrus-Beck would probably be a better option, though I could only find pseudo-code:

http://www.moreprocess.com/computer-graphics/cyrus-beck-line-clipping-algorithm

Once you've implemented either, you need to apply it to every line in your path, to get a new list of lines which intersect with the square.