Line.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. for(int i = 0; i < linePoints.Count - 1 ; i++)
  71. {
  72. canvas.DrawLine(Pens.Black, linePoints[i], linePoints[i + 1]);
  73. }
  74. //If there is only one point
  75. if(linePoints.Count == 1){ canvas.FillRectangle(Brushes.Black, linePoints[0].X, linePoints[0].Y, 1, 1); }
  76. return canvas;
  77. }
  78. /// <summary>
  79. /// A function that will take to matrixes and populate the with the line data of this line object
  80. /// </summary>
  81. /// <param name="boolMatrix">The Matrix of booleans, in which is saved wether there is a line at this position.</param>
  82. /// <param name="listMatrix">The Matrix of Lists of integers, in which is saved which lines are at this position</param>
  83. public void PopulateMatrixes(bool[,] boolMatrix, HashSet<int>[,] listMatrix)
  84. {
  85. if(!isTemporary)
  86. {
  87. foreach (Point currPoint in linePoints)
  88. {
  89. if (currPoint.X >= 0 && currPoint.Y >= 0 &&
  90. currPoint.X < boolMatrix.GetLength(0) && currPoint.Y < boolMatrix.GetLength(1))
  91. {
  92. boolMatrix[currPoint.X, currPoint.Y] = true;
  93. if (listMatrix[currPoint.X, currPoint.Y] == null)
  94. {
  95. listMatrix[currPoint.X, currPoint.Y] = new HashSet<int>();
  96. }
  97. listMatrix[currPoint.X, currPoint.Y].Add(identifier);
  98. }
  99. }
  100. }
  101. }
  102. /// <summary>
  103. /// Removes duplicate points from the line object
  104. /// </summary>
  105. private void CleanPoints()
  106. {
  107. if (linePoints.Count > 1)
  108. {
  109. List<Point> newList = new List<Point>();
  110. List<Point> tempList = new List<Point>();
  111. //Since Point is non-nullable, we must ensure the nullPoints,
  112. //which we remove can not possibly be points of the original given line.
  113. int nullValue = linePoints[0].X + 1;
  114. //Fill the gaps between points
  115. for (int i = 0; i < linePoints.Count - 1; i++)
  116. {
  117. nullValue += linePoints[i + 1].X;
  118. List<Point> partialList = GeometryCalculator.BresenhamLineAlgorithm(linePoints[i], linePoints[i + 1]);
  119. tempList.AddRange(partialList);
  120. }
  121. Point nullPoint = new Point(nullValue, 0);
  122. //Set duplicate points to the null point
  123. for (int i = 1; i < tempList.Count; i++)
  124. {
  125. if ((tempList[i].X == tempList[i - 1].X) && (tempList[i].Y == tempList[i - 1].Y))
  126. {
  127. tempList[i - 1] = nullPoint;
  128. }
  129. }
  130. //remove the null points
  131. foreach (Point tempPoint in tempList)
  132. {
  133. if (tempPoint.X != nullValue)
  134. {
  135. newList.Add(tempPoint);
  136. }
  137. }
  138. linePoints = new List<Point>(newList);
  139. }
  140. }
  141. }
  142. }