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 & HelpSyntax Questions › object boundaries
Page Index Toggle Pages: 1
object boundaries (Read 742 times)
object boundaries
Dec 8th, 2009, 7:15pm
 
Hi,

I just try things and was wondering if there is a way I could easily define object boundaries. As an example I'd create a square at a random position, say 10 by 10, and a noise of dots all over the canvas. Now I don't want the noise to be touching or inside the square but rather start in a specific distance. I could create another square with just a stroke applied for this simple example but that's not what I mean.

Any suggestions?
Re: object boundaries
Reply #1 - Dec 9th, 2009, 1:23am
 
What defines the boundary of a square?  not so difficult to define and test for in a set of conditions...  So when you place your dots check that they're not within the bounds of the square.  If they are place them somewhere else...
Re: object boundaries
Reply #2 - Dec 9th, 2009, 5:16am
 
Well, thank you. But how do I check for it?
Re: object boundaries
Reply #3 - Dec 9th, 2009, 6:31am
 
Well it's pretty easy which is why I didn't bother posting any code.

First things first: check how you're drawing the square (see  rectMode)

Now check if the point is inside the square:

Code:
// assuming rectMode(CORNER)
if (dot.x > square.x && dot.x < square.x + square.width && dot.y > square.y && dot.y < square.y + square.height) {
 // dot is inside the square
}


Note the && (AND) means you combine all the conditions in one test:  all have to be true for the condition to return true.

So, calculate new x,y coords for your point.  If they're inside the square ditch them and try again.  You could use a do while loop to achieve this, though that might be dangerous if the square is particularly large in relation to the size of your sketch (i.e. it will have to continue until it finds a safe point which could take some time).
Re: object boundaries
Reply #4 - Dec 9th, 2009, 6:49am
 
Oh, I did not respect the coordinate system.. Thanks!
Page Index Toggle Pages: 1