Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Ruch po elipsie, ale "pochylonej", dla matematyków chyba chwilka ;]
erix
post
Post #1





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Mam coś takiego:
Kod
var x = radius * Math.sin(degToRad(step));
var y = radius * Math.cos(degToRad(step));

to ruch po okręgu:

(IMG:http://images47.fotosik.pl/140/4109d6add456856e.png)

Kod
var x = radius * Math.sin(degToRad(step))*0.5;
var y = radius * Math.cos(degToRad(step));

po elipsie:

(IMG:http://images44.fotosik.pl/140/cdf80f6abaf258fb.png)

Ale nie mam pomysłu, w jaki sposób tę elipsę "pochylić":

(IMG:http://images48.fotosik.pl/140/6bedd0a7750dae0a.png)

Pogooglałem nieco i nigdzie nie znalazłem remedium; tylko rysowanie/torowanie po prostej elipsie. Może ktoś poratowałby wzorem/wskazówką byłego licealistę? (IMG:http://forum.php.pl/style_emoticons/default/snitch.gif)

edit:

Hmm, wymodziłem coś takiego:
Kod
var angle = 30;

//so calculate x and y (converting angle into radians)
var y = -radius * Math.cos(degToRad(step))*Math.sin(degToRad(angle));
var x = -(radius * Math.sin(degToRad(step))+y)*(1-Math.sin(degToRad(angle)));

Ale chyba nie do końca to.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 10)
Speedy
post
Post #2





Grupa: Zarejestrowani
Postów: 651
Pomógł: 28
Dołączył: 4.12.2004

Ostrzeżenie: (0%)
-----


W angielskiej wikipedii jest fajny opis, jak to zrobić (IMG:http://forum.php.pl/style_emoticons/default/winksmiley.jpg) . Dla sportu zrobiłem wersję w C++, która generuje plik z danymi dla Matlaba (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif) . Elipsa rysuje się prawidłowo.

Kod
#include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

/**
* @param float x - X coordinate
* @param float y - Y coordinate
* @param float a - Semimajor axis
* @param float b - Semiminor axis
* @param float angle - Angle of the ellipse
*/

void calculateEllipse(float x, float y, float a, float b, float angle, int steps)
{
    float alpha, sinalpha, cosalpha, beta, sinbeta, cosbeta, X, Y;
    // Angle is given by Degree Value
    beta = -angle * (M_PI / 180); // converts Degree Value into Radians
    sinbeta = sin(beta);
    cosbeta = cos(beta);
  
    float * XArray = new float[steps];
    float * YArray = new float[steps];

    for (int i = 0; i < 360; i += 360 / steps)
    {
        alpha = i * (M_PI / 180);  
        sinalpha = sin(alpha);
        cosalpha = cos(alpha);
        XArray[i] = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta);
        YArray[i] = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta);        
    }
    
    ofstream points;
    points.open("ellipse.m");
    
    points << "x = [ ";
    for (int i = 0; i < 360; i += 360 / steps)
        points << XArray[i] << ", ";
    points << "];" << endl;
    
    points << "y = [ ";
    for (int i = 0; i < 360; i += 360 / steps)
        points << YArray[i] << ", ";
    points << "]; " << endl;
    
    points << "plot(x,y);" << endl;
    points.close();
    delete[] XArray;
    delete[] YArray;
    
}

int main()
{
    calculateEllipse(10,10, 50, 20, 30, 360);
    system("pause");
    return 0;
}


Wyszło mi coś takiego:

(IMG:http://img132.imageshack.us/img132/7583/schowek02h.jpg)

Ten post edytował Speedy 8.06.2009, 00:36:55
Go to the top of the page
+Quote Post
Jabol
post
Post #3





Grupa: Przyjaciele php.pl
Postów: 1 467
Pomógł: 13
Dołączył: 22.02.2003

Ostrzeżenie: (0%)
-----


Rotacje robisz poprzez następującą transformacje:

R=[cos(a), sin(a);
-sin(a), cos(a)]
Go to the top of the page
+Quote Post
erix
post
Post #4





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




~Speedy dostał pomógła. ;]

Sportowałem to sobie pod JS.
Kod
function degToRad(angle){
        return ((angle*Math.PI)/180);
    }
    
    var speed=3;    // steps moved at once
    var angle=70;   // rotation angle
    var a = 50;     // oval dim 1°
    var b = 20;     // oval dim 2°
    var step = 0;
    
    beta = -degToRad(angle);    // angle
    sinbeta = Math.sin(beta);   // helper 1
    cosbeta = Math.cos(beta);   // helper 2
    

    function move() {
        if(step>360){
            step=0;
        }
        
        var alpha = degToRad(step)
        var sinalpha = Math.sin(alpha);
        var cosalpha = Math.cos(alpha);
        
        var x = (a*cosalpha*cosbeta-b*sinalpha*sinbeta);
        var y = (a*cosalpha*sinbeta+b*sinalpha*cosbeta);

        document.getElementById('point').style.left = (100+x)+'px';
        document.getElementById('point').style.top = (95+y)+'px';
        step += speed;

    }


    window.onload = function(){
        setInterval(move, 10);
    }


~dr_bonzo, czyli tak, jak jest to w tym listingu? Bo jeśli dobrze zrozumiałem, to chyba to. [;
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #5





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


@erix: ale ossochozzi?
Go to the top of the page
+Quote Post
erix
post
Post #6





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




A o jakiej transformacji napisałeś? (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)

Jeśli dobrze rozumiem, to:
Kod
R=[cos(a), sin(a);
-sin(a), cos(a)]

jest wyrażone przez
Kod
XArray[i] = x + (a * cosalpha * cosbeta - b * sinalpha * sinbeta);
YArray[i] = y + (a * cosalpha * sinbeta + b * sinalpha * cosbeta);

zgadza się?
Go to the top of the page
+Quote Post
nospor
post
Post #7





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




@erix to byl jabol a nie dr_bonzo (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
erix
post
Post #8





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Ups... Sory, Koledzy. [;

Dajcie im inne kolory.
Go to the top of the page
+Quote Post
mike
post
Post #9





Grupa: Przyjaciele php.pl
Postów: 7 494
Pomógł: 302
Dołączył: 31.03.2004

Ostrzeżenie: (0%)
-----


Cytat(erix @ 8.06.2009, 09:56:56 ) *
Dajcie im inne kolory.
Hmm, a to nie nick'i są identyfikatorami? (IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post
erix
post
Post #10





Grupa: Moderatorzy
Postów: 15 467
Pomógł: 1451
Dołączył: 25.04.2005
Skąd: Szczebrzeszyn/Rzeszów




Oj, zapędziłem się i najpierw spojrzałem na kolor grupy. ;p
Go to the top of the page
+Quote Post
dr_bonzo
post
Post #11





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Cytat
Dajcie im inne kolory.


^^ @erix: oj nospor, okularki sie przydadza (IMG:http://forum.php.pl/style_emoticons/default/smile.gif)
(IMG:http://forum.php.pl/style_emoticons/default/tongue.gif)
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 14.09.2025 - 22:36