C# Rotating Oval

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 }
时间: 2024-10-13 19:55:16

C# Rotating Oval的相关文章

Rotating clockwise inward matrix

Problem: Write a program to: 1) wait for the user to input a positive integer N, and 2) output a NxN matrix on the screen with numbers 1 to N, rotating clockwise inward. For example, if N = 4, then the output should be: 1 2 3 4 12 13 14 5 11 16 15 6

poj 3335 Rotating Scoreboard(半平面交)

Rotating Scoreboard Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6420   Accepted: 2550 Description This year, ACM/ICPC World finals will be held in a hall in form of a simple polygon. The coaches and spectators are seated along the ed

Python Tkinter canvas oval原理

Ovals, mathematically, are ellipses, including circles as a special case. The ellipse is fit into a rectangle defined by the coordinates (x0, y0) of the top left corner and the coordinates (x1, y1) of the bottom right corner: The oval will coincide w

490 - Rotating Sentences

 Rotating Sentences  In ``Rotating Sentences,'' you are asked to rotate a series of input sentences 90 degrees clockwise. So instead of displaying the input sentences from left to right and top to bottom, your program will display them from top to bo

Android - shape圆形画法(oval)

shape圆形画法(oval) 本文地址: http://blog.csdn.net/caroline_wendy 1. 创建一个文件夹drawable, 用于存放xml类型的图片资源; 2. 在drawable中建立一个shape标签的文件: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/an

使用oVal进行Java Bean 验证的注意事项

如果需要不同条件验证不同的属性的时候,需要使用profiles属性,每个校验注解符中都有.注意:oVal默认是启用所有的profiles,所以在对单独一个profile进行启用的时候,需要先disableAllProfiles. 当使用除了@NotNull之外的校验符时,需使用@NotNull先校验,否则其他校验符不起作用.譬如当使用@ValidateWithMethod校验符的时候,需要先使用@NotNull进行校验 全部Bean properties代码如下: @Data public cl

UVA490 Rotating Sentences

问题链接:UVA490 Rotating Sentences. 题意简述:输入若干行字符串,将其旋转90度后输出. 问题分析:需要一个二维数组存储输入的字符串,好在规模不大. 程序说明:封装了函数mygets()(函数gets()在新标准中,被建议不要使用,作用就自己做一个),其他都是套路. AC的C语言程序如下: /* UVA490 Rotating Sentences */ #include <stdio.h> #include <memory.h> #define MAXN

Rotating Image Slider - 图片旋转切换特效

非常炫的图片旋转滑动特效,相信会给你留下深刻印象.滑动图像时,我们会稍稍旋转它们并延缓各元素的滑动.滑块的不寻常的形状是由一些预先放置的元素和使用边框创建.另外支持自动播放选项,鼠标滚轮的功能. 在线演示      下载源码 您可能感兴趣的相关文章 网站开发中很有用的 jQuery 效果[附源码] 分享35个让人惊讶的 CSS3 动画效果演示 十分惊艳的8个 HTML5 & JavaScript 特效 Web 开发中很实用的10个效果[源码下载] 12款经典的白富美型 jQuery 图片轮播插件

Java对象校验框架之Oval

只要有接口,就会有参数的校验,目前开源的校验框架已经非常多了,不过不得不提一下Oval.OVal 是一个可扩展的Java对象数据验证框架,验证的规则可以通过配置文件.Annotation.POJOs 进行设定.可以使用纯 Java 语言.JavaScript .Groovy .BeanShell 等进行规则的编写.使用起来也非常简单 public class OvalTest { @Min(20) private int age; @Length(min = 6, max = 10) priva