#Procedural spiral generator

12 messages · Page 1 of 1 (latest)

junior lodge
#

So, I thought to start playing with vertices positions in the hope of finding some good mesh shape.

I came up with this weird idea of using a starting point in the space, then adding to it a distance and an angle to find a second point, then doing the same to the second point to find the third one and so on.

I wanted to know if I would ended up drawing a circle by using this algorithm and, surprise, it kinda worked.
I don't know how but, no matter the angle and the distance, as long as they are constants those segments always close the ring shape as long as they are enough in number.

This is the function I use to find the next point based on the previous one:

fn get_next_3d(p: [f32; 3], distance: f32, theta: f32, gamma: f32) -> [f32; 3] {
    [
        p[0] + distance * theta.sin() * gamma.cos(),
        p[1] + distance * theta.sin() * gamma.sin(),
        p[2] + distance * theta.cos(),
    ]
}

It's using spherical coordinates, where p[0] is the old x, p[1] the y and p[2] the z.
distance is the distance from the previous point, theta is the angle between the z axis and the x axis, and gamma is the angle between the x axis and the y axis.

So, if I call the above function in a loop (let's say ranged between 0 and 50) where the radius is constant and z is always 0., then I get a closed ring.

If, instead, I slowly increase the distance from 0 to 25 and then decrease it again from 25 to 50. And if I also constantly increase z from 0 to 1, then I get two cones (as in the second attached gif).
This is how the distance is increased and decreased at each loop (num):

        if num < iterations_f32 / 2.0 {
            d = distance * (num / iterations_f32);
        } else {
            d = distance * ((iterations_f32 - num) / iterations_f32);
        }

Is it possible:
To get a flat ring instead of circles?
To get a sphere instead of cones?
To apply triangulation to create a mesh out of these vertices?
How?

balmy lake
#

in thise case it creates a sphere with uniform vertices

junior lodge
#

Awesome, thanks for pointing me to the right direction!

By the way I realized I can keep the distance constant and changing the theta angle instead (PI * (num / iterations_f32)).

More similar to a sphere than before but still far from what it's done in the video you shared.

I'll try it out.

naive surge
#

How?

A circle is an infinity of 0-length segments, with a 0 angle increment between each step.

Your algorithm draws regular polygons; when the number of sides tend to infinity the polygon shape converges to a circle. Since the screen pixels are discreet, you don't need to wait for infinity, at some point the segments are smaller than a pixel and you see a circle.

#

To make a sphere out of a series of polygons like this, vary the radius with sin(z) instead of linearly with z, where z here is the axis of the cone you had.

#

Constant -> cylinder
Linear -> cone
sin -> sphere

#

You can triangulate by hand after that, by creating triangle "strips" between two consecutive polygons, which is not too hard if they have the same number of sides (N sides, 2N triangles per strip).

#

For the sphere case you will end up with a so-called UV-sphere

#

Oh actually i didn't see you were generating a single line sorry. The GIF is quite small for me. The above works only with a series of polygons, not a single line.

#

Well, the sphere part is still correct though. But I'm not quite sure what's the interest of trying to mesh that spiral shape, that will probably be quite difficult.

junior lodge
#

Hi djee, thanks for your detailed explanation.
Sorry my code and gifs are slightly misleading.

Yes that's a single line, and also I don't have the actual radius. What I call radius is actually the distance between the previous point and the next one.

(I'll try to change the variable naming above to make it clearer).

I agree, what I was trying to achieve probably wasn't worth the effort.
It's interesting however to see that, having a segment length and an increasing angle, you can get regular polygons like that, at least in the 2d world.