Arc.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System;
  2. using System.Windows;
  3. using System.Windows.Media;
  4. using System.Windows.Shapes;
  5. namespace Venus_Themes.CustomControls
  6. {
  7. public class Arc : Shape
  8. {
  9. private Geometry Data
  10. {
  11. get { return (Geometry)GetValue(DataProperty); }
  12. set { SetValue(DataProperty, value); }
  13. }
  14. // Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
  15. public static readonly DependencyProperty DataProperty =
  16. DependencyProperty.Register("Data", typeof(Geometry), typeof(Arc), new PropertyMetadata((Geometry)Geometry.Parse(""), (s, e) =>
  17. {
  18. if (s is Arc arc && e.NewValue.ToString() != e.OldValue.ToString())
  19. {
  20. arc.InvalidateVisual();
  21. }
  22. }));
  23. public Rect Rect
  24. {
  25. get { return (Rect)GetValue(RectProperty); }
  26. set { SetValue(RectProperty, value); }
  27. }
  28. // Using a DependencyProperty as the backing store for Rect. This enables animation, styling, binding, etc...
  29. public static readonly DependencyProperty RectProperty =
  30. DependencyProperty.Register("Rect", typeof(Rect), typeof(Arc), new PropertyMetadata(default(Rect), propertyChangedCallback));
  31. public double StartAngle
  32. {
  33. get { return (double)GetValue(StartAngleProperty); }
  34. set { SetValue(StartAngleProperty, value); }
  35. }
  36. // Using a DependencyProperty as the backing store for StartAngle. This enables animation, styling, binding, etc...
  37. public static readonly DependencyProperty StartAngleProperty =
  38. DependencyProperty.Register("StartAngle", typeof(double), typeof(Arc), new PropertyMetadata(0.0, propertyChangedCallback));
  39. public double EndAngle
  40. {
  41. get { return (double)GetValue(EndAngleProperty); }
  42. set { SetValue(EndAngleProperty, value); }
  43. }
  44. // Using a DependencyProperty as the backing store for EndAngle. This enables animation, styling, binding, etc...
  45. public static readonly DependencyProperty EndAngleProperty =
  46. DependencyProperty.Register("EndAngle", typeof(double), typeof(Arc), new PropertyMetadata(0.0, propertyChangedCallback));
  47. protected override Geometry DefiningGeometry { get { return (Geometry)GetValue(DataProperty); } }
  48. private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
  49. {
  50. if (d is Arc arc)
  51. {
  52. // 判断绘制的圆弧是否可见,以提高性能
  53. if (arc.Rect.Width > 0 && arc.Rect.Height > 0 && arc.StartAngle != arc.EndAngle)
  54. {
  55. double ellipseA = arc.Rect.Width / 2;
  56. double ellipseB = arc.Rect.Height / 2;
  57. // 起止角度间隔大于等于360°,直接画圆
  58. if (Math.Abs(arc.StartAngle - arc.EndAngle) >= 360)
  59. {
  60. string data = $"M{arc.Rect.X + ellipseA * 2},{arc.Rect.Y + ellipseB + 0.001} A{ellipseA},{ellipseB} 0,1,1 {arc.Rect.X + ellipseA * 2},{arc.Rect.Y + ellipseB}z";
  61. arc.Data = (Geometry)Geometry.Parse(data);
  62. return;
  63. }
  64. // 椭圆公式:X²/a²+Y²/b²=1
  65. // Rect与椭圆各参数的对应关系:
  66. // Rect.X与Rect.Y分别是椭圆外接矩形相对Arc区域的左上角的偏移量;
  67. // Rect.Width与Rect.Height分别是椭圆外接矩形的宽和高
  68. // 以下根据StartAngle和EndAngle算出圆弧在矩形函数曲线中的起始点和终止点
  69. // 判断绘制方向
  70. bool clockWise = arc.EndAngle > arc.StartAngle;
  71. // 判断优弧/劣弧
  72. bool majorArc = Math.Abs(arc.StartAngle - arc.EndAngle) % 360 >= 180;
  73. // 将起始点和终止点转化为椭圆上的坐标
  74. double rad = 0;
  75. Point startPoint = new Point();
  76. if ((90 - arc.StartAngle) % 360 == 0)
  77. {
  78. startPoint.X = 0;
  79. startPoint.Y = ellipseB;
  80. }
  81. else if ((270 - arc.StartAngle) % 360 == 0)
  82. {
  83. startPoint.X = 0;
  84. startPoint.Y = -ellipseB;
  85. }
  86. else
  87. {
  88. rad = GetRadian(arc.StartAngle);
  89. startPoint.X = ellipseA * ellipseB / Math.Sqrt(Math.Pow(ellipseB, 2) + Math.Pow(ellipseA * Math.Tan(rad), 2));
  90. startPoint.X *= Math.Cos(rad) > 0 ? 1 : -1;
  91. startPoint.Y = startPoint.X * Math.Tan(rad);
  92. }
  93. Point endPoint = new Point();
  94. if ((90 - arc.EndAngle) % 360 == 0)
  95. {
  96. endPoint.X = 0;
  97. endPoint.Y = ellipseB;
  98. }
  99. else if ((270 - arc.EndAngle) % 360 == 0)
  100. {
  101. endPoint.X = 0;
  102. endPoint.Y = -ellipseB;
  103. }
  104. else
  105. {
  106. rad = GetRadian(arc.EndAngle);
  107. endPoint.X = ellipseA * ellipseB / Math.Sqrt(Math.Pow(ellipseB, 2) + Math.Pow(ellipseA * Math.Tan(rad), 2));
  108. endPoint.X *= Math.Cos(rad) > 0 ? 1 : -1;
  109. endPoint.Y = endPoint.X * Math.Tan(rad);
  110. }
  111. string pathData = $"M{startPoint.X + ellipseA + arc.Rect.X},{startPoint.Y + ellipseB + arc.Rect.Y} ";
  112. pathData += $"A{ellipseA},{ellipseB} ";
  113. pathData += $"0,{(majorArc ? "1" : "0")},{(clockWise ? "1" : "0")} ";
  114. pathData += $"{endPoint.X + ellipseA + arc.Rect.X},{endPoint.Y + ellipseB + arc.Rect.Y}";
  115. arc.Data = (Geometry)Geometry.Parse(pathData);
  116. }
  117. else
  118. {
  119. arc.Data = (Geometry)Geometry.Parse("");
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// 将角度转化为弧度
  125. /// </summary>
  126. /// <param name="angle"></param>
  127. /// <returns></returns>
  128. private static double GetRadian(double angle)
  129. {
  130. return angle / 180.0 * Math.PI;
  131. }
  132. }
  133. }