ViewTriangle.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.CommTool;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Globalization;
  7. using System.Runtime.Serialization;
  8. using System.Windows.Forms;
  9. namespace PaintDotNet.Annotation.FieldView
  10. {
  11. /// <summary>
  12. /// 三角形视场绘制
  13. /// </summary>
  14. public class ViewTriangle : ViewBase
  15. {
  16. public override double Width
  17. {
  18. get => BasicCalculationHelper.GetDistance(Points[1], Points[2]);
  19. set
  20. {
  21. var angle = BasicCalculationHelper.CalculateAngle(Points[2], new PointF(Points[2].X + 100, Points[2].Y), Points[1]);
  22. var p = new PointF((float)(Points[2].X + value), Points[2].Y);
  23. p = BasicCalculationHelper.GetAnglePoint(p, Points[2], angle);
  24. MoveHandleTo(p, 2);
  25. }
  26. }
  27. public override double Height
  28. {
  29. get => BasicCalculationHelper.GetDistance(Points[0], Points[2]);
  30. set
  31. {
  32. var angle = BasicCalculationHelper.CalculateAngle(Points[2],new PointF(Points[2].X + 100, Points[2].Y), Points[0]);
  33. var p = new PointF((float)(Points[2].X + value), Points[2].Y);
  34. p = BasicCalculationHelper.GetAnglePoint(p, Points[2], angle);
  35. MoveHandleTo(p, 1);
  36. }
  37. }
  38. #region Structure
  39. public ViewTriangle()
  40. {
  41. objectType = DrawClass.View;
  42. drawToolType = DrawToolType.ViewTriangle;
  43. Rotatable = true;
  44. Initialize();
  45. }
  46. public ViewTriangle(int x, int y) : this()
  47. {
  48. AddPoint(x, y);
  49. AddPoint(x, y);
  50. AddPoint(x, y);
  51. }
  52. public ViewTriangle(List<PointF> points) : this()
  53. {
  54. Points = points;
  55. }
  56. #endregion
  57. #region Handle
  58. public override int HandleCount => 3;
  59. public override PointF GetHandle(int handleNumber)
  60. {
  61. return Points[handleNumber - 1];
  62. }
  63. public override Cursor GetHandleCursor(int handleNumber)
  64. {
  65. switch (handleNumber)
  66. {
  67. case 3:
  68. return m_rotateCursor;
  69. default:
  70. return m_resizeCursor;
  71. }
  72. }
  73. public override void MoveHandleTo(Point point, int handleNumber)
  74. {
  75. MoveHandleTo((PointF)point, handleNumber);
  76. }
  77. public override void MoveHandleTo(PointF point, int handleNumber)
  78. {
  79. if (handleNumber == 3)
  80. MoveRotate(point);
  81. else
  82. MoveCorner(point, handleNumber - 1);
  83. base.MoveHandleTo(point, handleNumber);
  84. }
  85. private void MoveCorner(PointF point, int i)
  86. {
  87. var k = i == 1 ? K : -1 / K;
  88. this.pointArrayList[i] = point;
  89. this.pointArrayList[2] = BasicCalculationHelper.GetDropFeet(this.pointArrayList[1 - i], -1 / k, point);
  90. }
  91. public override void SetK()
  92. {
  93. K = (double)(pointArrayList[1].Y - pointArrayList[2].Y) / (pointArrayList[1].X - pointArrayList[2].X);
  94. }
  95. #endregion
  96. #region Clone
  97. public override DrawObject Clone()
  98. {
  99. return Clone(ISurfaceBox);
  100. }
  101. public override DrawObject Clone(ISurfaceBox surfaceBox)
  102. {
  103. ViewTriangle triange = new ViewTriangle(Points);
  104. triange.ISurfaceBox = surfaceBox;
  105. triange.combineMode = combineMode;
  106. triange.Color = Color;
  107. triange.ID = ID;
  108. FillDrawObjectFields(triange);
  109. return triange;
  110. }
  111. #endregion
  112. }
  113. }