Skip to content

Instantly share code, notes, and snippets.

@shinout
Last active May 10, 2017 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinout/ad8457267950f2174fec3d3fc7594ece to your computer and use it in GitHub Desktop.
Save shinout/ad8457267950f2174fec3d3fc7594ece to your computer and use it in GitHub Desktop.
/**
* @param {number} n: 正n角形
* @param {number} r: 正n角形の中心から頂点への距離
* @return Array<{x: number, y: number }>: 座標
*/
function getRectanglePoints(n, r) {
// canvasに書かれる
const thetaUnit = Math.PI * 2 / n
const points = []
for (let i = 0; i < n; i++) {
points.push(polarToXY(r, thetaUnit * i))
}
return points
}
function polarToXY(r, theta) {
return {
x: r * Math.round(Math.cos(theta) * 1000) / 1000,
y: r * Math.round(Math.sin(theta) * 1000) / 1000
}
}
module.exports = getRectanglePoints
function getRaderChartPoints(r, rates) {
const thetaUnit = Math.PI * 2 / rates.length
return rates.map((rate, i) => polarToXY(r * rate, thetaUnit * i))
}
function polarToXY(r, theta) {
return {
x: r * Math.round(Math.cos(theta) * 1000) / 1000,
y: r * Math.round(Math.sin(theta) * 1000) / 1000
}
}
module.exports = getRaderChartPoints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment