InternalLine.cs 7.1 KB

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