Created
April 24, 2021 01:48
-
-
Save NachoToast/233d63f9ceed29bb84ca4871a3d846ab to your computer and use it in GitHub Desktop.
Create an array of coordinate objects {x, y} of points on a circle within set bounds.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function make_circle_coordinates(top = 0, left = 0, bottom = 300, right = 300, points = 20, visual = true) { | |
// e.g. top: 20, left: 20, right: 1900, bottom: 800 | |
let center = { | |
x: (left + right) / 2, | |
y: (top + bottom) / 2 | |
}, | |
radius = { | |
x: right - center.x, | |
y: bottom - center.y | |
}, | |
output_points = [], | |
// angles in radians | |
angle_step = 2 * Math.PI / points; | |
console.log(`Create circle with radius (${radius.x}, ${radius.y}) about point (${center.x}, ${center.y}).`); | |
// to do: this lol | |
for (let i = 0, angle = 0; i < points; i++, angle += angle_step) { | |
let point = { | |
x: center.x + Math.sin(angle) * radius.x, | |
y: center.y + Math.cos(angle) * radius.y | |
} | |
output_points.push(point); | |
} | |
if (visual) { | |
let p = document.createElement("p"); | |
p.classList.add("node"); | |
p.style.backgroundColor = "red"; | |
container.appendChild(p); | |
p.style.top = center.y + "px"; | |
p.style.left = center.x + "px"; | |
} | |
return output_points; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment