A
A
Anatol Faru2020-07-21 23:45:09
Python
Anatol Faru, 2020-07-21 23:45:09

How to create a cylindrical helix?

5f1755f0d29ed423713815.jpeg
Need to create a helix in 3d space.
I have the code to create a helix:

float t = distance/100.0;                    // длина линии
    pos2[0] = StartPos[0];                       // начальная позиция StartPos(точка вокруг которой будет рисоваться окружность)
    pos2[1] = StartPos[1] + radius;
    pos2[2] = StartPos[2];

        while (angle <= PI*t){
            x = radius * Cosine(angle);
            y = radius * Sine(angle);

            pos1[0] = pos2[0] + t;
            pos1[1] = pos2[1] + x;
            pos1[2] = pos2[2] + y;

            Здесь функция рисования линии.........................

            pos2 = pos1;
            angle += PI/10.0; // количество сегментов окружности (PI/10  -  10 сегментов)
        }

But this code only draws a line in one direction. I need this code to be able to draw a line in the direction of the vector I need, I can't figure out how to do it. I understand that you need to somehow rotate the plane in which the line is drawn.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2020-07-22
@tonline_kms65

You can multiply the resulting coordinates by the desired transformation matrix.
https://habr.com/ru/post/319144/ section "Rotation matrix"
The matrix is ​​calculated once before drawing, and then all the coordinates of the spiral are multiplied by it.
Example:
We have a vector around which we want to rotate the entire spiral: (vx,vy,vz)
There is an angle we want to rotate: a
Then the rotation matrix around this vector looks like this:

{
  {cos(a)+(1-cos(a))*vx*vx,     (1-cos(a))*vx*vy-sin(a)*vz,   (1-cos(a))*vx*vz+sin(a)*vy,  0},
  {(1-cos(a))*vy*vx+sin(a)*vz,  cos(a)+(1-cos(a))*vy*vy,     (1-cos(a))*vy*vz-sin(a)*vx,   0},
  {(1-cos(a))*vz*vx-sin(a)*vy,  (a-cos(a))*vz*vy+sin(a)*vx,  cos(a)+(1-cos(a))*vz*vz,      0},
  {0,                           0,                           0,                            1}
}

To simplify the formulas, I will denote the elements of the matrix through the variables a,b,c,d,e,f,g,h,i
a=cos(a)+(1-cos(a))*vx*vx
b=(1-cos(a))*vx*vy-sin(a)*vz
c=(1-cos(a))*vx*vz+sin(a)*vy
d=(1-cos(a))*vy*vx+sin(a)*vz
e=cos(a)+(1-cos(a))*vy*vy
f=(1-cos(a))*vy*vz-sin(a)*vx
g=(1-cos(a))*vz*vx-sin(a)*vy
h=(a-cos(a))*vz*vy+sin(a)*vx
i=cos(a)+(1-cos(a))*vz*vz

It turns out not so scary matrix:
a, b, c, 0
d, e, f, 0
g, h, i, 0
0, 0, 0, 1
To rotate any point (x, y, z) using this matrix , it is enough to multiply the coordinates of the point by the matrix, adding another unit as the fourth coordinate.
5f1bd4d78ffe6921159705.gif
Multiply to get the new coordinates of the point (x,y,z) (rotated by the given angle around the given vector)
newx = a*x + b*y + c*z
newy = d*x + e*y + f*z
newz = g*x + h*y + i*z
You can use this multiplication formula without thinking about matrices at all.
Just substitute the corresponding pieces of trigonometry, which I wrote above, instead of the letters ai.

I
inFureal, 2020-07-22
@inFureal

Look, you use the spiral formula
espiral.png
AND move along the Z axis. The more often you call, the denser the points, connect and draw

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question