using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ellipse
{
    public partial class Form1 : Form
    {
        PointF xyPoint = new PointF(); //左上角的X,Y坐标
        PointF centerPoint = new PointF();//椭圆的中心点
        PointF movePoint = new PointF();
        PointF xiePointF=new PointF();
        PointF xiedPointF = new PointF();//倾斜一定角度后的点
        private double perAngle = 0;
        private double nowAngle;
        PointF[] pointFs=new PointF[201];
        PointF[] pointFs1=new PointF[201];//与pointFs对称的点数组,椭圆是对称的
        PointF[] xiePointFs=new PointF[201];//倾斜的椭圆上半部分
        PointF[] xiePointFs1 = new PointF[201];//倾斜的椭圆下半部分,对称的部分
        private float a =200;
        private float b =100;
        private float stepLength = 2;//通过X坐标逐次加2计算出Y坐标,将所有坐标存在数组中,用DrawCurve连接
        private float xLength = 0;//点到坐标Y轴的线段距离
        private float yLength = 0;//颠倒坐标X轴的线段距离
        private float dbYlength = 0;
        private float xiedLength = 0;//点到椭圆中心点的线段长度
        private int angle = 0;//倾斜的角度
        public Form1()
        {
            InitializeComponent();
        }

private void panel1_Paint(object sender, PaintEventArgs e)
        {
            
        }

private void panel1_MouseClick(object sender, MouseEventArgs e)////画布鼠标键点击事件,重画图形
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }
            if (this.textBox1.Text == "")
            {
                this.errorProvider1.SetError(this.textBox1, "请输入倾斜的角度");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.textBox1, "");
            }
            xyPoint.X = this.panel1.Width/2-200;
            xyPoint.Y = this.panel1.Height/2-100;
            centerPoint.X = xyPoint.X + a;
            centerPoint.Y = xyPoint.Y + b;
            Graphics graphics = this.panel1.CreateGraphics();
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(this.panel1.BackColor);
            Pen pen = new Pen(Color.Red);
            for (int index = 0; index < pointFs.Length; index++)
            {
                movePoint.X = index * stepLength + xyPoint.X;
                xLength = centerPoint.X - movePoint.X;
                yLength = (float)(Math.Sqrt((1 - (xLength * xLength) / 40000) * 10000));
                movePoint.Y = b - yLength + xyPoint.Y;
                pointFs[index] = movePoint;
            }
            graphics.DrawCurve(pen, pointFs, 1.5f);
            for (int index = 0; index < pointFs1.Length; index++)
            {
                dbYlength = centerPoint.Y - pointFs[index].Y;
                pointFs1[index].Y = centerPoint.Y + dbYlength;
                pointFs1[index].X = pointFs[index].X;
            }
            graphics.DrawCurve(pen, pointFs1, 1.5f);
            for (int index = 0; index < pointFs.Length; index++)
            {
                xiePointF = pointFs[index];
                double startAngle = Math.Atan2(xiePointF.Y - centerPoint.Y, xiePointF.X - centerPoint.X);
                string jiaodu = this.textBox1.Text;
                perAngle = int.Parse(jiaodu) * Math.PI / 180;
                nowAngle = startAngle + perAngle;
                xiedLength = (float)Math.Sqrt(Math.Pow(xiePointF.X - centerPoint.X, 2) + Math.Pow(xiePointF.Y - centerPoint.Y, 2));
                xiedPointF.X = centerPoint.X - (float)(xiedLength * Math.Cos(nowAngle));
                xiedPointF.Y = centerPoint.Y + (float)(xiedLength * Math.Sin(nowAngle));
                xiePointFs[index] = xiedPointF;
            }
            graphics.DrawCurve(pen, xiePointFs, 1.5f);
            for (int index = 0; index < pointFs1.Length; index++)
            {
                xiePointF = pointFs1[index];
                double startAngle = Math.Atan2(xiePointF.Y - centerPoint.Y, xiePointF.X - centerPoint.X);
                string jiaodu = this.textBox1.Text;
                perAngle = int.Parse(jiaodu) * Math.PI / 180;
                nowAngle = startAngle + perAngle;
                xiedLength = (float)Math.Sqrt(Math.Pow(xiePointF.X - centerPoint.X, 2) + Math.Pow(xiePointF.Y - centerPoint.Y, 2));
                xiedPointF.X = centerPoint.X - (float)(xiedLength * Math.Cos(nowAngle));
                xiedPointF.Y = centerPoint.Y + (float)(xiedLength * Math.Sin(nowAngle));
                xiePointFs1[index] = xiedPointF;
            }
            graphics.DrawCurve(pen, xiePointFs1, 1.5f);
        }

