InternalLine.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /// Indicates if this is a single point.
  30. /// </summary>
  31. public bool isPoint { get; private set; }
  32. /// <summary>
  33. /// The location of the point, if this is a point
  34. /// </summary>
  35. public Point point { get; private set; }
  36. /// <summary>
  37. /// The constructor for lines which are only temporary.
  38. /// If you want nice lines use the other constructor.
  39. /// </summary>
  40. /// <param name="points">The points of the line</param>
  41. public InternalLine(List<Point> points)
  42. {
  43. linePoints = new List<Point>(points);
  44. pointColl = new PointCollection(linePoints);
  45. isTemporary = true;
  46. }
  47. /// <summary>
  48. /// The constructor for lines, which will be more resource efficient
  49. /// and have the ability to populate deletion matrixes.
  50. /// </summary>
  51. /// <param name="points">The points of the line</param>
  52. /// <param name="id">The identifier of the line</param>
  53. public InternalLine(List<Point> points, int id)
  54. {
  55. linePoints = new List<Point>(points);
  56. pointColl = new PointCollection(linePoints);
  57. identifier = id;
  58. CleanPoints();
  59. isTemporary = false;
  60. }
  61. public Point GetStartPoint()
  62. {
  63. return linePoints.First();
  64. }
  65. public Point GetEndPoint()
  66. {
  67. return linePoints.Last();
  68. }
  69. public List<Point> GetPoints()
  70. {
  71. return linePoints;
  72. }
  73. public int GetID()
  74. {
  75. return identifier;
  76. }
  77. public PointCollection GetPointCollection()
  78. {
  79. return pointColl;
  80. }
  81. /// <summary>
  82. /// A function that will take two matrixes and populate them with the line data of this line object
  83. /// </summary>
  84. /// <param name="boolMatrix">The Matrix of booleans, in which is saved wether there is a line at this position.</param>
  85. /// <param name="listMatrix">The Matrix of Lists of integers, in which is saved which lines are at this position</param>
  86. public void PopulateMatrixes(bool[,] boolMatrix, HashSet<int>[,] listMatrix)
  87. {
  88. if (!isTemporary)
  89. {
  90. foreach (Point currPoint in linePoints)
  91. {
  92. if (currPoint.X >= 0 && currPoint.Y >= 0 &&
  93. currPoint.X < boolMatrix.GetLength(0) && currPoint.Y < boolMatrix.GetLength(1))
  94. {
  95. boolMatrix[(int) currPoint.X, (int) currPoint.Y] = true;
  96. if (listMatrix[(int) currPoint.X, (int) currPoint.Y] == null)
  97. {
  98. listMatrix[(int) currPoint.X, (int) currPoint.Y] = new HashSet<int>();
  99. }
  100. listMatrix[(int) currPoint.X, (int) currPoint.Y].Add(identifier);
  101. }
  102. }
  103. }
  104. }
  105. /// <summary>
  106. /// Removes duplicate points from the line object
  107. /// </summary>
  108. private void CleanPoints()
  109. {
  110. if (linePoints.Count > 1)
  111. {
  112. //if this is a point or not
  113. var localIsPoint = true;
  114. //check if its a point
  115. foreach(Point p in linePoints)
  116. {
  117. if (p.X != linePoints[0].X || p.Y != linePoints[0].Y)
  118. localIsPoint = false;
  119. }
  120. if (!localIsPoint) {
  121. List<Point> newList = new List<Point>();
  122. List<Point> tempList = new List<Point>();
  123. //Since Point is non-nullable, we must ensure the nullPoints,
  124. //which we remove can not possibly be points of the original given line.
  125. int nullValue = (int) linePoints[0].X + 1;
  126. //Fill the gaps between points
  127. for (int i = 0; i < linePoints.Count - 1; i++)
  128. {
  129. nullValue += (int) linePoints[i + 1].X;
  130. List<Point> partialList = GeometryCalculator.BresenhamLineAlgorithm(linePoints[i], linePoints[i + 1]);
  131. tempList.AddRange(partialList);
  132. }
  133. Point nullPoint = new Point(nullValue, 0);
  134. //Set duplicate points to the null point
  135. for (int i = 1; i < tempList.Count; i++)
  136. {
  137. if ((tempList[i].X == tempList[i - 1].X) && (tempList[i].Y == tempList[i - 1].Y))
  138. {
  139. tempList[i - 1] = nullPoint;
  140. }
  141. }
  142. //remove the null points
  143. foreach (Point tempPoint in tempList)
  144. {
  145. if (tempPoint.X != nullValue)
  146. {
  147. newList.Add(tempPoint);
  148. }
  149. }
  150. linePoints = new List<Point>(newList);
  151. }
  152. else
  153. {
  154. isPoint = true;
  155. point = linePoints[0];
  156. linePoints = new List<Point>();
  157. linePoints.Add(point);
  158. }
  159. }
  160. }
  161. }
  162. }