我正在尝试制作一个到中心的旋转椭圆,除了在绘图中并且 x 和 y 的偏移量发生变化之外,我遇到了关于它如何不移动的问题。我首先绘制了椭圆的初始 x 和 y 位置,并尝试计算它与中心的距离 我想知道你们中的任何人是否可以提供帮助,我们将不胜感激。 ^^(这里是初学者)[IDE处理]
我不知道从极坐标到笛卡尔坐标的革命有什么问题。
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);
}
}
}
}
我希望椭圆旋转,没有错误。
最佳答案
使用 float
类型的全局变量 angle
。增加每一帧的角度并将其传递给方法 Heart.rotate()
:
float angle = 0.0;
void draw(){
background(0);
heart.rotate(angle);
angle += 0.01;
}
Heart.rotate()
方法必须在每一帧中绘制一个特定角度的椭圆
,而不是在每一帧中循环绘制所有可能的角度.
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);
}
}
}
注意,显示在全局 draw()
之后更新一次已被执行。 Heart.rotate()
方法的内循环中没有更新显示。
关于java - 为什么它不会旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57545617/