简单的几何图形关系判断

作者在 2011-11-24 16:36:43 发布以下内容
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace _0._0
{
    public partial class Form1 : Form
    {
        int m_drawtype = 0, n_drawtype = 0; //调用控件;
        int i = 0;                          //点的计数器;
        ArrayList ptX = new ArrayList();
        ArrayList ptY = new ArrayList();
        Point[] z_points = new Point[2];    //定义直线点的个数;
        Point[] s_points = new Point[3];    //定义三角形顶点的个数;
        Point[] f_points = new Point[10];   //定义多边形顶点的个数;
        Point[] n_points = new Point[1000]; //定义圆中等腰三角形的个数;
        Point[] t_points = new Point[1000]; //定义椭圆中等腰三角形的个数;
        PointF m_point = new PointF();      //定义圆心的坐标;
        PointF n_point = new PointF();      //定义椭圆的圆心坐标;
        PointF x_point = new PointF();      //定义一个未知点判断与三角形的关系;
        PointF s_point = new PointF();        //定义一个未知点判断与多边形的关系;
        PointF y_point = new PointF();      //定义一个未知点判断与圆的关系;

        public Form1()
        {
            InitializeComponent();
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();                   //退出;
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            ptX.Add(e.X);                   //鼠标单击一次,记录下点的X坐标;    
            ptY.Add(e.Y);                   //鼠标单击一次,记录下点的Y坐标;
            i++;                            //鼠标单击一次,计数器加1;

            
//判断点与三角形的位置关系;
            if (n_drawtype == 1)
            {
                x_point.X = e.X;            //未知点的X值;
                x_point.Y = e.Y;            //未知点的Y值;
                
//判断语句;
                if (((x_point.Y - s_points[0].Y) * (s_points[1].X - s_points[0].X) - (x_point.X - s_points[0].X) * (s_points[1].Y - s_points[0].Y)) * ((s_points[2].Y - s_points[0].Y) * (s_points[1].X - s_points[0].X) - (s_points[2].X - s_points[0].X) * (s_points[1].Y - s_points[0].Y)) > 0 && ((x_point.Y - s_points[1].Y) * (s_points[2].X - s_points[1].X) - (x_point.X - s_points[1].X) * (s_points[2].Y - s_points[1].Y)) * ((s_points[0].Y - s_points[1].Y) * (s_points[2].X - s_points[1].X) - (s_points[0].X - s_points[1].X) * (s_points[2].Y - s_points[1].Y)) > 0 && ((x_point.Y - s_points[0].Y) * (s_points[2].X - s_points[0].X) - (x_point.X - s_points[0].X) * (s_points[2].Y - s_points[0].Y)) * ((s_points[1].Y - s_points[0].Y) * (s_points[2].X - s_points[0].X) - (s_points[1].X - s_points[0].X) * (s_points[2].Y - s_points[0].Y)) > 0)
                    MessageBox.Show("点在三角形内!");
                else
                    MessageBox.Show("点在三角形外!");
                n_drawtype = 0;             //返回初始值;

            }

            //判断点与多边形的位置关系;
            if (n_drawtype == 2)
            {
                s_point.X = e.X;            //未知点的X值;
                s_point.Y = e.Y;            //未知点的Y值;
                double B = 0.0, B1 = 0.0, A = 0.0, SUM = 0.0;
                for (int k = 0; k < 10; k++)
                {
                    double Dx = f_points[k].X - s_point.X;
                    double Dy = f_points[k].Y - s_point.Y;

                    if (Dx == 0 && Dy > 0)
                    { B = 90.0; }
                    else if (Dx == 0 && Dy < 0)
                    { B = 270.0; }
                    else if (Dx > 0 && Dy >= 0)
                    { B = Math.Atan(Dy / Dx) ; }
                    else if (Dx > 0 && Dy < 0)
                    { B = Math.Atan(Dy / Dx) + 360.0; }
                    else
                    { B = Math.Atan(Dy / Dx) + 180.0; }

                    if (Math.Abs(B - B1) <= 180.0)
                    { A = B - B1; }
                    else if (B - B1 < -180.0)
                    { A = B - B1 + 360.0; }
                    else
                    { A = B - B1 - 360.0; }

                    B1 = B;

                    SUM = A + SUM;
                }

                if (Math.Abs(SUM - 360.0) < 1.0 || Math.Abs(SUM + 360.0) < 1.0)
                    MessageBox.Show("点在多边形内!");
                else
                    MessageBox.Show("点在多边形外!");
                n_drawtype = 0;              //返回初始值;
            }


            //圆心坐标;
            if (m_drawtype == 4)
            {
                m_point.X = e.X;            //圆心的X值;
                m_point.Y = e.Y;            //圆心的Y值;    
            }
            //判断点与圆的位置关系;
            if (n_drawtype == 3)
            {

                y_point.X = e.X;            //未知点的X值;
                y_point.Y = e.Y;            //未知点的Y值;
                
//判断语句;
                if (Math.Sqrt((y_point.X - m_point.X) * (y_point.X - m_point.X) + (y_point.Y - m_point.Y) * (y_point.Y - m_point.Y)) < Convert.ToDouble(textBox1.Text))
                    MessageBox.Show("点在圆内!");
                else
                    MessageBox.Show("点在圆外!");
                n_drawtype = 0;              //返回初始值;
            }

            //椭圆的圆心坐标;
            if (m_drawtype == 6)
            {
                n_point.X = e.X;            //椭圆圆心的X值;
                n_point.Y = e.Y;            //椭圆圆心的Y值;    
            }
        }

        private void Form1_DoubleClick(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();     //创建绘图函数;

            
//画直线;
            if (m_drawtype == 1)
            {
                for (int i = 0; i < 2; i++)
                {
                    z_points[i].X = (int)ptX[i];
                    z_points[i].Y = (int)ptY[i];
                }
                g.DrawLine(Pens.Red, z_points[0].X, z_points[0].Y, z_points[1].X, z_points[1].Y);   //画直线;
                ptX.Clear();                        //清零;
                ptY.Clear();

            }

            //画三角形;
            if (m_drawtype == 2)
            {
                if (i < 3)
                {
                    MessageBox.Show("less than 3 points!"); //判断顶点个数;
                    return;
                }

                for (int j = 0; j < 3; j++)
                {
                    s_points[j].X = (int)ptX[j];
                    s_points[j].Y = (int)ptY[j];
                }
                g.DrawPolygon(Pens.Blue, s_points); //画三角形;
                ptX.Clear();                        //清零;
                ptY.Clear();

            }


            //画多边形;
            if (m_drawtype == 3)
            {
                for (int k = 0; k < 10; k++)
                {
                    if (k >= Convert.ToInt32(textBox4.Text))
                    {
                        f_points[k].X = f_points[Convert.ToInt32(textBox4.Text) - 1].X;
                        f_points[k].Y = f_points[Convert.ToInt32(textBox4.Text) - 1].Y;
                    }
                    else
                    {
                        f_points[k].X = (int)ptX[k];
                        f_points[k].Y = (int)ptY[k];
                    }
                }
                g.DrawPolygon(Pens.Green, f_points);         //画多边形;
                ptX.Clear();                        //清零;
                ptY.Clear();
            }

            //画圆;
            if (m_drawtype == 5)
            {
                double q, d = 0.05;
                int n;
                q = 2.8 * Math.Sqrt(d / Convert.ToDouble(textBox1.Text));
                n = Convert.ToInt32(2 * Math.PI / q);
                for (int i = 0; i < 1000; i++)
                {
                    n_points[i].X = Convert.ToInt32((Convert.ToDouble(textBox1.Text) * Math.Cos(Convert.ToDouble(i) * q)) + m_point.X);
                    n_points[i].Y = Convert.ToInt32((Convert.ToDouble(textBox1.Text) * Math.Sin(Convert.ToDouble(i) * q)) + m_point.Y);
                }
                g.DrawLines(Pens.Red, n_points);    //画圆;
                MessageBox.Show(m_point.X.ToString() + "\t" + m_point.Y.ToString(), "圆心的坐标");
                ptX.Clear();        //清零;
                ptY.Clear();

            }

            //画椭圆;
            if (m_drawtype == 7)
            {
                double q, d = 0.05;
                int n;
                q = 2.8 * Math.Sqrt(d / Convert.ToDouble(textBox2.Text));
                n = Convert.ToInt32(2 * Math.PI / q);
                for (int i = 0; i < 1000; i++)
                {
                    t_points[i].X = Convert.ToInt32((Convert.ToDouble(textBox2.Text) * Math.Cos(Convert.ToDouble(i) * q)) + n_point.X);
                    t_points[i].Y = Convert.ToInt32((Convert.ToDouble(textBox3.Text) * Math.Sin(Convert.ToDouble(i) * q)) + n_point.Y);
                }
                g.DrawLines(Pens.Blue, t_points);    //画椭圆;
                MessageBox.Show(n_point.X.ToString() + "\t" + n_point.Y.ToString(), "椭圆圆心的坐标");
                ptX.Clear();        //清零;
                ptY.Clear();

            }
        }

        private void 直线ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_drawtype = 1;
        }

        private void 三角形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_drawtype = 2;
        }

        private void 点与三角形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            n_drawtype = 1;
        }

        private void 多边形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_drawtype = 3;
        }

        private void 点与多边形ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            n_drawtype = 2;
        }

        private void 确定圆心ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_drawtype = 4;
        }

        private void 绘制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_drawtype = 5;
        }
        private void 点与圆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            n_drawtype = 3;
        }

        private void 确定椭圆圆心ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_drawtype = 6;
        }

        private void 绘制ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            m_drawtype = 7;
        }

        private void 点与椭圆ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            n_drawtype = 4;
        }

        //判断输入的圆的半径数值是否正确
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //判断按键是不是要输入的类型。
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
                e.Handled = true;

            //小数点的处理。
            if ((int)e.KeyChar == 46)                           //小数点
            {
                if (textBox1.Text.Length <= 0)
                    e.Handled = true;   //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(textBox1.Text, out oldf);
                    b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);
                    if (b2 == false)
                    {
                        if (b1 == true)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }        
        }

        //判断输入的椭圆的长轴数值是否正确
        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            //判断按键是不是要输入的类型。
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
                e.Handled = true;

            //小数点的处理。
            if ((int)e.KeyChar == 46)                           //小数点
            {
                if (textBox2.Text.Length <= 0)
                    e.Handled = true;   //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(textBox2.Text, out oldf);
                    b2 = float.TryParse(textBox2.Text + e.KeyChar.ToString(), out f);
                    if (b2 == false)
                    {
                        if (b1 == true)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }

        //判断输入的椭圆的短轴数值是否正确
        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            //判断按键是不是要输入的类型。
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
                e.Handled = true;

            //小数点的处理。
            if ((int)e.KeyChar == 46)                           //小数点
            {
                if (textBox3.Text.Length <= 0)
                    e.Handled = true;   //小数点不能在第一位
                else
                {
                    float f;
                    float oldf;
                    bool b1 = false, b2 = false;
                    b1 = float.TryParse(textBox3.Text, out oldf);
                    b2 = float.TryParse(textBox3.Text + e.KeyChar.ToString(), out f);
                    if (b2 == false)
                    {
                        if (b1 == true)
                            e.Handled = true;
                        else
                            e.Handled = false;
                    }
                }
            }
        }
        //判断输入的多边形边数数值是否正确
        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            //判断按键是不是要输入的类型。
            if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 )
                e.Handled = true;    
        }
    }
}
 
c# | 阅读 807 次
文章评论,共0条
游客请输入验证码
文章分类
文章归档
最新评论