以连续的方式计算圆坐标坐标、方式

2023-09-12 21:18:07 作者:时间是最毒的毒药。

我需要有一个给定圆了一个又一个的所有点的坐标,这样我就可以使一个对象去的圈子跳跃,从一个点到下一个。我试过中点画圆算法,但我找到的版本是为了绘制和坐标不在​​顺序。它们同时产生8象限和在其顶部相对方向。如果至少它们是在同一方向,我可以使一个单独的阵列为每个象限和它们附加到彼此在末端。这是JavaScript改编code我现在有:

 函数calcCircle(centerCoordinates,半径){
    VAR coordinatesArray =新的Array();
    //转换坐标
    VAR X0 = centerCoordinates.left;
    VAR Y0 = centerCoordinates.top;
    //定义变量
    变种F = 1  - 半径;
    变种ddFx = 1;
    VAR ddFy = -radius<< 1;
    变种X = 0;
    变种Y =半径;
    coordinatesArray.push(新坐标(X0,Y0 +半径));
    coordinatesArray.push(新坐标(X0,Y0  - 半径));
    coordinatesArray.push(新坐标(X0 +​​半径,Y0));
    coordinatesArray.push(新坐标(X0  - 半径,Y0));
    //主回路
    而(X< Y){
      如果(f是氟烃基; = 0){
        y--;
        ddFy + = 2;
        F + = ddFy;
      }
      X ++;
      ddFx + = 2;
      F + = ddFx;
      coordinatesArray.push(新坐标(X0 +​​ X,Y 0 + Y));
      coordinatesArray.push(新坐标(X0  -  X,Y 0 + Y));
      coordinatesArray.push(新坐标(X0 +​​ X,Y0  -  Y));
      coordinatesArray.push(新坐标(X0  -  X,Y 0  -  Y));
      coordinatesArray.push(新坐标(X0 +​​ Y,Y 0 + X));
      coordinatesArray.push(新坐标(X0  -  Y,Y0 + X));
      coordinatesArray.push(新坐标(X0 +​​ Y,Y0  -  X));
      coordinatesArray.push(新坐标(X0  -  Y,Y0  -  X));
    }
    //返回结果
    返回coordinatesArray;
  }
 

我$ P PFER一些快速的算法不三角$,但任何帮助是AP preciated!

修改

卵形曲线坐标计算方法

这是最终的解决方案。谢谢大家!

 函数calcCircle(centerCoordinates,半径){
    VAR coordinatesArray =新的Array();
    VAR octantArrays =
      {OCT1:新的Array(),OCT2:新的Array(),OCT3:新的Array(),OCT4:新的Array()
       oct5:新的Array(),OCT6:新的Array(),oct7:新的Array(),oct8:新的Array()};
    //转换坐标
    VAR XP = centerCoordinates.left;
    VAR YP = centerCoordinates.top;
    //定义添加坐标阵列
    VAR setCrd =
      功能(targetArray,XC,YC){
        targetArray.push(新坐标(YC,XC));
      };
    //定义变量
    VAR XOFF = 0;
    VAR YOFF =半径;
    VAR余额= -radius;
    //主回路
    而(XOFF< = YOFF){
      //象限7  - 反转
      setCrd(octantArrays.oct7,XP + XOFF,YP + YOFF);
      //象限6  - 直
      setCrd(octantArrays.oct6,XP  -  XOFF,YP + YOFF);
      //象限3  - 反向
      setCrd(octantArrays.oct3,XP  -  XOFF,YP  -  YOFF);
      //象限2  - 直
      setCrd(octantArrays.oct2,XP + XOFF,YP  -  YOFF);
      //避免重复
      如果(XOFF!= YOFF){
        //象限8  - 直
        setCrd(octantArrays.oct8,XP + YOFF,YP + XOFF);
        //象限5  - 反向
        setCrd(octantArrays.oct5,XP  -  YOFF,YP + XOFF);
        //象限4  - 直
        setCrd(octantArrays.oct4,XP  -  YOFF,YP  -  XOFF);
        //象限1  - 反转
        setCrd(octantArrays.oct1,XP + YOFF,YP  -  XOFF);
      }
      //一些奇怪的东西
      余额+ = XOFF ++ + XOFF;
      如果(余额> = 0){
        平衡 -  = --yoff + YOFF;
      }
    }
    //反转逆时针八分阵列
    octantArrays.oct7.reverse();
    octantArrays.oct3.reverse();
    octantArrays.oct5.reverse();
    octantArrays.oct1.reverse();
    //删除逆时针八分数组最后一个元素(避免重复)
    octantArrays.oct7.pop();
    octantArrays.oct3.pop();
    octantArrays.oct5.pop();
    octantArrays.oct1.pop();
    //将所有阵列一起
    coordinatesArray =
      octantArrays.oct4.concat(octantArrays.oct3).concat(octantArrays.oct2).concat(octantArrays.oct1)。
        concat(octantArrays.oct8).concat(octantArrays.oct7).concat(octantArrays.oct6).concat(octantArrays.oct5);
    //返回结果
    返回coordinatesArray;
  }
 

解决方案

