private void AddArcs() { foreach (Arc obj in dxf.Arcs) { float startAngle = ConvertToGdiAngle(obj.StartAngle); float endAngle = ConvertToGdiAngle(obj.EndAngle); ArcF arcF; if (startAngle < endAngle) { arcF = new ArcF(ConvertToGdiX(obj.Center.X - obj.Radius), ConvertToGdiY(obj.Center.Y + obj.Radius), 2 * (float)obj.Radius, 2 * (float)obj.Radius, startAngle, (startAngle - endAngle), IsDotted(obj)); } else { arcF = new ArcF(ConvertToGdiX(obj.Center.X - obj.Radius), ConvertToGdiY(obj.Center.Y + obj.Radius), 2 * (float)obj.Radius, 2 * (float)obj.Radius, startAngle, (-startAngle + endAngle), IsDotted(obj)); } arcList.Add(arcF); List <PointF> pts = new List <PointF>(); PointF startPoint = new PointF(ConvertToGdiX(Math.Cos(arcF.StartAngle) * arcF.Radius), (float)(Math.Sin(arcF.StartAngle) * arcF.Radius)); PointF endPoint = new PointF(ConvertToGdiX(Math.Cos(arcF.StartAngle + arcF.SweepAngle) * arcF.Radius), (float)(Math.Sin(arcF.StartAngle + arcF.SweepAngle) * arcF.Radius)); pts.Add(startPoint); pts.Add(endPoint); PointF point0 = new PointF(arcF.Rectangle.Right, arcF.Center.Y); PointF point90 = new PointF(arcF.Center.X, arcF.Rectangle.Bottom); PointF point180 = new PointF(arcF.Rectangle.Left, arcF.Center.Y); PointF point270 = new PointF(arcF.Center.X, arcF.Rectangle.Top); if ((arcF.StartAngle <= 0 && arcF.StartAngle + arcF.SweepAngle > 0) || (arcF.StartAngle >= 0 && arcF.StartAngle + arcF.SweepAngle < 0) || (arcF.StartAngle <= 360 && arcF.StartAngle + arcF.SweepAngle > 360) || (arcF.StartAngle >= 360 && arcF.StartAngle + arcF.SweepAngle < 360)) { pts.Add(point0); } if ((arcF.StartAngle <= 90 && arcF.StartAngle + arcF.SweepAngle > 90) || (arcF.StartAngle >= 90 && arcF.StartAngle + arcF.SweepAngle < 90)) { pts.Add(point90); } if ((arcF.StartAngle <= 180 && arcF.StartAngle + arcF.SweepAngle > 180) || (arcF.StartAngle >= 180 && arcF.StartAngle + arcF.SweepAngle < 180)) { pts.Add(point180); } if ((arcF.StartAngle <= 270 && arcF.StartAngle + arcF.SweepAngle > 270) || (arcF.StartAngle >= 270 && arcF.StartAngle + arcF.SweepAngle < 270)) { pts.Add(point270); } foreach (var point in pts) { if (dxfMinX > point.X) { dxfMinX = point.X; } if (dxfMinY > point.Y) { dxfMinY = point.Y; } if (dxfMaxX < point.X) { dxfMaxX = point.X; } if (dxfMaxY < point.Y) { dxfMaxY = point.Y; } } } }
void GenArcFromThreePoint(float x1, float y1, float x2, float y2, float x3, float y3) { double a = x1 - x2; double b = y1 - y2; double c = x1 - x3; double d = y1 - y3; double e = ((x1 * x1 - x2 * x2) + (y1 * y1 - y2 * y2)) / 2.0; double f = ((x1 * x1 - x3 * x3) + (y1 * y1 - y3 * y3)) / 2.0; double det = b * c - a * d; if (Math.Abs(det) > 0.001) { //x0,y0为计算得到的原点 double x0 = -(d * e - b * f) / det; double y0 = -(a * f - c * e) / det; double radius = Math.Sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); double angle1; double angle2; double angle3; double sinValue1; double cosValue1; double sinValue2; double cosValue2; double sinValue3; double cosValue3; sinValue1 = (y1 - y0) / radius; cosValue1 = (x1 - x0) / radius; if (cosValue1 >= 0.99999) { cosValue1 = 0.99999; } if (cosValue1 <= -0.99999) { cosValue1 = -0.99999; } angle1 = Math.Acos(cosValue1); angle1 = angle1 / 3.14 * 180; if (sinValue1 < -0.05) { angle1 = 360 - angle1; } sinValue2 = (y2 - y0) / radius; cosValue2 = (x2 - x0) / radius; if (cosValue2 >= 0.99999) { cosValue2 = 0.99999; } if (cosValue2 <= -0.99999) { cosValue2 = -0.99999; } angle2 = Math.Acos(cosValue2); angle2 = angle2 / 3.14 * 180; if (sinValue2 < -0.05) { angle2 = 360 - angle2; } sinValue3 = (y3 - y0) / radius; cosValue3 = (x3 - x0) / radius; if (cosValue3 >= 0.99999) { cosValue3 = 0.99999; } if (cosValue3 <= -0.99999) { cosValue3 = -0.99999; } angle3 = Math.Acos(cosValue3); angle3 = angle3 / 3.14 * 180; if (sinValue3 < -0.05) { angle3 = 360 - angle3; } bool PosDown = false; double Delta13; if (angle1 < angle3) { Delta13 = angle3 - angle1; } else { Delta13 = angle3 - angle1 + 360; } double Delta12; if (angle1 < angle2) { Delta12 = angle2 - angle1; } else { Delta12 = angle2 - angle1 + 360; } if (Delta13 > Delta12) { PosDown = true; } else { PosDown = false; } ArcF arcF; if (PosDown) { if (angle3 > angle1) { arcF = new ArcF(new PointF((float)(x0 - radius), (float)(y0 - radius)), new SizeF((float)(2 * radius), (float)(2 * radius)), (float)(angle3), -(float)(angle3 - angle1), false); //arcF.SetScale(scaleSize); } else { arcF = new ArcF(new PointF((float)(x0 - radius), (float)(y0 - radius)), new SizeF((float)(2 * radius), (float)(2 * radius)), (float)(angle3), -(float)(angle3 - angle1 + 360), false); //arcF.SetScale(scaleSize); } } else { if (angle1 > angle3) { arcF = new ArcF(new PointF((float)(x0 - radius), (float)(y0 - radius)), new SizeF((float)(2 * radius), (float)(2 * radius)), (float)(angle1), -(float)(angle1 - angle3), false); //arcF.SetScale(scaleSize); } else { arcF = new ArcF(new PointF((float)(x0 - radius), (float)(y0 - radius)), new SizeF((float)(2 * radius), (float)(2 * radius)), (float)(angle1), -(float)(angle1 - angle3 + 360), false); //arcF.SetScale(scaleSize); } } if (arcF != null) { arcList.Add(arcF); } } }