你可以翻译你的 x,y 然后翻译绘图循环。
这样一来,您就可以使用位于屏幕中心而不是屏幕左上角的 0,0 坐标系。
您的距离和角度公式使用 0,0 作为原点,如果您使用左上角原点系统(如处理),则它不是屏幕的中心。您也可以改为修改这些公式。
下面的代码是您的编辑,在屏幕中央制作 0,0。
float r = 0; //radius
float x = 100; // x of the object
float y = 100; // y of the object
float theta = 0; //angle
void setup() {
size(800,600);
background(0);
smooth();
ellipseMode(CENTER); // draw ellipses based on their center
// translate the x and y from top-left origin to center origin
x -= width/2;
y -= height/2;
r = sqrt(x*x+y*y); //polar coordinate - distance from x,y to 0,0 on the grid
theta = atan(y/x); // angle to 0,0 on the grid
}
void draw() {
translate(width/2, height/2); // translate the whole canvas
fill(0,255,0);
ellipse(0,0,20,20); // mark the center of the canvas
fill(0,0,255);
ellipse(x,y,20,20); // mark the start point for the circle
// flip the angle if left of the origin
int flip = 1;
if(x < 0) flip = -1;
float x = r * cos(theta) * flip;
float y = r * sin(theta) * flip;
// Draw an ellipse at x,y
noStroke();
fill(255,0,0);
ellipse(x, y, 16, 16);
if (r>0){
r -= 1; // Decrement the radius
theta += 0.01; // Increment the angle
}
}
下面的代码保留了左上角的原点,但将半径和角度的初始化修改为相对于屏幕中心。
float x = 100; // x of the object
float y = 100; // y of the object
float r = 0; // radius
float theta = 0; // angle
void setup() {
size(800,600);
background(0);
smooth();
ellipseMode(CENTER); // draw ellipses based on their center
// distance between point and center of screen
r = dist(x,y,width/2,height/2);
// http://stackoverflow.com/a/7586218/1736092
// angle of line between point and center of screen
// relative to x-axis
float dX = x - width/2;
float dY = y - height/2;
theta = atan2(dY, dX);
stroke(0, 255, 0); // green stroke
line(x, y, width/2, height/2); // draw radius
noStroke();
fill(0, 255, 0); // green fill
ellipse(width/2, height/2, 20, 20); // mark the center of the canvas
fill(0, 0, 255); // blue fill
ellipse(x, y, 20, 20); // mark the start point for the circle
}
void draw() {
float x = r * cos(theta) + width/2;
float y = r * sin(theta) + height/2;
noStroke();
fill(255,0,0); // red fill
ellipse(x, y, 16, 16);
if(r>0) {
r -= 1; // Decrement the radius
theta += 0.01; // Increment the angle
}
}