# Nested for loop sphere and deformations

Hi there!

I’m currently using this principle to make a sphere of particles:
x = r * sin(phi) * cos(theta)
y = r * cos(phi)
z = r * sin(phi) * sin(theta)

And using an example sent to me:

for (int i = 0; i < 100; i++) {
for (int j = 1; j < 100; j++) {

``````		float theta = ofDegToRad(ofMap(i, 0, 100, 360.0, 0.0));
float phi = ofDegToRad(ofMap(j, 0, 100, 0.0, 360.0));
vec3 position = radius* vec3(sin(phi) * cos(theta), cos(phi), sin(phi) * sin(theta));
}
}
``````

My question is, would there be a way to, just by changing values, to give that sphere more of a “gourd”, or an 8 shape? Basically like what’s on this quick drawing:

I’ve attempted a few things using sin and cos but it’s just giving me very strange results, and I’m at a loss.
Any hint would be greatly appreciated!

Thank you

Hey @Potiron , fun question! My math skills aren’t fantastic, but I think you should try to find an alternative for r that you can use in the y = r * cos(phi) calculation. It may be some kind of polynomial that generates a curve, or a trig function of some kind. Sometimes the math is easier if the origin is the center.

A really fun option you could try is to generate a curve with an ofPolyline (using its .curveTo() or .bezierTo() methods) and then use values from the curve in place r when calculating y. The ofPolyline class has lots of great functions for getting points, angles, and etc from an ofPolyline. Because my math skills aren’t awesome, I’d likely try this option first. Again, the math might be easier if you keep the origin at the center. This way, values of y < 0 can be the regular r (a constant), and values of y > 0 can be from the ofPolyline. This might be a great way to go too, because changing the shape would only involve regenerating the ofPolyline, rather than applying new coefficients to an equation, or applying a whole new equation to generate the new shape.