# Ex Nihilo

A Processing Sketch Blog

## Bring on the Trig

2Π. Pythagorean. SOH CAH TOA. Conjures up terms of grade 10, doesn’t it? I had to go waaay back into the memory banks to even pull out a starting point for a recent trigonometry refresher.

It started like this: I had an idea to plot some points on a sphere. Now, I understand with some crafty use of the `rotate()` function I’d probably be able to get by without doing the math myself, but I guess I had the urge to dig a bit deeper in case I need to really know this (again) at some point.

So I started light and plotted some random points around a circle. By setting the X coordinate, I can reverse the Pythagorean equation enough to work out the math for the Y coordinate:

``````
X = random(0 - radius, radius);
Y = sqrt(pow(radius, 2) - pow(X, 2));
``````

That only gets me a positive value for Y though, which renders as a semicircle. So a bit more randomization was necessary to get the full circle:

``````
int rev = int(random(0, 2));
if (rev == 1) {
Y = sqrt(pow(radius, 2) - pow(X, 2));
} else {
Y = 0 - sqrt(pow(radius, 2) - pow(X, 2));
};
``````

Which doesn’t strike me as terribly elegant, but that’s about the limit of my coding ability.

So that was the start. Now that I can plot individual points around a circle, why not use something other than points? With some fairly basic `box()` objects and various rotations and transformations, things get interesting quickly:

I still haven’t gotten the math for the sphere down yet, though. That was a “walk away for now, and come back to it in a few days” type of problem.

Monday, August 17th, 2009 | Math | RSS 2.0

### 2 Responses to “Bring on the Trig”

1. Matthew says:

Not sure if this helps or not, but you can also think of points in terms of polar coordinates when working with circles. So instead of thinking of x,y coordinates, you specify points in terms of a distance from the origin and an angle from the x-axis. To generate random points on a circle with radius r, you can generate a random set of angles between 0 and 2pi (which we will call q) and then you plot (x = r * cos(q), y = r * sin(q)). The should help eliminate the tendency to not plot points near the axis as seen in your first white/gray sample and give a more even distribution along the circumference.

2. ben karel says:

Polar coordinates are indeed an excellent idea for thinking about, and generating, 2D graphics. What’s more, polar coordinates generalize very nicely to spherical coordinates — you just add another angle, to get two angles and a distance.