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?
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?
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)
I have attached, so, okay, i will post code.
Important here: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; } } }
This code is equation of circle orbit, now, i want that is Elipse, can you help me?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));
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:
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); { 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)
Thanks for your help, it work, but i still question, we can't edit this function to get my elipse?????
We can get it on this code????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; }
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.
All right, this problem solved. Thanks
It's really not that complex linear algebra.
An ellipse in 2d at origin is determined by
Change in coordinate matrices are simply rotations around the origin (without mirroring), and can be described as:Code:e(phi) = (r1 * cos(phi), r2 * sin(phi))
,Code:[ cos(theta) -sin(theta) ] R(theta) = [ sin(theta) cos(theta) ]
so the formula for a rotated ellipse at origin would be:
The example below should illustrate this (though you should know, that using trigonometry to draw arcs is highly inefficient):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))
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 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. 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 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. 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:
TomCode:draw_rotated_ellipse(e.Graphics, ellipse_angle, myorbit); draw_rect_at_ellipse(e.Graphics, ellipse_angle, myorbit, rotation_angle, myrect);
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)
Yeah, thanks, you are thoughtful, my problem solved, ^_^