InternalLine.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Media;
  8. namespace SketchAssistantWPF
  9. {
  10. public class InternalLine
  11. {
  12. /// <summary>
  13. /// list saving all the points of the line in the order of the path from start to end point
  14. /// </summary>
  15. private List<Point> linePoints;
  16. /// <summary>
  17. /// unique identifier of this Line object
  18. /// </summary>
  19. private int identifier;
  20. /// <summary>
  21. /// flag showing if this is only a temporary line
  22. /// </summary>
  23. private bool isTemporary;
  24. /// <summary>
  25. /// A collection of the original Points defining the line.
  26. /// </summary>
  27. private PointCollection pointColl;
  28. /// <summary>
  29. /// The constructor for lines which are only temporary.
  30. /// If you want nice lines use the other constructor.
  31. /// </summary>
  32. /// <param name="points">The points of the line</param>
  33. public InternalLine(List<Point> points)
  34. {
  35. linePoints = new List<Point>(points);
  36. pointColl = new PointCollection(linePoints);
  37. isTemporary = true;
  38. }
  39. /// <summary>
  40. /// The constructor for lines, which will be more resource efficient
  41. /// and have the ability to populate deletion matrixes.
  42. /// </summary>
  43. /// <param name="points">The points of the line</param>
  44. /// <param name="id">The identifier of the line</param>
  45. public InternalLine(List<Point> points, int id)
  46. {
  47. linePoints = new List<Point>(points);
  48. pointColl = new PointCollection(linePoints);
  49. identifier = id;
  50. CleanPoints();
  51. isTemporary = false;
  52. }
  53. public Point GetStartPoint()
  54. {
  55. return linePoints.First();
  56. }
  57. public Point GetEndPoint()
  58. {
  59. return linePoints.Last();
  60. }
  61. public List<Point> GetPoints()
  62. {
  63. return linePoints;
  64. }
  65. public int GetID()
  66. {
  67. return identifier;
  68. }
  69. public PointCollection GetPointCollection()
  70. {
  71. return pointColl;
  72. }
  73. /*
  74. /// <summary>
  75. /// A function that takes a Graphics element and returns it with
  76. /// the line drawn on it.
  77. /// </summary>
  78. /// <param name="canvas">The Graphics element on which the line shall be drawn</param>
  79. /// <returns>The given Graphics element with the additional line</returns>
  80. public Graphics DrawLine(Graphics canvas)
  81. {
  82. for (int i = 0; i < linePoints.Count - 1; i++)
  83. {
  84. canvas.DrawLine(Pens.Black, linePoints[i], linePoints[i + 1]);
  85. }
  86. //If there is only one point
  87. if (linePoints.Count == 1) { canvas.FillRectangle(Brushes.Black, linePoints[0].X, linePoints[0].Y, 1, 1); }
  88. return canvas;
  89. }
  90. */
  91. /// <summary>
  92. /// A function that will take to matrixes and populate the with the line data of this line object
  93. /// </summary>
  94. /// <param name="boolMatrix">The Matrix of booleans, in which is saved wether there is a line at this position.</param>
  95. /// <param name="listMatrix">The Matrix of Lists of integers, in which is saved which lines are at this position</param>
  96. public void PopulateMatrixes(bool[,] boolMatrix, HashSet<int>[,] listMatrix)
  97. {
  98. if (!isTemporary)
  99. {
  100. foreach (Point currPoint in linePoints)
  101. {
  102. if (currPoint.X >= 0 && currPoint.Y >= 0 &&
  103. currPoint.X < boolMatrix.GetLength(0) && currPoint.Y < boolMatrix.GetLength(1))
  104. {
  105. boolMatrix[(int) currPoint.X, (int) currPoint.Y] = true;
  106. if (listMatrix[(int) currPoint.X, (int) currPoint.Y] == null)
  107. {
  108. listMatrix[(int) currPoint.X, (int) currPoint.Y] = new HashSet<int>();
  109. }
  110. listMatrix[(int) currPoint.X, (int) currPoint.Y].Add(identifier);
  111. }
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// Removes duplicate points from the line object
  117. /// </summary>
  118. private void CleanPoints()
  119. {
  120. if (linePoints.Count > 1)
  121. {
  122. List<Point> newList = new List<Point>();
  123. List<Point> tempList = new List<Point>();
  124. //Since Point is non-nullable, we must ensure the nullPoints,
  125. //which we remove can not possibly be points of the original given line.
  126. int nullValue = (int) linePoints[0].X + 1;
  127. //Fill the gaps between points
  128. for (int i = 0; i < linePoints.Count - 1; i++)
  129. {
  130. nullValue += (int) linePoints[i + 1].X;
  131. List<Point> partialList = GeometryCalculator.BresenhamLineAlgorithm(linePoints[i], linePoints[i + 1]);
  132. tempList.AddRange(partialList);
  133. }
  134. Point nullPoint = new Point(nullValue, 0);
  135. //Set duplicate points to the null point
  136. for (int i = 1; i < tempList.Count; i++)
  137. {
  138. if ((tempList[i].X == tempList[i - 1].X) && (tempList[i].Y == tempList[i - 1].Y))
  139. {
  140. tempList[i - 1] = nullPoint;
  141. }
  142. }
  143. //remove the null points
  144. foreach (Point tempPoint in tempList)
  145. {
  146. if (tempPoint.X != nullValue)
  147. {
  148. newList.Add(tempPoint);
  149. }
  150. }
  151. linePoints = new List<Point>(newList);
  152. }
  153. }
  154. }
  155. }