Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
torita
torita's Profile
2
Posts
2
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Coloring Circles question
[1 Reply]
23-Oct-2012 03:47 PM
Forum:
Programming Questions
Hi, I am working on a sketch with moving circles.
I want this circles to appear when I click on the screen with a random color.
Now the random color is assign random each time that Processing draw a new frame.
so I have the circles that change color every frame, (line 23 & 24 of the Code)
any Idea on how to have a random color assigned to the circle at the first frame and no change through the animation?
Thanks
ArrayList circs;
void setup()
{
size(400, 400);
background (0);
fill(255);
smooth();
circs = new ArrayList();
}
void draw()
{
background(0);
UpdateMovement();
AdjustForBoundaryCollision();
AdjustForObjectCollision();
for (int i=0; i<circs.size(); i++)
{
color a = color(random(255),random(255),random(255));
fill(a);
Circle c = (Circle)circs.get(i);
c.draw();
}
}
void AdjustForBoundaryCollision()
{
for (int i =0; i<circs.size(); i++)
{
Circle c = (Circle)circs.get(i);
if (c.p.x+(c.r/2) > width ||c.p.x-(c.r/2)<0)
{
c.v.x *=-1;
}
if (c.p.y+(c.r/2) > height ||c.p.y-(c.r/2)<0)
{
c.v.y *=-1;
}
}
}
void UpdateMovement()
{
for (int i=0; i<circs.size(); i++)
{
Circle c = (Circle)circs.get(i);
c.UpdateMovement();
}
}
void AdjustForObjectCollision()
{
for (int i=0; i<circs.size(); i++)
{
Circle c =(Circle)circs.get(i);
for (int j=i+1; j<circs.size(); j++)
{
Circle c2 = (Circle)circs.get(j);
PVector cVect = new PVector(0, 0);
cVect.add(c.p);
cVect.sub(c2.p);
if (cVect.mag()<c.r+c2.r)
{
c.v.add(cVect);
c2.v.sub(cVect);
}
}
}
}
void mousePressed()
{
Circle c = new Circle(mouseX, mouseY);
circs.add(c);
}
void keyPressed()
{
save ("CircleCollection.png");
}
Second .pde file to be placed in the same folder of the first one.
class Circle
{
PVector p;
PVector v;
float damper = random(0.9,.9999999999999999999);
float r;
//constructor
Circle(float tx, float ty, float tr)
{
p = new PVector(tx, ty);
v = new PVector(random(-1,1), random(-1,1));
r = tr;
}
Circle(float tx, float ty)
{
p = new PVector(tx, ty);
v = new PVector(random(-1,1), random(-1,1));
r = random(50);
}
void draw()
{
ellipse (p.x, p.y, r, r);
}
void UpdateMovement()
{
//p.x +=v.x;
// p.y +=v.y;
p.add(v);
v.mult(damper);
}
}
Help for simple cellular automa
[5 Replies]
13-Sep-2012 08:30 PM
Forum:
Programming Questions
Hi everybody,
I am pretty new to Processing and I would really appreciate any help on this script I am working on to create a cellular Automa.Thank you.
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
int gen = 0;
int[] oldGen;
int[] newGen;
int genWidth;
int prevCA;
int currCA;
int nextCA;
int[] Rules;
void setup() {
size(600, 200);
background(0);
noStroke ();
genWidth = width;
oldGen = new int[genWidth];
newGen = new int[genWidth];
rules = new int[8];
rules [0] =1;
rules [1] =1;
rules [2] =1;
rules [3] =1;
rules [4] =1;
rules [5] =1;
rules [6] =1;
rules [7] =1;
}
void Draw()
{
if (gen==0)
for (int i=0; i
{
oldGen[i] = int(0.5+random(1));
newGen[i] = int(0.5+random(1));
}
}
else
{
//calculate new generation
for (int i=0; i
{
prevCA = oldGen[(i-1+genWidth)%genWidth];
currCA = oldGen[i];
nextCA = oldGen[(i+1)%genWidth];
}
if (currCA ==1)
{
if (nextCA ==1)
{
if (prevCA==1)
{
newGen[i]=rules [0];
}
else
{
newGen[i]=rules [1];
}
}
else
{
if (prevCA==1)
{
newGen[i]=rules [2];
}
else
{
newGen[i]=rules [3];
}
}
}
else
{
if (nextCA ==1)
{
if (prevCA==1)
{
newGen[i]=rules [4];
}
else
{
newGen[i]=rules [5];
}
}
else
{
if (prevCA==1)
{
newGen[i]=rules [6];
}
else
{
newGen[i]=rules [7];
}
}
}
}
//draw new generation
for (int i=0; i
if (newGen[i]==1)
{
fill(255);
}
else
{
fill(0);
}
rect(i, gen, 3, 3);
//replace the old generation with the new generation
for (int i=0; i
{
oldGen[i]= newGen[i];
}
//increment the new generation count
gen++;
«Prev
Next »
Moderate user : torita
Forum