9

I want to build complex shapes as the intersection of two circles and a rectangle. After researching a bit, java.awt.geom.Area class seems perfect for this task.

I was dismayed, however, when I discovered that the awt package doesn't come with the android SDK. Does anyone know of any alternatives for android that allows me to create complex shapes by defining the union and intersection of simpler shapes?

Note: Using graphics clipping to draw the shape doesn't work because I don't just want to draw the shapes, I also want to store the shapes in memory to do collision detection and other interactions.

Razor Storm
  • 12,167
  • 20
  • 88
  • 148

2 Answers2

3

Android Alternatives to java.awt.geom.Area

EDIT: @numan pointed out an excellent option using some classes in the Android SDK that I was unaware of at the time of the original answer:

https://developer.android.com/reference/android/graphics/Region.html https://developer.android.com/reference/android/graphics/Region.Op.html

Region allows you to define geometric areas, and then you can use Regions op() method with Region.Op enum to calculate intersections and more complex shapes.

Some other options

You can use a Canvas to draw custom shapes, particularly using the clip* methods:

http://developer.android.com/reference/android/graphics/Canvas.html

Here are some pages about 2d graphics in Android:

http://developer.android.com/guide/topics/graphics/2d-graphics.html http://developer.android.com/guide/topics/graphics/2d-graphics.html#shape-drawable http://developer.android.com/guide/topics/graphics/opengl.html

Some other good options if your graphics remain the same (or roughly the same) are XML-based:

http://developer.android.com/guide/topics/graphics/2d-graphics.html#drawables-from-xml

And one solution I find quite neat, is using 9-patch drawables:

http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

Collision detection It might be overkill for your purposes, but there are a number of game physics libraries:

http://www.andengine.org http://code.google.com/p/andengineexamples/

http://bulletphysics.org

http://www.emini.at/

http://www.dremsus.com/index.php/2012/01/box2d-game-demo-in-android/

Android, libgdx and box2d basics

Or you can roll your own solution:

http://cooers.blogspot.com/2012/08/simple-collision-detection-in-2d.html

http://content.gpwiki.org/index.php/Polygon_Collision

http://www.codeproject.com/Questions/244344/Collision-Detection-in-Android

Collision detection for rotated bitmaps on Android

It really depends on the purpose; for games, you'd probably be best to just use a library; but if collision detection is the only feature you need, you'd be better off doing it yourself to save resources.

Extra Credit: Some indexes of Android libraries

http://www.appbrain.com/stats/libraries/dev

http://www.theultimateandroidlibrary.com/

http://www.openintents.org/en/

Community
  • 1
  • 1
CodeShane
  • 6,480
  • 1
  • 18
  • 24
  • Thanks for your answer, but as I had written in my question I need to do more than simply drawing the complex shapes. The problem I face is that I need to do click detection on the complex shapes (ex: user clicked on point (256,321) is this within the shape or not?). – Razor Storm Nov 02 '12 at 04:48
  • Absolutely.. I was pointing out some good alternatives to the {awt} package, and neglected the last sentence! :) I'll amend my answer in a moment. – CodeShane Nov 02 '12 at 06:02
  • :) Thanks! Hahaha I didn't actually need any of the other functionalities of the awt package. It's just that the Area class which I like happened to be in the awt package. For the actual graphics I'm using `Canvas` and overwriting the `onDraw()` method of a `SurfaceView`. – Razor Storm Nov 02 '12 at 06:34
  • 1
    Blah, my "answer" looks more like a spam-filled website lol :/ If none of those links holds the key, I'm sure someone will jump in soon, America may be winding down, but it's almost morning in Europe. :D – CodeShane Nov 02 '12 at 06:47
  • Ahh - these answers dont seem to have something equivilent to AWTs Area.add - to construct complex polygons. – Chozabu Feb 22 '13 at 16:35
  • *for purposes other than drawing - such as click detection or collision – Chozabu Mar 16 '13 at 22:08
  • I'm sorry the four links relating to collision detection didn't help you, but it would be a tad friendlier to ask your own question instead of down-voting the 4-month-old accepted answer to someone else's question (that's just hurtful lol.) – CodeShane Mar 17 '13 at 07:15
  • 1
    @Chozabu you can do something like that with Region and Region.Op in the android sdk. – numan salati Jul 25 '13 at 16:24
  • @CodeShane Right you are! This is the correct answer to the question. Downvote is now an upvote! and adding numans info makes it also the answer I was after ;) – Chozabu Sep 14 '13 at 12:34
  • Can `Region` handles circles/ellipses? It seems to only handle rectangles. – Ryan R Mar 02 '15 at 20:42
2

Android UI tooklit uses Skia for graphics rendering and skia uses the Region abstractions for set operations on shapes (e.g intersection, union, subtract. see Region.Op class) shapes from Paths or Rects.

Region class will also get your far for simple collision detection with Region.quickContains or Region.quickReject methods.

numan salati
  • 19,394
  • 9
  • 63
  • 66