private void button1_Click(object sender, EventArgs e)//倾斜角度,每次递增10度
        {
            angle += 10;
            this.textBox1.Text = angle.ToString();
            if (this.textBox1.Text == "")
            {
                this.errorProvider1.SetError(this.textBox1, "请输入倾斜的角度");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.textBox1, "");
            }
            xyPoint.X = this.panel1.Width / 2-200;
            xyPoint.Y = this.panel1.Height / 2-100;
            centerPoint.X = xyPoint.X + a;
            centerPoint.Y = xyPoint.Y + b;
            Graphics graphics = this.panel1.CreateGraphics();
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(this.panel1.BackColor);
            Pen pen = new Pen(Color.Red);
            for (int index = 0; index < pointFs.Length; index++)
            {
                movePoint.X = index * stepLength + xyPoint.X;
                xLength = centerPoint.X - movePoint.X;
                yLength = (float)(Math.Sqrt((1 - (xLength * xLength) / 40000) * 10000));
                movePoint.Y = b - yLength + xyPoint.Y;
                pointFs[index] = movePoint;
            }
            graphics.DrawCurve(pen, pointFs, 1.5f);
            for (int index = 0; index < pointFs1.Length; index++)
            {
                dbYlength = centerPoint.Y - pointFs[index].Y;
                pointFs1[index].Y = centerPoint.Y + dbYlength;
                pointFs1[index].X = pointFs[index].X;
            }
            graphics.DrawCurve(pen, pointFs1, 1.5f);
            for (int index = 0; index < pointFs.Length; index++)
            {
                xiePointF = pointFs[index];
                double startAngle = Math.Atan2(xiePointF.Y - centerPoint.Y, xiePointF.X - centerPoint.X);
                string jiaodu = this.textBox1.Text;
                perAngle = int.Parse(jiaodu) * Math.PI / 180;
                nowAngle = startAngle + perAngle;
                xiedLength = (float)Math.Sqrt(Math.Pow(xiePointF.X - centerPoint.X, 2) + Math.Pow(xiePointF.Y - centerPoint.Y, 2));
                xiedPointF.X = centerPoint.X - (float)(xiedLength * Math.Cos(nowAngle));
                xiedPointF.Y = centerPoint.Y + (float)(xiedLength * Math.Sin(nowAngle));
                xiePointFs[index] = xiedPointF;
            }
            graphics.DrawCurve(pen, xiePointFs, 1.5f);
            for (int index = 0; index < pointFs1.Length; index++)
            {
                xiePointF = pointFs1[index];
                double startAngle = Math.Atan2(xiePointF.Y - centerPoint.Y, xiePointF.X - centerPoint.X);
                string jiaodu = this.textBox1.Text;
                perAngle = int.Parse(jiaodu) * Math.PI / 180;
                nowAngle = startAngle + perAngle;
                xiedLength = (float)Math.Sqrt(Math.Pow(xiePointF.X - centerPoint.X, 2) + Math.Pow(xiePointF.Y - centerPoint.Y, 2));
                xiedPointF.X = centerPoint.X - (float)(xiedLength * Math.Cos(nowAngle));
                xiedPointF.Y = centerPoint.Y + (float)(xiedLength * Math.Sin(nowAngle));
                xiePointFs1[index] = xiedPointF;
            }
            graphics.DrawCurve(pen, xiePointFs1, 1.5f);
        }

