We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › get bounding box
Page Index Toggle Pages: 1
get bounding box (Read 1030 times)
get bounding box
May 19th, 2008, 10:53pm
 
i'm trying to find a simple/efficient way to get the bounds of a drawn object.  obviously, since shapes aren't drawn as objects in processing but are just painted to the screen, this is not as simple a task as in, say, actionscript.

for example, if i draw a rect of width and height 100, place it at {x:200, y:200}, and rotate it PI/4, i'd like to know that its bounding box is {x:200-50*sqrt(2), y:200-50*sqrt(2), w:100*sqrt(2), y:100*sqrt(2)}.  it's simple enough to write out the trig to do this for a rect, but it would be nice to have this capability for more complex shapes too.

it seems like a problem that would have been solved already by now, though a forum search didn't get me anything.  is this already implemented in some way in processing (like via some sort of shape-drawing container class)?  or has anyone written a library to do it?

thanks!
Re: get bounding box
Reply #1 - May 20th, 2008, 1:50am
 
There is always the java polygon class. It has a contains() boolean that you can use to detect intersections. It can be seen in the hack http://processing.org/hacks/doku.php?id=hacks:using-awt-s-polygon-class-to-test-for-insideness

Also you can use the screenX(), screenY() to get the co-ordinate of a vert after translation. You can use that to get a list of screen positions for each vert, then use the min() and max() to get the correct positions for the bounding box.

hope this helps

mattD
Re: get bounding box
Reply #2 - Jun 19th, 2008, 1:57am
 
yeah, that's better than i could have hoped for.  the Polygon class looks like it has lots of stuff i can use here, including Point intersection and bounding box.  thanks PM!
Re: get bounding box
Reply #3 - Jun 19th, 2008, 8:36pm
 
hm.  actually, the Polygon class is working out for math, but i want to try using it for drawing, too.  i found Graphics.drawPolygon, but can't figure out how to get at the Graphics object implemented by PApplet / PGraphics.

PApplet extends Component, which led me to believe i could call g.getGraphics(), but that method is somehow inaccessible.

any other ideas?  i could always iterate thru the Polygon with Polygon.getPathIterator...but that is kinda confusing me.

i could also just iterate thru the Polygon's xpoints/ypoints array, but that seems like the least elegant of all possible solutions.

thanks,
-e
Re: get bounding box
Reply #4 - Jun 19th, 2008, 8:51pm
 
whoops, sorry, PApplet.g is a PGraphics object.  i should just call getGraphics directly on the PApplet.  got it.

tho calling drawPolygon isn't actually doing anything.  maybe because i'm drawing outside of the Processing engine, and maybe that's just not a good idea.

ok, never mind...
Re: get bounding box
Reply #5 - Jun 19th, 2008, 11:04pm
 
ok um, yeah. As you were.
(I'm confused)
Page Index Toggle Pages: 1