Skip to content

Instantly share code, notes, and snippets.

@goedel-gang
Created January 16, 2020 15:00
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 goedel-gang/1feea15f289e2892a90bf5ec66963455 to your computer and use it in GitHub Desktop.
Save goedel-gang/1feea15f289e2892a90bf5ec66963455 to your computer and use it in GitHub Desktop.
def draw_square(cx, cy, r, l, theta):
pushMatrix()
quarter_turns, theta = divmod(theta, HALF_PI)
translate(cx, cy)
rotate(quarter_turns * HALF_PI)
critical_theta = atan(l * 1.0 / (2 * r + l))
if theta <= critical_theta:
x = r + l / 2.0
y = (r + l / 2.0) * tan(theta)
elif theta >= HALF_PI - critical_theta:
x = (r + l / 2.0) / tan(theta)
y = r + l / 2.0
else:
a = 1
b = -l * (cos(theta) + sin(theta))
c = l ** 2 / 2.0 - r ** 2
R = (-b + sqrt(b ** 2 - 4 * a * c)) / (2 * a)
x = R * cos(theta)
y = R * sin(theta)
rect(x, y, l, l)
line(0, 0, x, y)
popMatrix()
def setup():
global theta
size(800, 800)
theta = 0.0
THETA_RATE = 0.01
def draw():
global theta
theta += THETA_RATE
background(0)
stroke(255)
noFill()
ellipseMode(RADIUS)
rectMode(CENTER)
ellipse(400, 400, 200, 200)
draw_square(400, 400, 200, 100, theta)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment