沿着螺旋向中心移动,开始时给出卡塔西亚坐标



我想沿着螺旋线将椭圆移动到屏幕中心 - 椭圆的起点通过 x 和 y 坐标给出。好吧,我不知道如何将螺旋向内推到中心(我可以通过向 x,y 添加宽度/2 和高度/2 来实现这一点),同时在给定的卡塔西亚 (x,y) 坐标处开始运动。下面是代码。感谢帮助!我一直在尝试在处理(java)中执行此操作:

float r = 0; //radius
float x = 700; // x of the object
float y = 50; // y of the object
float theta = 0; //angle
void setup() {
  size(800,600);
  background(0);
  smooth();
  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() {
  fill(0,255,0);
  rect(width/2,height/2,20,20);
  fill(0,0,255);
  rect(700,50,20,20);
  float x = r * cos(theta);
  float y = r * sin(theta);
  // Draw an ellipse at x,y
  noStroke();
  fill(255,0,0);
  ellipse(x, y, 16, 16); 
  if (r>0){
  r -= 1; // Increment the radius
  theta += 0.01; // Increment the angle
  }
}

你可以平移你的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
  }
}

最新更新