I'm trying to make a rotating ellipse to the center, and I'm having trouble as to how it isn't moving besides being in the draw and having an offset in x and y changing. I mapped the initial x and y position of the ellipses at first and try and calculated its distance from the center I was wondering if any of you could help, it'll be much appreciated. ^^ (beginner here) [IDE processing]
I dunno what's wrong with the revolution from polar to cartesian coord.
Heart heart = new Heart();
int h = 500;
int w = 500;
void setup(){
size(500,500);
heart = new Heart();
heart.init();
//heart.display();
}
void draw(){
background(0);
heart.rotate();
}
class Heart {
float[][] pos;
float[] dist;
int hold;
float xOff;
float yOff;
Heart(){
hold = 10;
dist = new float[hold];
pos = new float[hold][hold];
}
void init(){
for(int i = 0; i < hold; i++){
for(int j = 0; j < hold; j++){
pos[i][j] = random(0,h);
}
}
}
/*void display(){
for (int k = 0; k < hold; k++){
fill(0,10,255,50);
ellipse(pos[k][0],pos[0][k],15,15);
stroke(255);
line(w/2,h/2,pos[k][0],pos[0][k]);
}
}*/
void rotate(){
float[] r = new float[hold];
int theta;
for(int k = 0; k < hold; k++){
r[k] = dist(w/2,h/2,pos[k][0],pos[0][k]);
for(theta = 0; theta <= TWO_PI; theta++){
xOff = r[k] * cos(theta);
yOff = r[k] * sin(theta);
stroke(255);
println(pos[k][0] + xOff);
ellipse(pos[k][0] + xOff,pos[0][k] + yOff,15,15);
}
}
}
}
I expect the ellipses to revolve, no error.
Use a global variable angle
of type float
. Increment the angle in every frame and pass it to the method Heart.rotate()
:
float angle = 0.0;
void draw(){
background(0);
heart.rotate(angle);
angle += 0.01;
}
The method Heart.rotate()
hast to draw the the ellipse
with one certain angle i every frame, rather than all the possible angles in a loop in each frame.
class Heart {
// ...
void rotate(float theta){
float[] r = new float[hold];
for(int k = 0; k < hold; k++){
r[k] = dist(w/2,h/2,pos[k][0],pos[0][k]);
xOff = r[k] * cos(theta);
yOff = r[k] * sin(theta);
stroke(255);
println(pos[k][0] + xOff);
ellipse(pos[k][0] + xOff,pos[0][k] + yOff,15,15);
}
}
}
Note, the display is updated once after global draw()
has been executed. There is no update of the display in the inner loop of the method Heart.rotate()
.