private void button2_Click(object sender, EventArgs e)//倾斜角度,每次递减10度
        {
            angle -= 10;
            this.textBox1.Text = angle.ToString();
            if (this.textBox1.Text == "")
            {
                this.errorProvider1.SetError(this.textBox1, "请输入倾斜的角度");
                return;
            }
            else
            {
                this.errorProvider1.SetError(this.textBox1, "");
            }
            xyPoint.X = this.panel1.Width / 2-200;
            xyPoint.Y = this.panel1.Height / 2-100;
            centerPoint.X = xyPoint.X + a;
            centerPoint.Y = xyPoint.Y + b;
            Graphics graphics = this.panel1.CreateGraphics();
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(this.panel1.BackColor);
            Pen pen = new Pen(Color.Red);
            for (int index = 0; index < pointFs.Length; index++)
            {
                movePoint.X = index * stepLength + xyPoint.X;
                xLength = centerPoint.X - movePoint.X;
                yLength = (float)(Math.Sqrt((1 - (xLength * xLength) / 40000) * 10000));
                movePoint.Y = b - yLength + xyPoint.Y;
                pointFs[index] = movePoint;
            }
            graphics.DrawCurve(pen, pointFs, 1.5f);
            for (int index = 0; index < pointFs1.Length; index++)
            {
                dbYlength = centerPoint.Y - pointFs[index].Y;
                pointFs1[index].Y = centerPoint.Y + dbYlength;
                pointFs1[index].X = pointFs[index].X;
            }
            graphics.DrawCurve(pen, pointFs1, 1.5f);
            for (int index = 0; index < pointFs.Length; index++)
            {
                xiePointF = pointFs[index];
                double startAngle = Math.Atan2(xiePointF.Y - centerPoint.Y, xiePointF.X - centerPoint.X);
                string jiaodu = this.textBox1.Text;
                perAngle = int.Parse(jiaodu) * Math.PI / 180;
                nowAngle = startAngle + perAngle;
                xiedLength = (float)Math.Sqrt(Math.Pow(xiePointF.X - centerPoint.X, 2) + Math.Pow(xiePointF.Y - centerPoint.Y, 2));
                xiedPointF.X = centerPoint.X - (float)(xiedLength * Math.Cos(nowAngle));
                xiedPointF.Y = centerPoint.Y + (float)(xiedLength * Math.Sin(nowAngle));
                xiePointFs[index] = xiedPointF;
            }
            graphics.DrawCurve(pen, xiePointFs, 1.5f);
            for (int index = 0; index < pointFs1.Length; index++)
            {
                xiePointF = pointFs1[index];
                double startAngle = Math.Atan2(xiePointF.Y - centerPoint.Y, xiePointF.X - centerPoint.X);
                string jiaodu = this.textBox1.Text;
                perAngle = int.Parse(jiaodu) * Math.PI / 180;
                nowAngle = startAngle + perAngle;
                xiedLength = (float)Math.Sqrt(Math.Pow(xiePointF.X - centerPoint.X, 2) + Math.Pow(xiePointF.Y - centerPoint.Y, 2));
                xiedPointF.X = centerPoint.X - (float)(xiedLength * Math.Cos(nowAngle));
                xiedPointF.Y = centerPoint.Y + (float)(xiedLength * Math.Sin(nowAngle));
                xiePointFs1[index] = xiedPointF;
            }
            graphics.DrawCurve(pen, xiePointFs1, 1.5f);
        }

private void textBox1_TextChanged(object sender, EventArgs e)//输入倾斜的角度
        {
            angle = int.Parse(this.textBox1.Text);
        }

}
}

