Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Created August 24, 2020 03:30
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 Ram-N/46aba6b0bff276abe99a1cfd66cc6aab to your computer and use it in GitHub Desktop.
Save Ram-N/46aba6b0bff276abe99a1cfd66cc6aab to your computer and use it in GitHub Desktop.
How to Draw a Cardioid using Processing.py
w, h = 800, 800
def setup():
size(w, h)
stroke(0, 0, 250) # blue
num_steps = 200
step = TWO_PI / num_steps
r = 100 # Radius of both generating circles
def draw():
background(250)
noFill()
strokeWeight(3)
translate(width / 2, height / 2)
ellipse(0, 0, 100, 100) # base circle
# Every 200 frames, max_angle gets to be TWO_PI. THen it gets reset
max_angle = frameCount % num_steps
if max_angle: # Draw the orange circle
fill(220, 69, 0)
a = max_angle * step
ellipse(r * cos(a), r * sin(a), 100, 100) # large orange circle
# Let's exaggerate and show the point of contact between the two circles
fill(100, 255, 0)
x = r * (1 - cos(a)) * cos(a) + r / 2
y = r * (1 - cos(a)) * sin(a)
ellipse(x, y, 10, 10)
# This is where the cardioid gets drawn.
for ang in range(max_angle):
fill(0, 0, 250)
a = ang * step
x = r * (1 - cos(a)) * cos(a) + r / 2
y = r * (1 - cos(a)) * sin(a)
ellipse(x, y, 3, 3) # basically the x,y point. Drawing it as an ellipse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment