Line.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. namespace SketchAssistant
  8. {
  9. public class Line
  10. {
  11. /// <summary>
  12. /// list saving all the points of the line in the order of the path from start to end point
  13. /// </summary>
  14. private List<Point> linePoints;
  15. /// <summary>
  16. /// unique identifier of this Line object
  17. /// </summary>
  18. private int identifier;
  19. /// <summary>
  20. /// flag showing if this is only a temporary line
  21. /// </summary>
  22. private bool isTemporary;
  23. /// <summary>
  24. /// The constructor for lines which are only temporary.
  25. /// If you want nice lines use the other constructor.
  26. /// </summary>
  27. /// <param name="points">The points of the line</param>
  28. public Line(List<Point> points)
  29. {
  30. linePoints = new List<Point>(points);
  31. isTemporary = true;
  32. }
  33. /// <summary>
  34. /// The constructor for lines, which will be more resource efficient
  35. /// and have the ability to populate deletion matrixes.
  36. /// </summary>
  37. /// <param name="points">The points of the line</param>
  38. /// <param name="id">The identifier of the line</param>
  39. public Line(List<Point> points, int id)
  40. {
  41. linePoints = new List<Point>(points);
  42. identifier = id;
  43. CleanPoints();
  44. isTemporary = false;
  45. }
  46. public Point GetStartPoint()
  47. {
  48. return linePoints.First();
  49. }
  50. public Point GetEndPoint()
  51. {
  52. return linePoints.Last();
  53. }
  54. public List<Point> GetPoints()
  55. {
  56. return linePoints;
  57. }
  58. public int GetID()
  59. {
  60. return identifier;
  61. }
  62. /// <summary>
  63. /// A function that takes a Graphics element and returns it with
  64. /// the line drawn on it.
  65. /// </summary>
  66. /// <param name="canvas">The Graphics element on which the line shall be drawn</param>
  67. /// <returns>The given Graphics element with the additional line</returns>
  68. public Graphics DrawLine(Graphics canvas)
  69. {
  70. Pen thePen = new Pen(Color.Black);
  71. for(int i = 0; i < linePoints.Count - 1 ; i++)
  72. {
  73. canvas.DrawLine(thePen, linePoints[i], linePoints[i + 1]);
  74. }
  75. //If there is only one point
  76. if(linePoints.Count == 1){ canvas.FillRectangle(Brushes.Black, linePoints[0].X, linePoints[0].Y, 1, 1); }
  77. return canvas;
  78. }
  79. /// <summary>
  80. /// A function that will take to matrixes and populate the with the line data of this line object
  81. /// </summary>
  82. /// <param name="boolMatrix">The Matrix of booleans, in which is saved wether there is a line at this position.</param>
  83. /// <param name="listMatrix">The Matrix of Lists of integers, in which is saved which lines are at this position</param>
  84. public void PopulateMatrixes(bool[,] boolMatrix, HashSet<int>[,] listMatrix)
  85. {
  86. if(!isTemporary)
  87. {
  88. foreach (Point currPoint in linePoints)
  89. {
  90. if (currPoint.X >= 0 && currPoint.Y >= 0 &&
  91. currPoint.X < boolMatrix.GetLength(0) && currPoint.Y < boolMatrix.GetLength(1))
  92. {
  93. boolMatrix[currPoint.X, currPoint.Y] = true;
  94. if (listMatrix[currPoint.X, currPoint.Y] == null)
  95. {
  96. listMatrix[currPoint.X, currPoint.Y] = new HashSet<int>();
  97. }
  98. listMatrix[currPoint.X, currPoint.Y].Add(identifier);
  99. }
  100. }
  101. }
  102. }
  103. /// <summary>
  104. /// Removes duplicate points from the line object
  105. /// </summary>
  106. private void CleanPoints()
  107. {
  108. if (linePoints.Count > 1)
  109. {
  110. List<Point> newList = new List<Point>();
  111. List<Point> tempList = new List<Point>();
  112. //Since Point is non-nullable, we must ensure the nullPoints,
  113. //which we remove can not possibly be points of the original given line.
  114. int nullValue = linePoints[0].X + 1;
  115. //Fill the gaps between points
  116. for (int i = 0; i < linePoints.Count - 1; i++)
  117. {
  118. nullValue += linePoints[i + 1].X;
  119. List<Point> partialList = BresenhamLineAlgorithm(linePoints[i], linePoints[i + 1]);
  120. tempList.AddRange(partialList);
  121. }
  122. Point nullPoint = new Point(nullValue, 0);
  123. //Set duplicate points to the null point
  124. for (int i = 1; i < tempList.Count; i++)
  125. {
  126. if ((tempList[i].X == tempList[i - 1].X) && (tempList[i].Y == tempList[i - 1].Y))
  127. {
  128. tempList[i - 1] = nullPoint;
  129. }
  130. }
  131. //remove the null points
  132. foreach (Point tempPoint in tempList)
  133. {
  134. if (tempPoint.X != nullValue)
  135. {
  136. newList.Add(tempPoint);
  137. }
  138. }
  139. linePoints = new List<Point>(newList);
  140. }
  141. }
  142. /// <summary>
  143. /// An implementation of the Bresenham Line Algorithm,
  144. /// which calculates all points between two points in a straight line.
  145. /// Implemented using the pseudocode on Wikipedia.
  146. /// </summary>
  147. /// <param name="p0">The start point</param>
  148. /// <param name="p1">The end point</param>
  149. /// <returns>All points between p0 and p1 (including p0 and p1)</returns>
  150. public static List<Point> BresenhamLineAlgorithm(Point p0, Point p1)
  151. {
  152. int deltaX = p1.X - p0.X;
  153. int deltaY = p1.Y - p0.Y;
  154. List<Point> returnList;
  155. if (Math.Abs(deltaY) < Math.Abs(deltaX))
  156. {
  157. if(p0.X > p1.X)
  158. {
  159. returnList = GetLineLow(p1.X, p1.Y, p0.X, p0.Y);
  160. returnList.Reverse();
  161. }
  162. else
  163. {
  164. returnList = GetLineLow(p0.X, p0.Y, p1.X, p1.Y);
  165. }
  166. }
  167. else
  168. {
  169. if (p0.Y > p1.Y)
  170. {
  171. returnList = GetLineHigh(p1.X, p1.Y, p0.X, p0.Y);
  172. returnList.Reverse();
  173. }
  174. else
  175. {
  176. returnList = GetLineHigh(p0.X, p0.Y, p1.X, p1.Y);
  177. }
  178. }
  179. return returnList;
  180. }
  181. /// <summary>
  182. /// Helping function of the Bresenham Line algorithm,
  183. /// under the assumption that abs(deltaY) is smaller than abs(deltX)
  184. /// and x0 is smaller than x1
  185. /// </summary>
  186. /// <param name="x0">x value of point 0</param>
  187. /// <param name="y0">y value of point 0</param>
  188. /// <param name="x1">x value of point 1</param>
  189. /// <param name="y1">y value of point 1</param>
  190. /// <returns>All points on the line between the two points</returns>
  191. private static List<Point> GetLineLow(int x0, int y0, int x1, int y1)
  192. {
  193. List<Point> returnList = new List<Point>();
  194. int dx = x1 - x0;
  195. int dy = y1 - y0;
  196. int yi = 1;
  197. if(dy < 0)
  198. {
  199. yi = -1;
  200. dy = -dy;
  201. }
  202. int D = 2 * dy - dx;
  203. int y = y0;
  204. for (int x = x0; x <= x1; x++)
  205. {
  206. returnList.Add(new Point(x, y));
  207. if (D > 0)
  208. {
  209. y = y + yi;
  210. D = D - 2 * dx;
  211. }
  212. D = D + 2 * dy;
  213. }
  214. return returnList;
  215. }
  216. /// <summary>
  217. /// Helping function of the Bresenham Line algorithm,
  218. /// under the assumption that abs(deltaY) is larger or equal than abs(deltX)
  219. /// and y0 is smaller than y1
  220. /// </summary>
  221. /// <param name="x0">x value of point 0</param>
  222. /// <param name="y0">y value of point 0</param>
  223. /// <param name="x1">x value of point 1</param>
  224. /// <param name="y1">y value of point 1</param>
  225. /// <returns>All points on the line between the two points</returns>
  226. private static List<Point> GetLineHigh(int x0, int y0, int x1, int y1)
  227. {
  228. List<Point> returnList = new List<Point>();
  229. int dx = x1 - x0;
  230. int dy = y1 - y0;
  231. int xi = 1;
  232. if (dx < 0)
  233. {
  234. xi = -1;
  235. dx = -dx;
  236. }
  237. int D = 2 * dx - dy;
  238. int x = x0;
  239. for (int y = y0; y <= y1; y++)
  240. {
  241. returnList.Add(new Point(x, y));
  242. if (D > 0)
  243. {
  244. x = x + xi;
  245. D = D - 2 * dy;
  246. }
  247. D = D + 2 * dx;
  248. }
  249. return returnList;
  250. }
  251. }
  252. }