您的位置:首页 > 编程语言 > C#

C# Rotating Oval

2015-11-04 16:17 316 查看
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)
{
return k / (1 + e * Math.Cos(the - phi)) * Math.Cos(the);
}
double fy(double the, double phi)
{
return k / (1 + e * Math.Cos(the - phi)) * Math.Sin(the);
}
Point mix, miy, corner;
double minX(double phi)
{
double l = Math.PI / 2, r = Math.PI / 2 * 3;
while (r - l > 1e-8)
{
double d = (r - l) / 3;
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 l = -Math.PI, r = 0;
while (r - l > 1e-8)
{
double d = (r - l) / 3;
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)
{
double phi = Math.PI*44/180;
Text = "" + phi / Math.PI * 180;
pe.Graphics.Clear(Color.Wheat);
var pen = new Pen(new SolidBrush(Color.Aqua), 3);
double the = 2 * Math.PI / 5000;
a=Math.Min(ClientSize.Width,ClientSize.Height)>>2;
b=0.618*a;
k=b*b/a;
c=Math.Sqrt(a*a-b*b);
e=c/a;
int centerX = ClientSize.Width >> 1;
int centerY = ClientSize.Height >> 1;
for (double i = 0; i< 2 * Math.PI; i += the)
{
double x=k/(1+e*Math.Cos(i-phi))*Math.Cos(i);
double y = k / (1 + e * Math.Cos(i-phi)) * Math.Sin(i);
pe.Graphics.FillEllipse(new SolidBrush(Color.Tomato),(int)(x+centerX),(int)(y+centerY),2,2);
}
pe.Graphics.FillEllipse(new SolidBrush(Color.Tomato), centerX, centerY,8,8);
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(new SolidBrush(Color.Blue), miy.X, miy.Y, 8, 8);
}
static void Main()
{
Application.Run(new Haha());
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: