Created
January 22, 2020 23:20
-
-
Save tshddx/28717d404f50656c6f3fb92345d35f44 to your computer and use it in GitHub Desktop.
Some trigonometry for drawing spinning squares inside other squares
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 generateStationarySquares(maxRad, squareCount) { | |
const squares = []; | |
for (let i = 0; i < squareCount; i++) { | |
// Divide maxRad by sqrt(2) i times. | |
const rad = maxRad / Math.pow(Math.sqrt(2), i); | |
// We want the outermost stationary square to be rotated 90 degrees, like a | |
// diamond. | |
const ang = i % 2 === 0 ? Math.PI / 4 : 0; | |
squares.push([rad, ang]); | |
} | |
squares.reverse(); | |
return squares; | |
} | |
function getOuterSquareAngle(outerRad, innerRad) { | |
// https://www.wolframalpha.com/input/?i=L+%3D+cos(a)+%2B+sin(a);+solve+for+a | |
const L = outerRad / innerRad; | |
const num = 1 - Math.sqrt(2 - L * L); | |
const den = L + 1; | |
const ang = 2 * (Math.PI + Math.atan(num / den)); | |
return ang; | |
} | |
function polarToCartesian(centerX, centerY, radius, angle) { | |
return { | |
x: centerX + radius * Math.cos(angle), | |
y: centerY + radius * Math.sin(angle), | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment