沿自己的中心点旋转的椭圆阵列

Array of ellipses rotating along own center point

这是原文的延续 post:

我正在尝试创建一个椭圆网格,它们都沿着自己的旋转中心旋转。然后,我试图通过一个中心点缩放 "square grid" 个椭圆,同时仍允许它们沿各自的中心点旋转。

ArrayList<RotatingEllipse> ellipses = new ArrayList<RotatingEllipse>();


void setup() {
  size(500, 500);
  noStroke();
  smooth();

  ellipses.add(new RotatingEllipse(width*.25, height*.25));
  ellipses.add(new RotatingEllipse(width*.75, height*.75));

  ellipses.add(new RotatingEllipse(width*.75, height*.25));
  ellipses.add(new RotatingEllipse(width*.25, height*.75));

  ellipses.add(new RotatingEllipse(width/2*.25, height/2*.25));
  ellipses.add(new RotatingEllipse(width/2*.75, height/2*.75));

  ellipses.add(new RotatingEllipse(width/2*.75, height/2*.25));
  ellipses.add(new RotatingEllipse(width/2*.25, height/2*.75));
}

void draw() {
  background(#202020);

  for (RotatingEllipse e : ellipses) {
    e.stepAndDraw();
  }
}


class RotatingEllipse {

  float rotateAroundX;
  float rotateAroundY;
  float distanceFromRotatingPoint;
  float angle;

  public RotatingEllipse(float startX, float startY) {

    rotateAroundX = (width/2 + startX)/2;
    rotateAroundY = (height/2 + startY)/2;


    distanceFromRotatingPoint = dist(startX, startY, rotateAroundX,    rotateAroundY);

    angle = atan2(startY-height/2, startX-width/2);
  }

  public void stepAndDraw() {


    angle += PI/128;


    float x = rotateAroundX + cos(angle)*distanceFromRotatingPoint;
    float y = rotateAroundY + sin(angle)*distanceFromRotatingPoint;



    float distance = dist(x, y, width/2, height/2); 

// size of ellipses  
    float diameter = 50*(200-distance)/500;
   ellipse(x, y, diameter, diameter);
  }
}

我认为你的问题分为两部分。

第 1 部分:为什么您的积分显示在它们所在的位置?

你能做的最好的事情就是拿出一张纸和一支铅笔画出一些点。你的每一个点会出现在哪里?

处理是将您的点准确地放在您告诉它放置的位置。这是一个例子:

ellipses.add(new RotatingEllipse(width/2*.25, height/2*.75));

您希望它出现在哪里?如果你的宽高都是500,那么width/2*.25就是62.5height/2*.75就是187.5。换句话说,这将位于屏幕左上象限的某个位置。

对你的每一个省略号进行计算,你就会明白为什么它们会出现在它们所在的位置。

第 2 部分:如何计算两点之间的中点?

与其说这是一个编程问题,不如说这是一个数学问题,用谷歌搜索 "midpoint between two points" returns 之类的东西会得到很多结果。

但基本上,如果你在 x1,y1x2,y2 处有两个点,那么你可以通过平均它们的 xy 位置来找到它们的中点:

midpoint = (x1+x2)2, (y1+y2)/2

我建议你通过第一部分找出你的点应该在哪里,然后使用第二部分找到这些点之间的中点。