PolarToCartesian
Convert a polar coordinate (r,θ) to cartesian (x,y): x = r cos(θ), y = r sin(θ) Original by Daniel Shiffman.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
let r;
// Angle and angular velocity, accleration
let theta;
let theta_vel;
let theta_acc;
function setup() {
createCanvas(710, 400);
// Initialize all values
r = height * 0.45;
theta = 0;
theta_vel = 0;
theta_acc = 0.0001;
}
function draw() {
background(0);
// Translate the origin point to the center of the screen
translate(width / 2, height / 2);
// Convert polar to cartesian
let x = r * cos(theta);
let y = r * sin(theta);
// Draw the ellipse at the cartesian coordinate
ellipseMode(CENTER);
noStroke();
fill(200);
ellipse(x, y, 32, 32);
// Apply acceleration and velocity to angle
// (r remains static in this example)
theta_vel += theta_acc;
theta += theta_vel;
}