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 › Working with void draw..
Page Index Toggle Pages: 1
Working with void draw.. (Read 1042 times)
Working with void draw..
Feb 2nd, 2010, 1:32pm
 
I'm new to processing and working on this for a class assignment. Basically what I am trying to do is make a graffiti spray paint type effect. I got it nearly exactly how I want it only I can't figure out how to get my spray effect to take a circular rather than square form. Any help with what code to edit/add would be most appreciated.

The current code is:

Quote:
void setup() {

size(640,480);
 smooth();
 noStroke();
 fill(0);
 // noLoop();
}

void draw() {
if (mousePressed == true) { //makes the function activated by mouse.
 stroke(mouseY,mouseX,mouseY,mouseX);
for(int i=0;i<mouseX;i++) //makes it thicker at left corner of where mouse is pressed.
for(int j=0;j<mouseY;j++) //more dispersed farther from corner you press the mouse.
point(mouseX + random(i),mouseY + random(j));}} //make splatter effect start where mouse clicks.


Re: Working with void draw..
Reply #1 - Feb 2nd, 2010, 2:31pm
 
the idea with a random scattering in a circle is that you're picking a random point that is a certain distance from the midpoint (or less).  You can use sin() and cos() to find a point that is a random angle and random distance from the midpoint, or you can fudge it by using your random-in-a-square method but discarding the results until the distance requirement is satisfied.
Re: Working with void draw..
Reply #2 - Feb 9th, 2010, 5:10am
 
First generate a random x value.
Once you have a given x define a radius.
With these values you can determine theta(arccos(xVariable/radius)).
With that you can generate a random y value within the circle at that given x value(+or- radius*sin(theta)).

Here is my finished code:
Code:

void setup() {

size(640,480);
smooth();
noStroke();
fill(0);
// noLoop();
}

void draw() {
 if (mousePressed == true) { //makes the function activated by mouse.
   stroke(mouseY,mouseX,mouseY,mouseX);
   for(int i=0;i<mouseX;i++){ //makes it thicker at left corner of where mouse is pressed.
     for(int j=0;j<mouseY;j++){ //more dispersed farther from corner you press the mouse.
       
       float xVariable = random(-i*10, i*10);
       float radius = 20;
       float theta = acos(xVariable/radius);
       float yVariable = random(-radius*sin(theta), radius*sin(theta));
       point(mouseX + xVariable,mouseY + yVariable);
//make splatter effect start where mouse clicks.
     }
   }
 }
}
Page Index Toggle Pages: 1