Java 沿着螺旋线移动到中心,起点给出cartasian坐标

Java 沿着螺旋线移动到中心,起点给出cartasian坐标,java,processing,polar-coordinates,spiral,Java,Processing,Polar Coordinates,Spiral,我想将椭圆沿着螺旋线移动到屏幕的中心-椭圆的起点通过x和y坐标给出。我不知道如何将螺旋线向内推到中心(我可以通过在x,y上加上宽度/2和高度/2来实现),同时在给定的cartasian(x,y)坐标下开始运动。下面是代码。谢谢你的帮助! 我一直试图在processing(java)中做到这一点: 可以平移x、y,然后平移绘制循环 这使得您使用的坐标系是屏幕中心的0,0,而不是屏幕左上角的0,0 float x = 100; // x of the object float y = 100; //

我想将椭圆沿着螺旋线移动到屏幕的中心-椭圆的起点通过x和y坐标给出。我不知道如何将螺旋线向内推到中心(我可以通过在x,y上加上宽度/2和高度/2来实现),同时在给定的cartasian(x,y)坐标下开始运动。下面是代码。谢谢你的帮助! 我一直试图在processing(java)中做到这一点:


可以平移x、y,然后平移绘制循环

这使得您使用的坐标系是屏幕中心的0,0,而不是屏幕左上角的0,0

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
  }
}
距离和角度公式使用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
  }

}

只是试着运行建议的代码,当改变起始点时,看起来是一团糟——假设x=100,y=100,标记点有不同的位置。当检查象限时,它可能会起作用-我将尝试测试它。好的,所以我要做的是在绘制椭圆时添加条件,以检查第一个给定的x是否大于画布的一半。仍然想知道为什么我不能使用x来检查它,并且必须设置第二个全局值,我喜欢float I=x;。。。。代码:if(I更新以修复此错误。我忘记了如果您被留下(否定)对于原点,您正在使用的角度方法将翻转角度。更新演示了一种方法,可以检查此情况,并在代码中的适当点翻转,而无需设置全局。添加了代码,以显示一种更好的方法,可以修改初始半径和角度,使其相对于屏幕中心,而无需从屏幕上移动原点左上角。
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
  }
}