1

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.

StoneAgeCoder
  • 923
  • 1
  • 7
  • 13
  • 1
    The "constructive solid geometry" functionality is solely added by the `Area` class. You should say more clearly *where* you noticed a "performance jump". But note that you can, in any case, perform the operations that you want with `Area` objects, and afterwards convert an area into a `Path2D`, by calling `Path2D p = new Path2D.Double(); p.append(area.getPathIterator(null,1.0f));` – Marco13 May 29 '14 at 22:27
  • @Marco13 Thanks to your last answers I looked more into tiling for my game and the change from texturepaint to using images was enormous. I have also gone ahead and roughly tiled each shape according to its image type I will edit my post to include an image demonstrating my methods. – StoneAgeCoder May 29 '14 at 23:22
  • @Marco13 Its a little rough ;) but I had to use paint because I have to redownload GIMP. – StoneAgeCoder May 29 '14 at 23:47
  • It didn't become clearer with these images. Maybe you should describe in more detail *where* and *when* you want to perform the intersections and paint the shapes. – Marco13 May 30 '14 at 08:37
  • @Marco13 I initially was performing the intersections before loading up the game, but the load time was too long so I have switched to a regional render. Each red tile you see intersects a portion of a map sized Area where I call Area.intersect() for each individual type of image. I will repost a different image to demonstrate the intersection for each individual shape. – StoneAgeCoder May 30 '14 at 18:27
  • Sorry, I still did not understand *what* you are intersecting there (and *why*) and *what* you are painting after all. Maybe I'll read it again a few times and finally understand it ;-) but maybe someone else already has an idea... – Marco13 May 30 '14 at 19:37

1 Answers1

1

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.

Gilthans
  • 1,656
  • 1
  • 18
  • 23
  • Is there a java implementation of one of these algorithms available on the web because I searched and cannot find one? Also I am intersecting it a large irregular Path2D object with a simple square if that makes it any easier :). – StoneAgeCoder May 29 '14 at 23:18
  • I've updated the answer with some links - I'm afraid I couldn't find a straight-forward implementation and don't have one on the fly... – Gilthans May 30 '14 at 13:03