Skip to content

Instantly share code, notes, and snippets.

@Dillie-O
Last active January 5, 2016 19:18
Show Gist options
  • Save Dillie-O/4548290 to your computer and use it in GitHub Desktop.
Save Dillie-O/4548290 to your computer and use it in GitHub Desktop.
Function to calculate points on an equidistant star.
function CalculateStarPoints(centerX, centerY, arms, outerRadius, innerRadius)
{
var results = "";
var angle = Math.PI / arms;
for (var i = 0; i < 2 * arms; i++)
{
// Use outer or inner radius depending on what iteration we are in.
var r = (i & 1) == 0 ? outerRadius : innerRadius;
var currX = centerX + Math.cos(i * angle) * r;
var currY = centerY + Math.sin(i * angle) * r;
// Our first time we simply append the coordinates, subsequet times
// we append a ", " to distinguish each coordinate pair.
if (i == 0)
{
results = currX + "," + currY;
}
else
{
results += ", " + currX + "," + currY;
}
}
return results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment