V
V
Vlad Romanovsky2014-03-13 23:16:21
JavaScript
Vlad Romanovsky, 2014-03-13 23:16:21

How to write a helix algorithm?

Good day. The following task is before me, but, unfortunately, I am not strong in mathematical algorithms and would be grateful for help.
There is an array of point coordinates with xy coordinates that match. It is necessary to arrange these points in a spiral around the original coordinate.
In fact - there is a set of events in Google Maps that point to 1 point and you need to arrange them around this point in a spiral so that you can see each marker. True, there are also other points nearby and new overlaps should be avoided, but this is already my problem :)

Answer the question

In order to leave comments, you need to log in

7 answer(s)
I
Ivan Starkov, 2014-03-13
@icelaba

Go here
en.wikipedia.org/wiki/%D0%90%D1%80%D1%85%D0%B8%D0%...
If the spiral suits you
write r = k*phi
x = r*cos(phi)
y = r*sin(phi)
suppose N points
for(i=0;i!=N;++i){
phi =i*some_const;
r = k*i;
x = r*cos(phi)
y = r*sin(phi)
draw(x,y, "what do you want to draw here")
} choose
the constants
k some_const yourself

Y
Yuri Morozov, 2014-03-13
@metamorph

Unfortunately, you are also not strong in presenting your thoughts.
What is "with xy coordinates that match"? What do they match?
What is the "original coordinate"? Does she sort of ask?
And events pointing to one point - what is it at all? ..

Y
Yuri Lobanov, 2014-03-14
@iiil

Here is an example in jQuery. To be honest, I did not understand what you need) First I wrote, and then I thought. Well, you can look at the algorithm in the example, and rewrite it yourself in the desired language)
jsfiddle.net/iiil/yLTnx/22

M
Mercury13, 2014-03-14
@Mercury13

I think the easiest way to do is not the Archimedes spiral, but its approximation by the Euler method. Let us have a certain distance dSafe, at which the circles should be from each other.
To put the second circle, move away from the central one by the vector (dSafe, 0). For the third and further we will do so.
Let's find the angle of the tangent to the spiral of Archimedes. The tangent vector will be a units along the radius vector (a is the helix coefficient r=af) and r units along the normal. Thus, if we are at the point (x, y), we get a tangent vector like this.
t = (a x + r y; a y − r x), r = sqrt(x² + y²)
Normalize this vector to length dSafe and add to (x, y).
Piece number one. The spiral parameter is directly proportional to the safe distance (you will see this below in the code). And thing number two - only “infinitely small” steps (at zero a, i.e. circle) will keep us on the circle, larger steps will take us out, to compensate we will make a negative.
Here is the actual code (in C++ Builder)

namespace {

const double dSafe = 30;
const int rSafe = dSafe / 2;
const double a = -0.3 * dSafe;

int toPixel(double x)
{
    return floor(x + 0.5) + 200;
}

}

void TfmMain::drawPoint(double x, double y)
{
    int xx = toPixel(x);
    int yy = toPixel(y);
    Canvas->Ellipse(xx - rSafe, yy - rSafe, xx + rSafe, yy + rSafe);
}

void __fastcall TfmMain::FormPaint(TObject *Sender)
{
    Canvas->Brush->Style = bsClear;
    Canvas->Pen->Color = clBlack;

    double x = 0, y = 0;
    drawPoint(x, y);
    x += dSafe;
    drawPoint(x, y);

    for (int i = 0; i < 120; ++i) {
        double r = sqrt(x*x + y*y);
        double tx = a*x + r*y;
        double ty = a*y - r*x;
        double tLen = sqrt(tx*tx + ty*ty);
        double k = dSafe / tLen;
        x += tx * k;
        y += ty * k;
        drawPoint(x, y);
    }
}

M
Mercury13, 2014-03-14
@Mercury13

And one more thing. Two to seven points should be placed differently (one in the center, the rest in a circle at a distance dSafe from it; well, or at worst, choose the coefficient a so that the spiral diverges only a little). For eight-eleven points it is necessary to select individual a. And only for twelve or more is a = −0.3 dSafe suitable.

M
Mercury13, 2014-03-14
@Mercury13

688b4461d0a145be8f07116dcdad6612.png

D
do6rolet, 2020-05-19
@do6rolet

import turtle
import math
turtle.shape('turtle')
k=0.1
rad=0.1
# calculation is based on the formula p=k*f;
# the transition from the polar coordinate system to the Cartesian one is carried out
# according to the formulas: x=p*cosФ y=p*sinФ
# cos&sin is calculated in terms of radians, and the displacement increment is set
# through the increase by the variable rad
for i in range (500):
x=k* math.degrees(rad)*math.cos(rad) # loop body. spaces are eaten when publishing
y=k*math.degrees(rad)*math.sin(rad) # loop body
turtle.goto(x,y) # loop body
rad+=0.1 # loop body
turtle.exitonclick() # exit from turtle by click

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question