Aug 7th, 2012, 05:26 AM
#1
Thread Starter
Member
[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
Aug 7th, 2012, 09:10 AM
#2
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)
Aug 7th, 2012, 09:20 AM
#3
Thread Starter
Member
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?
Aug 7th, 2012, 02:13 PM
#4
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:
RectangleF myrect = new RectangleF(300.0f, 150.0f, 30.0f, 30.0f);
RectangleF myorbit = new RectangleF(200.0f, 100.0f, 115.0f, 65.0f);
float ellipse_angle = 0.0f;
float rotation_angle = 0.0f;
private void Main_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawEllipse(Pens.Red, myorbit.X, myorbit.Y, myorbit.Width, myorbit.Height);
using (System.Drawing.Drawing2D.Matrix transformation = new System.Drawing.Drawing2D.Matrix() { })
{
transformation.Translate(myrect.X, myrect.Y);
transformation.Rotate(rotation_angle);
transformation.Translate(-myrect.X, -myrect.Y);
e.Graphics.Transform = transformation;
}
e.Graphics.DrawRectangle(Pens.AntiqueWhite, myrect.X, myrect.Y, myrect.Width, myrect.Height);
}
private void timerxoay_Tick(object sender, EventArgs e)
{
ellipse_angle += (float)(Math.PI / 40.0);
rotation_angle += (float)(Math.PI / 10.0);
myrect.Location = new PointF(myorbit.X + (float)((Math.Cos(ellipse_angle) + 1.0) * myorbit.Width / 2.0),
myorbit.Y + (float)((Math.Sin(ellipse_angle) + 1.0) * myorbit.Height / 2.0));
this.Invalidate();
}
private void cmdrotate_Click(object sender, EventArgs e)
{
timerxoay.Enabled = !timerxoay.Enabled;
}
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)
Aug 7th, 2012, 08:08 PM
#5
Thread Starter
Member
Re: Elliptical orbit
Originally Posted by
ThomasJohnsen
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:
RectangleF myrect = new RectangleF(300.0f, 150.0f, 30.0f, 30.0f);
RectangleF myorbit = new RectangleF(200.0f, 100.0f, 115.0f, 65.0f);
float ellipse_angle = 0.0f;
float rotation_angle = 0.0f;
private void Main_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawEllipse(Pens.Red, myorbit.X, myorbit.Y, myorbit.Width, myorbit.Height);
using (System.Drawing.Drawing2D.Matrix transformation = new System.Drawing.Drawing2D.Matrix() { })
{
transformation.Translate(myrect.X, myrect.Y);
transformation.Rotate(rotation_angle);
transformation.Translate(-myrect.X, -myrect.Y);
e.Graphics.Transform = transformation;
}
e.Graphics.DrawRectangle(Pens.AntiqueWhite, myrect.X, myrect.Y, myrect.Width, myrect.Height);
}
private void timerxoay_Tick(object sender, EventArgs e)
{
ellipse_angle += (float)(Math.PI / 40.0);
rotation_angle += (float)(Math.PI / 10.0);
myrect.Location = new PointF(myorbit.X + (float)((Math.Cos(ellipse_angle) + 1.0) * myorbit.Width / 2.0),
myorbit.Y + (float)((Math.Sin(ellipse_angle) + 1.0) * myorbit.Height / 2.0));
this.Invalidate();
}
private void cmdrotate_Click(object sender, EventArgs e)
{
timerxoay.Enabled = !timerxoay.Enabled;
}
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:
How to custum the tilt of elip by my favourite?
Thanks again, nice help, thank you.
Aug 8th, 2012, 02:44 AM
#6
Thread Starter
Member
Re: Elliptical orbit
All right, this problem solved. Thanks
Aug 8th, 2012, 09:27 AM
#7
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:
// This sub can be made many many times more efficient by using properties of the ellipse equation
// sqr(x/a) + sqr(y/b) = 1 (using derivatives and the fact, that there are 4 identical quadrants).
// There are plenty of highly efficient circle/ellipse algorithm to be found online.
// I decided to keep this simple, using trigonometry, since speed is obviously not your primary concern.
private void draw_rotated_ellipse(Graphics g, float theta, RectangleF ellipse)
{
PointF xaxis = new PointF((float)Math.Cos(theta), (float)Math.Sin(theta));
PointF yaxis = new PointF(-(float)Math.Sin(theta), (float)Math.Cos(theta));
PointF ellipse_point;
PointF current;
PointF previous = PointF.Empty;
float delta_phi = (float)(Math.PI / 20.0);
for (float phi = 0.0f; phi <= (float)(2.0 * Math.PI); phi += delta_phi)
{
// Ellipse equation for an ellipse at origin.
ellipse_point = new PointF((float)(ellipse.Width * Math.Cos(phi)), (float)(ellipse.Height * Math.Sin(phi)));
// Apply the rotation transformation and translate to new center.
current = new PointF(ellipse.Left + (ellipse_point.X * xaxis.X + ellipse_point.Y * xaxis.Y),
ellipse.Top + (ellipse_point.X * yaxis.X + ellipse_point.Y * yaxis.Y));
if (previous != PointF.Empty) g.DrawLine(Pens.Red, previous, current);
previous = current;
}
}
// This sub is pretty much identical to the one above, so not much explanation needed.
private void draw_rect_at_ellipse(Graphics g, float theta, RectangleF ellipse, float phi, RectangleF rect)
{
PointF xaxis = new PointF((float)Math.Cos(theta), (float)Math.Sin(theta));
PointF yaxis = new PointF(-(float)Math.Sin(theta), (float)Math.Cos(theta));
PointF ellipse_point;
// Ellipse equation for an ellipse at origin.
ellipse_point = new PointF((float)(ellipse.Width * Math.Cos(phi)), (float)(ellipse.Height * Math.Sin(phi)));
// Apply the rotation transformation and translate to new center.
rect.Location = new PointF(ellipse.Left + (ellipse_point.X * xaxis.X + ellipse_point.Y * xaxis.Y),
ellipse.Top + (ellipse_point.X * yaxis.X + ellipse_point.Y * yaxis.Y));
g.DrawRectangle(Pens.AntiqueWhite, rect.X, rect.Y, rect.Width, rect.Height);
}
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)
Aug 8th, 2012, 10:32 AM
#8
Thread Starter
Member
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
Forum Rules
Click Here to Expand Forum to Full Width