Note that there are some explanatory texts on larger screens.

plurals
  1. POCalculate circle coordinates in a sequential fashion
    text
    copied!<p>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:</p> <pre><code> 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 &lt;&lt; 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 &lt; y) { if (f &gt;= 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; } </code></pre> <p>I prefer some fast algorithm without trigonometry, but any help is appreciated!</p> <p><strong>EDIT</strong></p> <p>This is the final solution. Thanks everybody!</p> <pre><code> 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 &lt;= 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 &gt;= 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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload