Line.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. class Line
  10. {
  11. private List<Point> linePoints;
  12. private int identifier;
  13. public Line(List<Point> points)
  14. {
  15. linePoints = new List<Point>(points);
  16. }
  17. public Line(List<Point> points, int id)
  18. {
  19. linePoints = new List<Point>(points);
  20. identifier = id;
  21. CleanPoints();
  22. }
  23. public Point GetStartPoint()
  24. {
  25. return linePoints.First();
  26. }
  27. public Point GetEndPoint()
  28. {
  29. return linePoints.Last();
  30. }
  31. /// <summary>
  32. /// A function that takes a Graphics element and returns it with
  33. /// the line drawn on it.
  34. /// </summary>
  35. /// <param name="canvas">The Graphics element on which the line shall be drawn</param>
  36. /// <returns>The given Graphics element with the additional line</returns>
  37. public Graphics DrawLine(Graphics canvas)
  38. {
  39. Pen thePen = new Pen(Color.Black);
  40. for(int i = 0; i < linePoints.Count - 1 ; i++)
  41. {
  42. canvas.DrawLine(thePen, linePoints[i], linePoints[i + 1]);
  43. }
  44. return canvas;
  45. }
  46. /// <summary>
  47. /// A function that will take to matrixes and populate the with the line data of this line object
  48. /// Exceptions:
  49. /// Will throw IndexOutOfRangeException if any of the points of this line are
  50. /// outside of the given matrixes.
  51. /// </summary>
  52. /// <param name="boolMatrix">The Matrix of booleans, in which is saved wether there is a line at this position.</param>
  53. /// <param name="listMatrix">The Matrix of Lists of integers, in which is saved which lines are at this position</param>
  54. public void PopulateMatrixes(bool[,] boolMatrix, List<int>[,] listMatrix)
  55. {
  56. foreach(Point currPoint in linePoints)
  57. {
  58. try
  59. {
  60. boolMatrix[currPoint.X, currPoint.Y] = true;
  61. if (listMatrix[currPoint.X, currPoint.Y] == null)
  62. {
  63. listMatrix[currPoint.X, currPoint.Y] = new List<int>();
  64. }
  65. listMatrix[currPoint.X, currPoint.Y].Add(identifier);
  66. }
  67. catch(IndexOutOfRangeException e)
  68. {
  69. }
  70. }
  71. }
  72. /// <summary>
  73. /// Removes duplicate points from the line object
  74. /// </summary>
  75. private void CleanPoints()
  76. {
  77. List<Point> newList = new List<Point>();
  78. Point nullPoint = new Point(-1, -1);
  79. for (int i = 1; i < linePoints.Count; i++)
  80. {
  81. if ((linePoints[i].X == linePoints[i - 1].X) && (linePoints[i].Y == linePoints[i - 1].Y))
  82. {
  83. linePoints[i - 1] = nullPoint;
  84. }
  85. }
  86. foreach(Point linepoint in linePoints)
  87. {
  88. if (!(linepoint.X == -1))
  89. {
  90. newList.Add(linepoint);
  91. }
  92. }
  93. linePoints = newList;
  94. }
  95. }
  96. }