This program is used to show how to generate an oval.
The moon's orbit around the sun is an oval two.
锘縰sing System; using System.Windows.Forms; using System.Drawing; using System.Collections.Generic; class Haha : Form { Haha() { WindowState = FormWindowState.Maximized; Paint += draw; Timer t = new Timer(); t.Tick += delegate { Invalidate(); }; init(); t.Interval = 200; t.Start(); Activated += delegate { t.Start(); }; SizeChanged += delegate { if (WindowState == FormWindowState.Minimized) t.Stop(); }; } const int period=100; int now=0; Bitmap[] bit=new Bitmap[period]; void init() { double the=Math.PI*2/period; LinkedList<Point> mark = new LinkedList<Point>(); var p = new Pen(new SolidBrush(Color.Tomato), 1); for (int i = 0; i < bit.Length; i++) { bit[i] = new Bitmap(200,200); var g = Graphics.FromImage(bit[i]); int R = Math.Min(bit[i].Width, bit[i].Height) >> 1; int x = bit[i].Width >> 1; int y = bit[i].Height >> 1; g.DrawEllipse(p, x - R, y - R, R << 1, R << 1); int RR = R >> 1; double xx = x + RR * Math.Cos(the * i); double yy = y + RR * Math.Sin(the * i); g.DrawEllipse(p, (float)(xx - RR), (float)(yy - RR), RR << 1, RR << 1); double r = RR * 0.5; double xxx = xx + r * Math.Cos(-the * i); double yyy = yy + r * Math.Sin(-the * i); mark.AddLast(new Point((int)xxx, (int)yyy)); g.DrawEllipse(p, (float)(xxx - r), (float)(yyy - r), (float)r * 2, (float)r * 2); foreach (var point in mark) { g.FillEllipse(new SolidBrush(Color.Tomato), point.X, point.Y, 2, 2); } g.DrawLine(p, (float)xxx, (float)yyy, (float)xx, (float)yy); g.DrawLine(p, (float)xx, (float)yy, (float)x, (float)y); } } void draw(object o,PaintEventArgs e) { now = (now + 1) % period; int w = Math.Min(ClientSize.Width, ClientSize.Height); e.Graphics.DrawImage(bit[now],0,0,w,w); } static void Main() { Application.Run(new Haha()); } }
The oval rotates around its own corner.
using System; using System.Windows.Forms; using System.Drawing; class Haha : Form { Haha() { Text = "椭圆焦点极坐标方程:顺时针旋转减去旋转角,逆时针旋转加上旋转角"; Paint += draw; WindowState = FormWindowState.Maximized; } double a, b, w, h; double c, e, k; double fx(double the, double phi) { + e * Math.Cos(the - phi)) * Math.Cos(the); } double fy(double the, double phi) { + e * Math.Cos(the - phi)) * Math.Sin(the); } Point mix, miy, corner; double minX(double phi) { , r = Math.PI / * ; ) { ; double lm = l + d; double rm = r - d; if (fx(lm, phi) > fx(rm, phi)) l = lm; else r = rm; } mix.X = (int)fx(l, phi); miy.Y = (int)fy(l, phi); return fx(l, phi); } double minY(double phi) { ; ) { ; double lm = l + d; double rm = r - d; if (fy(lm, phi) > fy(rm, phi)) l = lm; else r = rm; } miy.X = (int)fx(l, phi); miy.Y = (int)fy(l, phi); return fy(l, phi); } void draw(object o, PaintEventArgs pe) { /; Text = ; pe.Graphics.Clear(Color.Wheat); ); * Math.PI / ; a=Math.Min(ClientSize.Width,ClientSize.Height)>>; b=0.618*a; k=b*b/a; c=Math.Sqrt(a*a-b*b); e=c/a; ; ; ; i< * Math.PI; i += the) { +e*Math.Cos(i-phi))*Math.Cos(i); + e * Math.Cos(i-phi)) * Math.Sin(i); pe.Graphics.FillEllipse(,); } pe.Graphics.FillEllipse(,); minX(phi); minY(phi); corner.X=mix.X; corner.Y=miy.Y; mix.X += centerX; mix.Y += centerY; miy.X += centerX; miy.Y += centerY; corner.X += centerX; corner.Y += centerY; pen.Color = Color.Black; pe.Graphics.DrawLine(pen, mix, corner); pen.Color = Color.Blue; pe.Graphics.DrawLine(pen, miy, corner); pe.Graphics.FillEllipse(, ); } static void Main() { Application.Run(new Haha()); } }