C#窗体程序画倾斜一定角度的椭圆的更多相关文章

  1. 如何给windows窗体程序打包成一个安装包

    http://blog.csdn.net/xyy410874116/article/details/6341787 给windows窗体程序打包成一个安装包:具体操作在:http://hi.baidu ...

  2. 学习java随笔第十一篇:java窗体程序

    要开java的窗体程序,就要下载开发窗体的工具. 这里我用的是的myeclipse,可以直接在网上下载安装即可. 我用的是10.0版本的,如果需要汉化的话,可以看一下这篇文章:myeclipse.10 ...

  3. VS2010 win32项目windows窗体程序 向导生成代码解析

    目录: 1.Win32项目的windows窗体程序的向导生成了如下代码 2.手工生成代码如下 3.当消息队列中没有消息需要处理,我们可以利用这段时间处理我们自己的任务 1.Win32项目的window ...

  4. 使用控制台调试WinForm窗体程序

    .程序代码结构 .Win32DebuggerHelper.cs using System.Runtime.InteropServices; /* TODO:使用方法 Win32.AllocConsol ...

  5. 两种方法:VS2008下C++窗体程序显示控制台的方法——在QT程序中使用cout和cin

    老蔡写了一个基于QT的窗体程序,而过去写的类的调试信息都是用cout显示的,苦于窗体程序无法显示cout信息很多信息都看不到,于是就想到让控制台和窗体同时显示.显示控制台方法如下 1.项目(或者叫“工 ...

  6. C#窗体程序与sql sever 数据库链接

    一.所用工具 Visual Studio 2017和SQL Server Management Studio 2012 二.连接 打开SQL Server Management Studio 2012 ...

  7. winform窗体程序运行后怎样隐藏?

    运行winform窗体,我们是怎样隐藏的呢? 例子: 1)创建简单winform窗体 2)编写隐藏窗体程序的代码 3)效果演示 1)创建一个简单的winform窗体MainForm,

  8. MFC窗体程序中添加调试控制台

    在编写复杂程序的过程中,我们经常需要将一些信息输出到文件或者屏幕上.较控制台应用程序,MFC窗体程序要显得麻烦一些! 下面有2种方法来实现为MFC窗体程序添加调试控制台,方便程序员调试程序和了解当前程 ...

  9. c# WinForm窗体编程中对窗体程序设置快捷键

    c# WinForm窗体编程中对窗体程序设置快捷键http://www.cnblogs.com/bison1989/archive/2011/09/19/2180977.html /// <su ...

随机推荐

  1. SharePoint 2013的100个新功能之网站管理(二)

    一:SharePoint 2013的网站策略 网站策略指的是自动关闭或删除网站的条件.有四个选项:不自动关闭或删除网站.自动删除网站.自动关闭并删除网站和运行一个工作流来关闭.删除网站. 二:新的主题 ...

  2. .\Obj\main.axf: Error: L6406E: No space in execution regions with .ANY selector matching sin_i.o(.co

    这个问题原因是 芯片的 空间不足 解决方法是  在KEIL 的DEVICE中选择 更大的空间的芯片型号

  3. 通过代码来执行testng.xml

    大多数时候,我们都是通过Eclipse IDE上的操作命令来执行testng 框架下的case 运行.那如果我们不想通过这种方式,而是想通过代码调用来实现执行该怎么办?下面是我搜集的两种方式供大家参考 ...

  4. MySQL 面试基础

    相关:http://blog.csdn.net/u013252072/article/details/52912385          http://blog.csdn.net/zhangliang ...

  5. php操作Memcache示例

    <?php //==============================实例化============================ $mem=new Memcache; //====== ...

  6. 解决apache服务器本地可以访问,同局域网内他人不能访问的问题(转)

    问题提出:发现这个问题时,我同学让我做好的网页小案例给他看看,我是用wamp搭建的本地服务器,本地访问地址例如为: http://localhost/Dedecms/index.php 对于其他人要访 ...

  7. mybatis0208 缓存

    查询缓存 1.1缓存的意义 数据在磁盘会有一个IO,高并发读取效率就很低,将用户经常查询的数据放在缓存(内存)中,用户去查询数据就不用从磁盘上(关系型数据库数据文件)查询,从缓存中查询,从而提高查询效 ...

  8. 一次优化web项目的经历记录(一)

    一次优化web项目的经历记录 这段时间以来的总结与反思 前言:最近很长一段时间没有更新博客了,忙于一堆子项目的开发,严重拖慢了学习与思考的进程.开水倒满了需要提早放下杯子,晚了就会烫手,这段时间以来, ...

  9. [Redis] windows下安装 Redis

    一:Redis是什么? Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 通过https://github.c ...

  10. 高性能动画!HTML5 Canvas JavaScript框架KineticJS

    高性能动画!HTML5 Canvas JavaScript框架KineticJS KineticJS是一款开源的HTML5 Canvas JavaScript框架,能为桌面和移动应用提供高性能动画,并 ...