已知不在同一直线上的三点坐标,画弧,在线求代码,急!急!急!急!

解决方案 »

  1.   

    已知三点(x1,y1),(x2,y2),(x3,y3);
    则外接圆圆心:
    x0 ={(x1*x1+y1*y1-x3*x3-y3*y3)*(y2-y3)-(y1-y3)(x2*x2+y2*y2-x3*x3-y3*y3)}
     / (2*{(x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)}
    y0 ={ (x1-x3) *(x2*x2+y2*y2-x3*x3-y3*y3)-(x2-x3)(x1*x1+y1*y1-x3*x3-y3*y3)}
     / (2*{(x1-x3)*(y2-y3) - (y1-y3)*(x2-x3)}你根据圆心坐标用Graphics.DrawArc画弧线
      

  2.   

    也可用这种方式求圆心和半径
      float x1, y1, x2, y2, y3, x3;
                x1 =20.0f;
                y1 =90.0f;
                x2 = 120.0f;
                y2 = 70.0f;
                x3 = 40.0f;
                y3 = 20.0f;
                float x0, y0,r0;//圆心和半径
                float m1, m2,mx1,mx2,my1,my2;
                m1 = -(x2 - x1) / (y2 - y1);
                m2 = -(x3 - x2) / (y3 - y2);
                mx1 = (x1 + x2) / 2.0f;
                mx2 = (x2 + x3) / 2.0f;
                 my1 = (y1 + y2) / 2.0f;
                my2 = (y2 + y3) / 2.0f;
                x0=(m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2);
                y0=m1*(x0-mx1)+my1;
          
                r0 = Convert.ToSingle(Math.Sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1)));
      

  3.   

    下面画弧线你要根据个点和圆心的斜率来计算角度,要考虑好几种情况,Graphics.DrawArc画弧线
      

  4.   

    给你代码参考一下:你自己再测试一下
     float x1, y1, x2, y2, y3, x3;
                x1 =200.0f;
                y1 =150.0f;
                x2 = 380.0f;
                y2 = 130.0f;
                x3 = 40.0f;
                y3 = 200.0f;
                float x0, y0,r0;//圆心和半径
                float m1, m2,mx1,mx2,my1,my2;
                if( Math.Abs(y2 - y1) < 0.0001)
                {
                    m2 = -(x3 - x2) / (y3 - y2);
                    mx2 = (x2 + x3) / 2.0f;
                    my2 = (y2 + y3) / 2.0f;
                    x0 = (x1+x2)/2.0f;
                    y0 = m2 * (x0 - mx2) + my2;
                }
                else if (Math.Abs(y3 - y2)<0.0001)
                {
                    m1 = -(x2 - x1) / (y2 - y1);
                    mx1 = (x1 + x2) / 2.0f;   
                    my1 = (y1 + y2) / 2.0f;
                    x0 = (x2+x3) / 2.0f;
                    y0 = m1 * (x0 - mx1) + my1;
                }
                else
                {
                    m1 = -(x2 - x1) / (y2 - y1);
                    m2 = -(x3 - x2) / (y3 - y2);
                    mx1 = (x1 + x2) / 2.0f;
                    mx2 = (x2 + x3) / 2.0f;
                    my1 = (y1 + y2) / 2.0f;
                    my2 = (y2 + y3) / 2.0f;
                    x0 = (m1 * mx1 - m2 * mx2 + my2 - my1) / (m1 - m2);
                    y0 = m1 * (x0 - mx1) + my1;
                }
          
                r0 = Convert.ToSingle(Math.Sqrt((x0 - x1) * (x0 - x1) + (y0 - y1) * (y0 - y1)));
           
                Graphics g = this.pictureBox1.CreateGraphics();
                g.DrawEllipse(new Pen(Color.Blue), new RectangleF(x1, y1, 2,2));
                g.DrawEllipse(new Pen(Color.Blue), new RectangleF(x2, y2, 2, 2));
                g.DrawEllipse(new Pen(Color.Blue), new RectangleF(x3, y3, 2, 2));
                         //画弧
                float k1, k2, k3;//斜率
                k1 = (y1 - y0) / (x1 - x0);
                k2 = (y2 - y0) / (x2 - x0);
                k3 = (y3 - y0) / (x3 - x0);
            
                double[] a = new double[3];//弧度
                double[] b= new double[3];//角度
                double[] x = new double[3];
                double min, max;
                double v = 180.0 / 3.14159;//弧度与角度转换
                a[0] = Math.Atan(k1);
                a[1] = Math.Atan(k2);
                a[2] = Math.Atan(k3);            x[0] = x1;
                x[1] = x2;
                x[2] = x3;
                for (int i = 0; i < 3; i++)
                {
                    if (a[i] >= 0)
                    {
                        if (x[i] > x0)
                        {
                            b[i] = a[i] * v;
                        }
                        else if (x[i] < x0)
                        {
                            b[i] = 180.0 + a[i] * v;
                        }
                    }
                    else
                    {
                        a[i] = Math.Abs(a[i]);
                        if (x[i] > x0)
                        {
                            b[i] =360.0- a[i] * v;
                        }
                        else if (x[i] < x0)
                        {
                            b[i] = 180.0- a[i] * v;
                        }
                    }
                }
                //寻找最小角度和最大角度
                min = b[0];
                max = b[0];
                for (int i = 1; i < 3; i++)
                {
                    if (b[i] < min)
                        min = b[i];
                    if (b[i] > max)
                        max = b[i];
                }
                //画弧
                g.DrawArc(new Pen(Color.Black), new RectangleF(x0 - r0, y0 - r0, 2 * r0, 2 * r0), (float)min, (float)(max - min));
      

  5.   

    以下是我编的在窗口里任意点3个点,然后画出圆弧的程序。因为我也是个初学者,所以对Graphics画图不是很熟悉。我现在的一个问题是,为什么点击的地方和画出来的地方不匹配?
    我测试我的算法,好像没有什么问题的。难道是画图的pixel点值和从窗口里取的location不是一个概念么?using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication1
    {    public partial class Form1 : Form
        {
            int nCount = 0;
            Point[] p = new Point[3];        public Form1()
            {
                InitializeComponent();
            }        private void Form1_Click(object sender, EventArgs e)
            {
                if (nCount < 2)
                { // still wait for the click
                    p[nCount] = ((MouseEventArgs)e).Location;
                    nCount++;
                }
                else
                { //time to draw
                    Graphics g = this.CreateGraphics();
                    g.Clear(Color.White);
                    p[2] = ((MouseEventArgs)e).Location;
                    double[] x = new double[3];
                    double[] y = new double[3];
                    for(int i=0; i<3; i++){
                        x[i] = (double)p[i].X;
                        y[i] = (double)p[i].Y;
                    }
                   double x0, y0;
                    x0 = ((x[0] * x[0] + y[0] * y[0] - x[2] * x[2] - y[2] * y[2]) * (y[1] - y[2])
                         - (x[1] * x[1] + y[1] * y[1] - x[2] * x[2] - y[2] * y[2]) * (y[0] - y[2]))
                        / (2.0 * ((x[0] - x[2]) * (y[1] - y[2]) - (x[1] - x[2]) * (y[0] - y[2])));
                    y0 = ((x[1] * x[1] + y[1] * y[1] - x[2] * x[2] - y[2] * y[2]) * (x[0] - x[2])
                         - (x[0] * x[0] + y[0] * y[0] - x[2] * x[2] - y[2] * y[2])*(x[1]-x[2]))
                        / (2.0 * ((x[0] - x[2]) * (y[1] - y[2]) - (x[1] - x[2]) * (y[0] - y[2])));                double radius = Math.Sqrt((x[0] - x0) * (x[0] - x0) + (y[0] - y0) * (y[0] - y0));                Rectangle rect = new Rectangle((int)(x0-radius), (int)(y0-radius), (int)(x0+radius), (int)(y0+radius));                double theta0 = Math.Asin((y[0]-y0)/radius);
                    if (x[0] < x0) theta0 = Math.PI - theta0;
                    if (theta0 < 0) theta0 += Math.PI * 2.0;
                    double theta1 = Math.Asin((y[1]-y0)/radius);
                    if (x[1] < x0) theta1 = Math.PI - theta1;
                    if (theta1 < 0) theta1 += Math.PI * 2.0;
                    double theta2 = Math.Asin((y[2] - y0) / radius);
                    if (x[2] < x0) theta2 = Math.PI - theta0;
                    if (theta2 < 0) theta2 += Math.PI * 2.0;                Pen pen = new Pen(Color.Black, 3);
                    double startAngle = Math.Min(theta0, theta1);
                    if(startAngle > theta2) startAngle = theta2;
                    startAngle *= 180.0 / Math.PI;
                    double endAngle = Math.Max(theta0, theta1);
                    if(endAngle < theta2) endAngle = theta2;
                    endAngle *= 180.0 / Math.PI;
                    g.DrawArc(pen, rect, (float)startAngle, (float)(endAngle-startAngle));                nCount = 0;
                }        }
        }
    }