Results 1 to 8 of 8

Thread: [RESOLVED] Elliptical orbit

  1. #1
    Member
    Join Date
    Apr 12
    Posts
    48

    Resolved [RESOLVED] Elliptical orbit

    I have a project, but i can make the square rotate around (circle), not that i want, i want the square rotate like Elipse, can you help me?
    Attached Files Attached Files

  2. #2
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: Elliptical orbit

    Could you post the code here (or the part of the code, that's relevant).
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  3. #3
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: Elliptical orbit

    I have attached, so, okay, i will post code.
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Elipse
    {
        public partial class Main : Form
        {
            public Main()
            {
                InitializeComponent();
            }
            Graphics g;
            PointF[] p;
            PointF[] cn;
            Pen pen;
            private void Main_Load(object sender, EventArgs e)
            {
                pen = new Pen(Color.AntiqueWhite);
                g = this.CreateGraphics();
                p = new PointF[] { new PointF(200, 150), new PointF(300, 150) };
                cn = new PointF[] { new PointF(300, 150), new PointF(330, 150), new PointF(330, 180), new PointF(300, 180) };
            }
    
            private void Main_Paint(object sender, PaintEventArgs e)
            {
                //Dagiac(p,Color.AntiqueWhite);
                Dagiac(cn, Color.AntiqueWhite);
            }
            private PointF XoayDiemTamC(PointF p, PointF c, double a)
            {
                PointF Q = new PointF();
                Q.X = p.X * (float)Math.Cos(a) - p.Y * (float)Math.Sin(a) + c.X * (1 - (float)Math.Cos(a)) + c.Y * (float)Math.Sin(a);
                Q.Y = p.X * (float)Math.Sin(a) + (p.Y * (float)Math.Cos(a)) - c.X * (float)Math.Sin(a) + c.Y * (1 - (float)Math.Cos(a));
                return Q;
            }
            private PointF[] XoayDiemTamCDagiac(PointF[] p, PointF c, double a)
            {
                PointF[] Q = new PointF[p.Length];
                for (int i = 0; i < p.Length; i++)
                {
                    Q[i] = XoayDiemTamC(p[i], c, a);
                }
                return Q;
            }
            private void MyLine(PointF p1, PointF p2, Color c)
            {           
                g.DrawLine(new Pen(c), p1, p2);
            }
            private void Dagiac(PointF[] p, Color c)
            {
                for (int i = 0; i < p.Length - 1; i++)
                {
                    MyLine(p[i], p[i + 1], c);
                }
                MyLine(p[p.Length - 1], p[0], c);
            }
    
            private void timerxoay_Tick(object sender, EventArgs e)
            {
                //p = XoayDiemTamCDagiac(p, p[0], 1 * Math.PI / 180);
                cn = XoayDiemTamCDagiac(cn, p[0], 1 * Math.PI / 180);
                //g.Clear(BackColor);
                this.Invalidate();
            }
    
            private void cmdrotate_Click(object sender, EventArgs e)
            {
                timerxoay.Enabled = true;
            }
        }
    }
    Important here:
    Code:
    Q.X = p.X * (float)Math.Cos(a) - p.Y * (float)Math.Sin(a) + c.X * (1 - (float)Math.Cos(a)) + c.Y * (float)Math.Sin(a);
                Q.Y = p.X * (float)Math.Sin(a) + (p.Y * (float)Math.Cos(a)) - c.X * (float)Math.Sin(a) + c.Y * (1 - (float)Math.Cos(a));
    This code is equation of circle orbit, now, i want that is Elipse, can you help me?

  4. #4
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: Elliptical orbit

    I don't quite follow your code exactly. Why create a graphics object on the main form only to use it in the Paint event, in which a graphics object is already passed as a parameter. Also I think you're making this a tad more complicated than it is. Code below to hopefully give you what you need (or at least illustrate the principle):
    csharp Code:
    1. RectangleF myrect = new RectangleF(300.0f, 150.0f, 30.0f, 30.0f);
    2.         RectangleF myorbit = new RectangleF(200.0f, 100.0f, 115.0f, 65.0f);
    3.         float ellipse_angle = 0.0f;
    4.         float rotation_angle = 0.0f;
    5.  
    6.         private void Main_Paint(object sender, PaintEventArgs e)
    7.         {
    8.             e.Graphics.DrawEllipse(Pens.Red, myorbit.X, myorbit.Y, myorbit.Width, myorbit.Height);
    9.  
    10.             using (System.Drawing.Drawing2D.Matrix transformation = new System.Drawing.Drawing2D.Matrix() { })
    11.             {
    12.                 transformation.Translate(myrect.X, myrect.Y);
    13.                 transformation.Rotate(rotation_angle);
    14.                 transformation.Translate(-myrect.X, -myrect.Y);
    15.  
    16.                 e.Graphics.Transform = transformation;
    17.             }
    18.  
    19.             e.Graphics.DrawRectangle(Pens.AntiqueWhite, myrect.X, myrect.Y, myrect.Width, myrect.Height);
    20.         }
    21.  
    22.         private void timerxoay_Tick(object sender, EventArgs e)
    23.         {
    24.             ellipse_angle += (float)(Math.PI / 40.0);
    25.             rotation_angle += (float)(Math.PI / 10.0);
    26.             myrect.Location = new PointF(myorbit.X + (float)((Math.Cos(ellipse_angle) + 1.0) * myorbit.Width / 2.0),
    27.                                          myorbit.Y + (float)((Math.Sin(ellipse_angle) + 1.0) * myorbit.Height / 2.0));
    28.             this.Invalidate();
    29.         }
    30.  
    31.         private void cmdrotate_Click(object sender, EventArgs e)
    32.         {
    33.             timerxoay.Enabled = !timerxoay.Enabled;
    34.         }

    Regards Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  5. #5
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: Elliptical orbit

    Quote Originally Posted by ThomasJohnsen View Post
    I don't quite follow your code exactly. Why create a graphics object on the main form only to use it in the Paint event, in which a graphics object is already passed as a parameter. Also I think you're making this a tad more complicated than it is. Code below to hopefully give you what you need (or at least illustrate the principle):
    csharp Code:
    1. RectangleF myrect = new RectangleF(300.0f, 150.0f, 30.0f, 30.0f);
    2.         RectangleF myorbit = new RectangleF(200.0f, 100.0f, 115.0f, 65.0f);
    3.         float ellipse_angle = 0.0f;
    4.         float rotation_angle = 0.0f;
    5.  
    6.         private void Main_Paint(object sender, PaintEventArgs e)
    7.         {
    8.             e.Graphics.DrawEllipse(Pens.Red, myorbit.X, myorbit.Y, myorbit.Width, myorbit.Height);
    9.  
    10.             using (System.Drawing.Drawing2D.Matrix transformation = new System.Drawing.Drawing2D.Matrix() { })
    11.             {
    12.                 transformation.Translate(myrect.X, myrect.Y);
    13.                 transformation.Rotate(rotation_angle);
    14.                 transformation.Translate(-myrect.X, -myrect.Y);
    15.  
    16.                 e.Graphics.Transform = transformation;
    17.             }
    18.  
    19.             e.Graphics.DrawRectangle(Pens.AntiqueWhite, myrect.X, myrect.Y, myrect.Width, myrect.Height);
    20.         }
    21.  
    22.         private void timerxoay_Tick(object sender, EventArgs e)
    23.         {
    24.             ellipse_angle += (float)(Math.PI / 40.0);
    25.             rotation_angle += (float)(Math.PI / 10.0);
    26.             myrect.Location = new PointF(myorbit.X + (float)((Math.Cos(ellipse_angle) + 1.0) * myorbit.Width / 2.0),
    27.                                          myorbit.Y + (float)((Math.Sin(ellipse_angle) + 1.0) * myorbit.Height / 2.0));
    28.             this.Invalidate();
    29.         }
    30.  
    31.         private void cmdrotate_Click(object sender, EventArgs e)
    32.         {
    33.             timerxoay.Enabled = !timerxoay.Enabled;
    34.         }

    Regards Tom
    Thanks for your help, it work, but i still question, we can't edit this function to get my elipse?????
    Code:
    private PointF XoayDiemTamC(PointF p, PointF c, double a)
            {
                PointF Q = new PointF();
                Q.X = p.X * (float)Math.Cos(a) - p.Y * (float)Math.Sin(a) + c.X * (1 - (float)Math.Cos(a)) + c.Y * (float)Math.Sin(a);
                Q.Y = p.X * (float)Math.Sin(a) + (p.Y * (float)Math.Cos(a)) - c.X * (float)Math.Sin(a) + (c.Y * (1 - (float)Math.Cos(a)));
                return Q;
            }
    We can get it on this code????
    In addition, in your code, how to change the coordinate axis. In this form, the coordinate axis of your elips is:
    Name:  Untitled.jpg
Views: 119
Size:  13.1 KB
    How to custum the tilt of elip by my favourite?
    Thanks again, nice help, thank you.

  6. #6
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: Elliptical orbit

    All right, this problem solved. Thanks

  7. #7
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 10
    Location
    Denmark
    Posts
    521

    Re: [RESOLVED] Elliptical orbit

    It's really not that complex linear algebra.
    An ellipse in 2d at origin is determined by
    Code:
    e(phi) = (r1 * cos(phi), r2 * sin(phi))
    Change in coordinate matrices are simply rotations around the origin (without mirroring), and can be described as:
    Code:
               [ cos(theta) -sin(theta) ]
    R(theta) = [ sin(theta)  cos(theta) ]
    ,
    so the formula for a rotated ellipse at origin would be:
    Code:
    e(phi)R(theta) = (r1 cos(phi)cos(theta) + r2 sin(phi)sin(theta), r2 sin(phi)cos(theta) - r1 cos(phi)sin(theta))
    The example below should illustrate this (though you should know, that using trigonometry to draw arcs is highly inefficient):
    csharp Code:
    1. // This sub can be made many many times more efficient by using properties of the ellipse equation
    2.         // sqr(x/a) + sqr(y/b) = 1 (using derivatives and the fact, that there are 4 identical quadrants).
    3.         // There are plenty of highly efficient circle/ellipse algorithm to be found online.
    4.         // I decided to keep this simple, using trigonometry, since speed is obviously not your primary concern.
    5.         private void draw_rotated_ellipse(Graphics g, float theta, RectangleF ellipse)
    6.         {
    7.             PointF xaxis = new PointF((float)Math.Cos(theta), (float)Math.Sin(theta));
    8.             PointF yaxis = new PointF(-(float)Math.Sin(theta), (float)Math.Cos(theta));
    9.             PointF ellipse_point;
    10.             PointF current;
    11.             PointF previous = PointF.Empty;
    12.  
    13.             float delta_phi = (float)(Math.PI / 20.0);
    14.  
    15.             for (float phi = 0.0f; phi <= (float)(2.0 * Math.PI); phi += delta_phi)
    16.             {
    17.                 // Ellipse equation for an ellipse at origin.
    18.                 ellipse_point = new PointF((float)(ellipse.Width * Math.Cos(phi)), (float)(ellipse.Height * Math.Sin(phi)));
    19.  
    20.                 // Apply the rotation transformation and translate to new center.
    21.                 current = new PointF(ellipse.Left + (ellipse_point.X * xaxis.X + ellipse_point.Y * xaxis.Y),
    22.                                      ellipse.Top + (ellipse_point.X * yaxis.X + ellipse_point.Y * yaxis.Y));
    23.  
    24.                 if (previous != PointF.Empty) g.DrawLine(Pens.Red, previous, current);
    25.  
    26.                 previous = current;
    27.             }
    28.         }
    29.  
    30.         // This sub is pretty much identical to the one above, so not much explanation needed.
    31.         private void draw_rect_at_ellipse(Graphics g, float theta, RectangleF ellipse, float phi, RectangleF rect)
    32.         {
    33.             PointF xaxis = new PointF((float)Math.Cos(theta), (float)Math.Sin(theta));
    34.             PointF yaxis = new PointF(-(float)Math.Sin(theta), (float)Math.Cos(theta));
    35.             PointF ellipse_point;
    36.  
    37.             // Ellipse equation for an ellipse at origin.
    38.             ellipse_point = new PointF((float)(ellipse.Width * Math.Cos(phi)), (float)(ellipse.Height * Math.Sin(phi)));
    39.  
    40.             // Apply the rotation transformation and translate to new center.
    41.             rect.Location = new PointF(ellipse.Left + (ellipse_point.X * xaxis.X + ellipse_point.Y * xaxis.Y),
    42.                                        ellipse.Top + (ellipse_point.X * yaxis.X + ellipse_point.Y * yaxis.Y));
    43.  
    44.             g.DrawRectangle(Pens.AntiqueWhite, rect.X, rect.Y, rect.Width, rect.Height);
    45.         }

    and replace the paint event handler with:
    Code:
                draw_rotated_ellipse(e.Graphics, ellipse_angle, myorbit);
                draw_rect_at_ellipse(e.Graphics, ellipse_angle, myorbit, rotation_angle, myrect);
    Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  8. #8
    Member
    Join Date
    Apr 12
    Posts
    48

    Re: [RESOLVED] Elliptical orbit

    Yeah, thanks, you are thoughtful, my problem solved, ^_^

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •