110,567
社区成员
发帖
与我相关
我的任务
分享
double u = (y[1] - y[0]) / (x[1] - x[0]);
double v = (y[2] - y[1]) / (x[2] - x[1]);
double w = 0.5 * (y[0] + y[1]) + 0.5 * (x[0] + x[1]) / ((y[1] - y[0]) / (x[1] - x[0]));
double k = 0.5 * (y[1] + y[2]) + 0.5 * (x[1] + x[2]) / ((y[2] - y[1]) / (x[2] - x[1]));
double cx = (k - w) / (1 / v - 1 / u);
double cy = (w - k) / u / (1 / v - 1 / u) + w;
double r = Math.Sqrt(Math.Pow((y[0] - cy), 2) + Math.Pow((x[0] - cx), 2));
class Program
{
static void Main(string[] args)
{
CircleInfo circle = new CircleInfo(new CPoint() { X = -2, Y = 0 }, new CPoint() { X = 0, Y = 2 }, new CPoint() { X = 2, Y = 0 });
Console.WriteLine(string.Format("CenterX:{0}\tCenterY:{1}\tRadius:{2}", circle.Center.X, circle.Center.Y, circle.Radius));
Console.ReadKey();
}
}
class CircleInfo
{
public CircleInfo(CPoint p1, CPoint p2, CPoint p3)
{
this.center = new CPoint();
if ((p2.X - p1.X) * (p3.Y - p1.Y) == (p2.Y - p1.Y) * (p3.X - p1.X))//三点在一条直线上
{
;
}
else
{
if (p2.Y - p1.Y != 0 && p3.Y - p1.Y != 0)
{
SetCircleInfo(p1, p2, p3);
}
else if (p1.Y - p2.Y != 0 && p3.Y - p2.Y != 0)
{
SetCircleInfo(p2, p1, p3);
}
else if (p2.Y - p3.Y != 0 && p1.Y - p3.Y != 0)
{
SetCircleInfo(p3, p2, p1);
}
}
}
private CPoint center;
public CPoint Center
{
get { return center; }
set { center = value; }
}
private double radius;
public double Radius
{
get { return radius; }
set { radius = value; }
}
void SetCircleInfo(CPoint p1, CPoint p2, CPoint p3)
{
double ratio1 = -(p2.X - p1.X) / (p2.Y - p1.Y);
double ratio2 = -(p3.X - p1.X) / (p3.Y - p1.Y);
CPoint cp1 = new CPoint() { X = 0.5 * (p1.X + p2.X), Y = 0.5 * (p1.Y + p2.Y) };
CPoint cp2 = new CPoint() { X = 0.5 * (p1.X + p3.X), Y = 0.5 * (p1.Y + p3.Y) };
center.X = (cp2.Y - cp1.Y + ratio1 * cp1.X - ratio2 * cp2.X) / (ratio1 - ratio2);
center.Y = ratio1 * (center.X - cp1.X) + cp1.Y;
radius = Math.Pow((p1.X - center.X) * (p1.X - center.X) + (p1.Y - center.Y) * (p1.Y - center.Y), 0.5);
}
}
class CPoint
{
public double X { get; set; }
public double Y { get; set; }
}