- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
1. static class Extensions
2. {
3. /// <summary>
4. /// Adds an arc (fillet), if able, at each polyline vertex.
5. /// </summary>
6. /// <param name="pline">The instance to which the method applies.</param>
7. /// <param name="radius">The arc radius.</param>
8. public static void FilletAll(this Polyline pline, double radius)
9. {
10. int j = pline.Closed ? 0 : 1;
11. for (int i = j; i < pline.NumberOfVertices - j; i++)
12. {
13. i += pline.FilletAt(i, radius);
14. }
15. }
16.
17. /// <summary>
18. /// Adds an arc (fillet) at the specified vertex.
19. /// </summary>
20. /// <param name="pline">The instance to which the method applies.</param>
21. /// <param name="index">The index of the verex.</param>
22. /// <param name="radius">The arc radius.</param>
23. /// <returns>1 if the operation succeeded, 0 if it failed.</returns>
24. public static int FilletAt(this Polyline pline, int index, double radius)
25. {
26. int prev = index == 0 && pline.Closed ? pline.NumberOfVertices - 1 : index - 1;
27. if (pline.GetSegmentType(prev) != SegmentType.Line ||
28. pline.GetSegmentType(index) != SegmentType.Line)
29. {
30. return 0;
31. }
32. LineSegment2d seg1 = pline.GetLineSegment2dAt(prev);
33. LineSegment2d seg2 = pline.GetLineSegment2dAt(index);
34. Vector2d vec1 = seg1.StartPoint - seg1.EndPoint;
35. Vector2d vec2 = seg2.EndPoint - seg2.StartPoint;
36. double angle = vec1.GetAngleTo(vec2) / 2.0;
37. double dist = radius / Math.Tan(angle);
38. if (dist > seg1.Length || dist > seg2.Length)
39. {
40. return 0;
41. }
42. Point2d pt1 = seg1.EndPoint + vec1.GetNormal() * dist;
43. Point2d pt2 = seg2.StartPoint + vec2.GetNormal() * dist;
44. double bulge = Math.Tan((Math.PI / 2.0 - angle) / 2.0);
45. if (Clockwise(seg1.StartPoint, seg1.EndPoint, seg2.EndPoint))
46. {
47. bulge = -bulge;
48. }
49. pline.AddVertexAt(index, pt1, bulge, 0.0, 0.0);
50. pline.SetPointAt(index + 1, pt2);
51. return 1;
52. }
53.
54. /// <summary>
55. /// Evaluates if the points are clockwise.
56. /// </summary>
57. /// <param name="p1">First point.</param>
58. /// <param name="p2">Second point</param>
59. /// <param name="p3">Third point</param>
60. /// <returns>True if points are clockwise, False otherwise.</returns>
61. private static bool Clockwise(Point2d p1, Point2d p2, Point2d p3)
62. {
63. return ((p2.X - p1.X) * (p3.Y - p1.Y) - (p2.Y - p1.Y) * (p3.X - p1.X)) < 1e-8;
64. }
65. }
|
|