如何画圆弧。
首先两点(p1,p2)p1代表圆心,p2代表圆上的一点,确定圆。然后根据另一点p3确定圆弧。难点在于确定圆弧的角度。
有时候大于180度。这个处理起来比较费劲。
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1
public void I_GE_Base.Draw()
2![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
3
4
float x_Start;
5
float y_Start;
6
float radius = 0;
7
float a_Start = 0;
8
float a_End = 0;
9
x_Start = m_Start.X;
10
y_Start = m_Start.Y;
11
12
//半径
13
radius = Distance(m_Start, m_End);
14
15
Graphics g = SDP_Graph.SDP_Main.bg_buf.Graphics;
16
g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias;
17
//消除锯齿
18
19![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (m_Num == 1)
{
20
g.DrawEllipse(m_Pen, x_Start - radius, y_Start - radius, 2 * radius, 2 * radius);
21
}
22
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (m_Num == 2)
{
24
//重要的一句。当半径为0时不画。
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (radius != 0)
{
26
a_Start = Angle(m_Start, m_End);
27
a_End = Angle(m_Start, m_End, m_Over);
28
g.DrawArc(m_OPen, x_Start - radius, y_Start - radius, 2 * radius, 2 * radius, a_Start, a_End);
29
}
30
31
}
32
33
}
34![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
36
/// 两点之间的距离
37
/// </summary>
38
public int Distance(Point p1, Point p2)
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
40
int dis = 0;
41
dis = (int)Math.Sqrt(Math.Pow((p1.X - p2.X), 2) + Math.Pow((p1.Y - p2.Y), 2));
42
return dis;
43
}
44![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
45![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
46
/// 求三点之间的夹角。其中以p1为顶点
47
/// </summary>
48
///
49![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
50
public int Angle(Point p1, Point p2, Point p3)
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
52
//p1代表圆心。abc分别代表三条边,ang代表夹角
53
int a;
54
int b;
55
int c;
56
int ret = 0;
57
int ang = 0;
58
a = Distance(p2, p3);
59
b = Distance(p1, p2);
60
c = Distance(p1, p3);
61
62
//当三点在同一条直线上时
63![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (c == (a + b) || b == (a + c))
{
64
ang = 0;
65
}
66![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (a == (b + c))
{
67
ang = 180;
68
}
69
//商不能为0
70![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (b != 0 && c != 0)
{
71
72![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
73
ang = (int)(Math.Acos(((Math.Pow(b, 2.0) + Math.Pow(c, 2.0) - Math.Pow(a, 2.0)) / (2 * b * c))) * (180 / Math.PI));
74
}
75![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch (Exception ex)
{
76
MessageBox.Show(ex.ToString());
77
}
78
79
}
80
81
ret = RetAngle(p1, p2, p3);
82
83![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (ret == 180)
{
84
ang = 180 - ang;
85
}
86
87
return ang + ret;
88
89
}
90![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
91![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
92![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
93
/// 求圆上一点到圆心,与x轴之间的夹角
94
/// </summary>
95
public int Angle(Point p1, Point p2)
96![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
97
98
//p1代表圆心,p2代表另外一点
99
int ang = 0;
100
101
//当位于不同象限时计算的角度不同。有两个象限需要加-180度。
102![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (((p2.X < p1.X) && (p2.Y > p1.Y) || ((p2.Y < p1.Y) && (p2.X < p1.X))))
{
103
ang = -180;
104
}
105
106![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (p1.Y != p2.Y)
{
107
ang += (int)(Math.Atan((p2.Y - p1.Y) / (p2.X - p1.X)) * (180 / Math.PI));
108
}
109![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (p2.Y >= p1.Y)
{
110
ang = 0;
111
}
112![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (p2.Y < p1.Y)
{
113
ang = 180;
114
}
115
116
return ang;
117
}
118![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
119![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
120
/// 判断角度是否大于180度
121
/// </summary>
122
public int RetAngle(Point start, Point end, Point over)
123![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
124![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (end.X > start.X)
{
125
//直线的两点式,判断在第一和第四象限是在直线的上方还是下方
126![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((((end.Y - start.Y) / (end.X - start.X)) * (over.X - start.X) + start.Y) < over.Y)
{
127
return 0;
128
}
129![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
130
return 180;
131
}
132
}
133
//在第三和第二象限
134![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else if (end.X < start.X)
{
135![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if ((((end.Y - start.Y) / (end.X - start.X)) * (over.X - start.X) + start.Y) < over.Y)
{
136
return 180;
137
}
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
139
return 0;
140
}
141
}
142![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
143
return 0;
144
}
145
}
146![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如何画圆弧。
首先两点(p1,p2)p1代表圆心,p2代表圆上的一点,确定圆。然后根据另一点p3确定圆弧。难点在于确定圆弧的角度。
有时候大于180度。这个处理起来比较费劲。