使用可以试试下面的办法:用你给的算法,但把你的坐标,​​以八个不同coordinateArrays。事后必须扭转其中一半(那些与(X0 +​​ X,Y0-y)时,(X0-X,Y 0 + Y),(X0 +​​ Y,Y0 + x)时,(X0-γ,Y0-x)的)事后追加所有阵列以正确的顺序。请注意,您添加的第一个四点到正确的阵列。

I need to have all point coordinates for a given circle one after another, so I can make an object go in circles by hopping from one point to the next. I tried the Midpoint circle algorithm, but the version I found is meant to draw and the coordinates are not sequential. They are produced simultaneously for 8 quadrants and in opposing directions on top of that. If at least they were in the same direction, I could make a separate array for every quadrant and append them to one another at the end. This is the JavaScript adapted code I have now:

  function calcCircle(centerCoordinates, radius) {
    var coordinatesArray = new Array();
    // Translate coordinates
    var x0 = centerCoordinates.left;
    var y0 = centerCoordinates.top;
    // Define variables
    var f = 1 - radius;
    var ddFx = 1;
    var ddFy = -radius << 1;
    var x = 0;
    var y = radius;
    coordinatesArray.push(new Coordinates(x0, y0 + radius));
    coordinatesArray.push(new Coordinates(x0, y0 - radius));
    coordinatesArray.push(new Coordinates(x0 + radius, y0));
    coordinatesArray.push(new Coordinates(x0 - radius, y0));
    // Main loop
    while (x < y) {
      if (f >= 0) {
        y--;
        ddFy += 2;
        f += ddFy;
      }
      x++;
      ddFx += 2;
      f += ddFx;
      coordinatesArray.push(new Coordinates(x0 + x, y0 + y));
      coordinatesArray.push(new Coordinates(x0 - x, y0 + y));
      coordinatesArray.push(new Coordinates(x0 + x, y0 - y));
      coordinatesArray.push(new Coordinates(x0 - x, y0 - y));
      coordinatesArray.push(new Coordinates(x0 + y, y0 + x));
      coordinatesArray.push(new Coordinates(x0 - y, y0 + x));
      coordinatesArray.push(new Coordinates(x0 + y, y0 - x));
      coordinatesArray.push(new Coordinates(x0 - y, y0 - x));
    }
    // Return the result
    return coordinatesArray;
  }

I prefer some fast algorithm without trigonometry, but any help is appreciated!

EDIT

This is the final solution. Thanks everybody!

  function calcCircle(centerCoordinates, radius) {
    var coordinatesArray = new Array();
    var octantArrays =
      {oct1: new Array(), oct2: new Array(), oct3: new Array(), oct4: new Array(),
       oct5: new Array(), oct6: new Array(), oct7: new Array(), oct8: new Array()};
    // Translate coordinates
    var xp = centerCoordinates.left;
    var yp = centerCoordinates.top;
    // Define add coordinates to array
    var setCrd =
      function (targetArray, xC, yC) {
        targetArray.push(new Coordinates(yC, xC));
      };
    // Define variables
    var xoff = 0;
    var yoff = radius;
    var balance = -radius;
    // Main loop
    while (xoff <= yoff) {
      // Quadrant 7 - Reverse
      setCrd(octantArrays.oct7, xp + xoff, yp + yoff);
      // Quadrant 6 - Straight
      setCrd(octantArrays.oct6, xp - xoff, yp + yoff);
      // Quadrant 3 - Reverse
      setCrd(octantArrays.oct3, xp - xoff, yp - yoff);
      // Quadrant 2 - Straight
      setCrd(octantArrays.oct2, xp + xoff, yp - yoff);
      // Avoid duplicates
      if (xoff != yoff) {
        // Quadrant 8 - Straight
        setCrd(octantArrays.oct8, xp + yoff, yp + xoff);
        // Quadrant 5 - Reverse
        setCrd(octantArrays.oct5, xp - yoff, yp + xoff);
        // Quadrant 4 - Straight
        setCrd(octantArrays.oct4, xp - yoff, yp - xoff);
        // Quadrant 1 - Reverse
        setCrd(octantArrays.oct1, xp + yoff, yp - xoff);
      }
      // Some weird stuff
      balance += xoff++ + xoff;
      if (balance >= 0) {
        balance -= --yoff + yoff;
      }
    }
    // Reverse counter clockwise octant arrays
    octantArrays.oct7.reverse();
    octantArrays.oct3.reverse();
    octantArrays.oct5.reverse();
    octantArrays.oct1.reverse();
    // Remove counter clockwise octant arrays last element (avoid duplicates)
    octantArrays.oct7.pop();
    octantArrays.oct3.pop();
    octantArrays.oct5.pop();
    octantArrays.oct1.pop();
    // Append all arrays together
    coordinatesArray =
      octantArrays.oct4.concat(octantArrays.oct3).concat(octantArrays.oct2).concat(octantArrays.oct1).
        concat(octantArrays.oct8).concat(octantArrays.oct7).concat(octantArrays.oct6).concat(octantArrays.oct5);
    // Return the result
    return coordinatesArray;
  }

解决方案

Use can try the following approach: use the algorithm you gave but push your coordinates to eight different coordinateArrays. Afterwards you have to reverse half of them (those with (x0+x,y0-y), (x0-x,y0+y), (x0+y,y0+x), (x0-y,y0-x)) and afterwards append all arrays in the correct order. Take care that you add the first four points to the correct arrays.