DrawPolygon.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.SettingModel;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Globalization;
  8. using System.Windows.Forms;
  9. namespace PaintDotNet.Annotation.Label
  10. {
  11. using static PaintDotNet.Base.SettingModel.LabelStyleModel;
  12. using PointList = List<PointF>;
  13. /// <summary>
  14. /// 标注->多边形->多边形
  15. /// </summary>
  16. public class DrawPolygon : DrawObject
  17. {
  18. /// <summary>
  19. /// 鼠标形状
  20. /// </summary>
  21. private static Cursor handleCursor = new Cursor(PdnResources.GetResourceStream("Cursors.AnnotationPolyHandle.cur"));
  22. /// <summary>
  23. /// 点集合
  24. /// </summary>
  25. private PointList pointArray;
  26. /// <summary>
  27. /// 标注样式信息model
  28. /// </summary>
  29. public PolygonPolygon polygonStyle;
  30. private RectangleF rectangleF;
  31. public DrawPolygon() : base()
  32. {
  33. this.objectType = DrawClass.Label;
  34. this.drawToolType = DrawToolType.DrawPolygon;
  35. pointArray = new PointList();
  36. Initialize();
  37. }
  38. public DrawPolygon(ISurfaceBox surfaceBox, int x1, int y1) : base()
  39. {
  40. this.objectType = DrawClass.Label;
  41. this.drawToolType = DrawToolType.DrawPolygon;
  42. polygonStyle = surfaceBox.GetLabelStyleModel().polygonPolygon;
  43. pointArray = new PointList();
  44. startPoint.X = x1;
  45. startPoint.Y = y1;
  46. pointArray.Add(new PointF(x1, y1));
  47. Initialize();
  48. }
  49. public DrawPolygon(ISurfaceBox surfaceBox, List<PointF> points, ParentStyleModel parentStyleModel, Object content) : base()
  50. {
  51. this.objectType = DrawClass.Label;
  52. this.drawToolType = DrawToolType.DrawPolygon;
  53. this.ISurfaceBox = surfaceBox;
  54. polygonStyle = (PolygonPolygon)parentStyleModel;
  55. pointArray = points;
  56. startPoint = points[points.Count - 2];
  57. endPoint = points[points.Count - 1];
  58. }
  59. /// <summary>
  60. /// Clone this instance
  61. /// </summary>
  62. public override DrawObject Clone()
  63. {
  64. DrawPolygon drawPolygon = new DrawPolygon();
  65. drawPolygon.objectType = DrawClass.Label;
  66. drawPolygon.drawToolType = DrawToolType.DrawPolygon;
  67. drawPolygon.ISurfaceBox = this.ISurfaceBox;
  68. foreach (PointF p in this.pointArray)
  69. {
  70. drawPolygon.pointArray.Add(p);
  71. }
  72. drawPolygon.polygonStyle = this.polygonStyle;
  73. drawPolygon.startPoint = this.startPoint;
  74. drawPolygon.endPoint = this.endPoint;
  75. FillDrawObjectFields(drawPolygon);
  76. return drawPolygon;
  77. }
  78. public override DrawObject Clone(ISurfaceBox surfaceBox)
  79. {
  80. DrawPolygon drawPolygon = new DrawPolygon();
  81. drawPolygon.objectType = DrawClass.Label;
  82. drawPolygon.drawToolType = DrawToolType.DrawPolygon;
  83. drawPolygon.ISurfaceBox = surfaceBox;
  84. foreach (PointF p in this.pointArray)
  85. {
  86. drawPolygon.pointArray.Add(p);
  87. }
  88. drawPolygon.startPoint = this.startPoint;
  89. drawPolygon.endPoint = this.endPoint;
  90. drawPolygon.polygonStyle = this.polygonStyle;
  91. FillDrawObjectFields(drawPolygon);
  92. return drawPolygon;
  93. }
  94. public override void Draw(Graphics g)
  95. {
  96. if (HandleCount >= 3)
  97. {
  98. g.SmoothingMode = SmoothingMode.AntiAlias;
  99. Color color = Color.FromArgb(this.polygonStyle.lineColor);
  100. Pen pen = new Pen(color, PenWidth);
  101. int penWidth = this.polygonStyle.lineWidth;
  102. pen.DashStyle = (DashStyle)this.polygonStyle.lineStyle;
  103. pen.Width = penWidth;
  104. Color fillColor = Color.FromArgb(this.polygonStyle.fillColor);
  105. SolidBrush fillBrush = new SolidBrush(fillColor);
  106. g.FillPolygon(fillBrush, pointArray.ToArray());
  107. g.DrawPolygon(pen, pointArray.ToArray());
  108. pen.Dispose();
  109. fillBrush.Dispose();
  110. }
  111. }
  112. public void AddPoint(PointF point)
  113. {
  114. pointArray.Add(point);
  115. }
  116. public override int HandleCount
  117. {
  118. get
  119. {
  120. return pointArray.Count;
  121. }
  122. }
  123. /// <summary>
  124. /// Get handle pointscroll by 1-based number
  125. /// </summary>
  126. /// <param name="handleNumber"></param>
  127. /// <returns></returns>
  128. public override PointF GetHandle(int handleNumber)
  129. {
  130. if (handleNumber < 1)
  131. handleNumber = 1;
  132. if (handleNumber > pointArray.Count)
  133. handleNumber = pointArray.Count;
  134. return pointArray[handleNumber - 1];
  135. }
  136. public override Cursor GetHandleCursor(int handleNumber)
  137. {
  138. return handleCursor;
  139. }
  140. public override void MoveHandleTo(Point point, int handleNumber)
  141. {
  142. if (handleNumber < 1)
  143. handleNumber = 1;
  144. if (handleNumber > pointArray.Count)
  145. handleNumber = pointArray.Count;
  146. pointArray[handleNumber - 1] = point;
  147. }
  148. public override void Move(int deltaX, int deltaY)
  149. {
  150. int n = pointArray.Count;
  151. PointF point;
  152. for (int i = 0; i < n; i++)
  153. {
  154. point = new PointF(pointArray[i].X + ISurfaceBox.UnscaleScalar(deltaX), pointArray[i].Y + ISurfaceBox.UnscaleScalar(deltaY));
  155. pointArray[i] = point;
  156. }
  157. }
  158. /// <summary>
  159. /// Draw tracker for selected object
  160. /// </summary>
  161. /// <param name="g"></param>
  162. public override void DrawTracker(Graphics g)
  163. {
  164. if (!Selected)
  165. return;
  166. SolidBrush brush = new SolidBrush(Color.Black);
  167. for (int i = 1; i <= HandleCount; i++)
  168. {
  169. g.FillRectangle(brush, GetHandleRectangle(i));
  170. }
  171. brush.Dispose();
  172. rectangleF = GetBoundingBox();
  173. g.DrawRectangle(new Pen(Color.White), rectangleF.X, rectangleF.Y, rectangleF.Width, rectangleF.Height);
  174. }
  175. /// <summary>
  176. /// Hit test.
  177. /// Return value: -1 - no hit
  178. /// 0 - hit anywhere
  179. /// > 1 - handle number
  180. /// </summary>
  181. /// <param name="pointscroll"></param>
  182. /// <returns></returns>
  183. public override int HitTest(Point point)
  184. {
  185. if (Selected)
  186. {
  187. for (int i = 1; i <= HandleCount; i++)
  188. {
  189. if (GetHandleRectangle(i).Contains(point))
  190. return i;
  191. }
  192. }
  193. if (PointInObject(point))
  194. return 0;
  195. return -1;
  196. }
  197. protected override bool PointInObject(Point point)
  198. {
  199. int counter = 0;
  200. int i;
  201. double xinters;
  202. PointF p1, p2;
  203. int pointCount = pointArray.Count;
  204. p1 = pointArray[0];
  205. for (i = 1; i <= pointCount; i++)
  206. {
  207. p2 = pointArray[i % pointCount];
  208. if (point.Y > Math.Min(p1.Y, p2.Y)//校验点的Y大于线段端点的最小Y
  209. && point.Y <= Math.Max(p1.Y, p2.Y))//校验点的Y小于线段端点的最大Y
  210. {
  211. if (point.X <= Math.Max(p1.X, p2.X))//校验点的X小于等线段端点的最大X(使用校验点的左射线判断).
  212. {
  213. if (p1.Y != p2.Y)//线段不平行于X轴
  214. {
  215. xinters = (point.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
  216. if (p1.X == p2.X || point.X <= xinters)
  217. {
  218. counter++;
  219. }
  220. }
  221. }
  222. }
  223. p1 = p2;
  224. }
  225. if (counter % 2 == 0)
  226. {
  227. return false;
  228. }
  229. else
  230. {
  231. return true;
  232. }
  233. }
  234. public override bool IntersectsWith(Rectangle rectangle)
  235. {
  236. return rectangleF.IntersectsWith(rectangle);
  237. }
  238. public override RectangleF GetBoundingBox()
  239. {
  240. RectangleF rectangle;
  241. float minx = 0, maxx = 0, miny = 0, maxy = 0;
  242. for (int i = 0; i < pointArray.Count; i++)
  243. {
  244. if (i == 0)
  245. {
  246. minx = maxx = pointArray[i].X;
  247. miny = maxy = pointArray[i].Y;
  248. }
  249. else
  250. {
  251. if (pointArray[i].X > maxx) maxx = pointArray[i].X;
  252. if (pointArray[i].X < minx) minx = pointArray[i].X;
  253. if (pointArray[i].Y > maxy) maxy = pointArray[i].Y;
  254. if (pointArray[i].Y < miny) miny = pointArray[i].Y;
  255. }
  256. }
  257. rectangle = new RectangleF(minx, miny, maxx - minx, maxy - miny);
  258. return rectangle;
  259. }
  260. internal void setNextPoint(PointF p)
  261. {
  262. AddPoint(p);
  263. startPoint = endPoint;
  264. endPoint = p;
  265. }
  266. internal void setEndPoint(PointF p)
  267. {
  268. endPoint = p;
  269. }
  270. public override List<PointF> GetPoints()
  271. {
  272. return pointArray;
  273. }
  274. public override ParentStyleModel GetStyle()
  275. {
  276. return polygonStyle;
  277. }
  278. }
  279. }