Cardioids: Part 1

Avi Gupta
hackerLog
Published in
3 min readDec 16, 2018

What is a cardioid?
A cardioid is basically the shape created by using one point on a circle and rolling it around on another circle with the same radius. This is what I mean:

GIF courtesy of Wikipedia.

But a cardioid actually follows an equation:
r = x + x(sin(theta));

In which “theta” is the angle at which the rolling circle (black, in the above gif) is at respective to the stationary congruent circle (blue, in the above gif), and “r” is the radius. The higher the variable “x” is, the larger the cardioid.

When x = 100, this is what the cardioid looks like, based on the code I wrote below:

cardioid when r = 100+100sin(theta).

(Link to code near the end.)

So that’s the basic structure of a cardioid.

So how did we make that cardioid using processing?

First, we declared our variables, r and theta.

// declares the universal variables: r (radius) and theta (the angle)
float r;
float theta;

Then, we set up the program.

//sets up the program.
void setup(){
size(600, 600);
r = height * .25;// radius is 1/4 of the height. (this doesn't matter later)
theta = 0;
background(0);
frameRate(1000);
}

After that, we draw it all out.

void draw() {
stroke(random(255), random(255), 255);// makes a random color (always has blue in it).
strokeWeight(random(5));// makes a random stroke thickness. translate(width/2, height/2);// moves the top left of the grid to the center of the canvas.

Now that we have our positioning right, we make the equation to translate cartesian coordinates to polar coordinates.

  // this translates cartesian coordinates into polar coordinates.
float x = r*sin(theta) ;
float y = r*cos(theta) ;

Now that that’s done, we put down the points to make the cardioid:

  point(x, y);// makes a point at the specified x and y.
theta+=1; // increases theta by 1.

Then, we make the basic equation for the cardioid:

  // this is the equation for a cardioid: r = x + x(sin(theta)).
// we use 'xc' because we've
// already used the 'x' variable for the x coordinate
// play around with xc!
float xc = 100;
r = xc + xc* sin(theta);

Finally, if you want to reset the canvas, add these lines of code:

// clears the canvas and starts over the process.
if (theta == 360){
background(0);
theta = 0;
}
}

Play around with the different variables to get different cardioids. What gives you more lobes (a lobe is the basic cardioid that I showed you)? What makes it smoother? Can you make the cardioid larger if you want to? How can you make the cardioid with lines? Can you make the circles visible like in the first gif?
So many questions! I’ll answer them in part 2.
In the meanwhile, let me know if you figure them out and show me what you did!

Full code on GitHub here.

Signing out ‘til next time,

Avi

--

--

Avi Gupta
hackerLog

Named after the first 2 games I got on my xBox, Forza and Minecraft. Also, i have a blog. Real name is Avi.