码迷,mamicode.com
首页 > Windows程序 > 详细

C# Rotating Oval

时间:2015-11-04 17:31:31      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:

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.

 1 using System;
 2 using System.Windows.Forms;
 3 using System.Drawing;
 4 class Haha : Form
 5 {
 6     Haha()
 7     {
 8         Text = "椭圆焦点极坐标方程:顺时针旋转减去旋转角,逆时针旋转加上旋转角";
 9         Paint += draw;
10         WindowState = FormWindowState.Maximized; 
11     }
12     double a, b, w, h;
13     double c, e, k;
14     double fx(double the, double phi)
15     {
16         return k / (1 + e * Math.Cos(the - phi)) * Math.Cos(the);
17     }
18     double fy(double the, double phi)
19     {
20         return k / (1 + e * Math.Cos(the - phi)) * Math.Sin(the);
21     }
22     Point mix, miy, corner;
23     double minX(double phi)
24     {
25         double l = Math.PI / 2, r = Math.PI / 2 * 3;
26         while (r - l > 1e-8)
27         {
28             double d = (r - l) / 3;
29             double lm = l + d;
30             double rm = r - d;
31             if (fx(lm, phi) > fx(rm, phi)) l = lm;
32             else r = rm;
33         }
34         mix.X = (int)fx(l, phi);
35         miy.Y = (int)fy(l, phi);
36         return fx(l, phi);
37     }
38     double minY(double phi)
39     {
40         double l = -Math.PI, r = 0;
41         while (r - l > 1e-8)
42         {
43             double d = (r - l) / 3;
44             double lm = l + d;
45             double rm = r - d;
46             if (fy(lm, phi) > fy(rm, phi)) l = lm;
47             else r = rm;
48         }
49         miy.X = (int)fx(l, phi);
50         miy.Y = (int)fy(l, phi);
51         return fy(l, phi);
52     }
53     void draw(object o, PaintEventArgs pe)
54     { 
55         double phi = Math.PI*44/180;
56         Text = "" + phi / Math.PI * 180;
57         pe.Graphics.Clear(Color.Wheat);
58         var pen = new Pen(new SolidBrush(Color.Aqua), 3);
59         double the = 2 * Math.PI / 5000;
60         a=Math.Min(ClientSize.Width,ClientSize.Height)>>2;
61         b=0.618*a;
62         k=b*b/a;
63         c=Math.Sqrt(a*a-b*b);
64         e=c/a;
65         int centerX = ClientSize.Width >> 1;
66         int centerY = ClientSize.Height >> 1;
67         for (double i = 0; i< 2 * Math.PI; i += the)
68         {
69             double x=k/(1+e*Math.Cos(i-phi))*Math.Cos(i);
70             double y = k / (1 + e * Math.Cos(i-phi)) * Math.Sin(i);
71             pe.Graphics.FillEllipse(new SolidBrush(Color.Tomato),(int)(x+centerX),(int)(y+centerY),2,2);
72         }
73         pe.Graphics.FillEllipse(new SolidBrush(Color.Tomato), centerX, centerY,8,8);
74         minX(phi);
75         minY(phi);
76         corner.X=mix.X;
77         corner.Y=miy.Y;
78         mix.X += centerX;
79         mix.Y += centerY;
80         miy.X += centerX;
81         miy.Y += centerY;
82         corner.X += centerX;
83         corner.Y += centerY;
84         pen.Color = Color.Black;
85         pe.Graphics.DrawLine(pen, mix, corner);
86         pen.Color = Color.Blue;
87         pe.Graphics.DrawLine(pen, miy, corner);
88         pe.Graphics.FillEllipse(new SolidBrush(Color.Blue), miy.X, miy.Y, 8, 8);
89     }
90     static void Main()
91     {
92         Application.Run(new Haha());
93     }
94 }

 

C# Rotating Oval

标签:

原文地址:http://www.cnblogs.com/weidiao/p/4936356.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!