RedrawLine.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. namespace SketchAssistantWPF
  9. {
  10. public class RedrawLine
  11. {
  12. InternalLine LineToRedraw;
  13. Point[] points;
  14. private int radius;
  15. private HashSet<Point>[] detectionZones;
  16. private int finishedIndex;
  17. /// <summary>
  18. /// Constructor of the RedrawLine.
  19. /// </summary>
  20. /// <param name="rad">The radius around each point of the line input will be accepted.</param>
  21. public RedrawLine(int rad)
  22. {
  23. radius = rad;
  24. }
  25. /// <summary>
  26. /// A function to intialize the redraw line.
  27. /// </summary>
  28. /// <param name="line"></param>
  29. /// <returns></returns>
  30. public bool Init(InternalLine line)
  31. {
  32. HashSet<Point> initialZone = GeometryCalculator.FilledCircleAlgorithm(new Point(0, 0), radius);
  33. LineToRedraw = line;
  34. points = line.GetPoints().ToArray();
  35. List<HashSet<Point>> dZones = new List<HashSet<Point>>();
  36. foreach(Point p in points)
  37. {
  38. HashSet<Point> newZone = new HashSet<Point>();
  39. foreach(Point i_p in initialZone)
  40. {
  41. newZone.Add(new Point(i_p.X + p.X, i_p.Y + p.Y));
  42. }
  43. dZones.Add(newZone);
  44. }
  45. detectionZones = dZones.ToArray();
  46. finishedIndex = 0;
  47. Console.WriteLine("This line has {0} points ", line.GetPoints().Count());
  48. return false;
  49. }
  50. public Point GetOverlayPosition()
  51. {
  52. return points[0];
  53. }
  54. public double GetDirection(Point p)
  55. {
  56. if(finishedIndex > points.Length - 1)
  57. {
  58. return -1;
  59. }
  60. if(detectionZones[finishedIndex + 1].Contains(p))
  61. {
  62. finishedIndex++;
  63. }
  64. double angle = 0;
  65. var np = points[finishedIndex + 1];
  66. Vector vector0 = new Vector(1,0);
  67. Vector vector1 = new Vector(np.X-p.X, np.Y - p.Y);
  68. angle = Math.Acos((vector0.X* vector1.X + vector0.Y * vector1.Y) / (vector0.Length * vector1.Length)) / Math.PI * 180;
  69. /*double cross_prod = np.Y - p.Y;
  70. double acute_angle = Math.Atan2(Math.Abs(cross_prod), np.X - p.Y)* 180 /Math.PI;
  71. if (cross_prod < 0) { angle = 360 - acute_angle; }
  72. else { angle = acute_angle; }
  73. */
  74. //TODO: Calculate angles between p and the next n points of the line
  75. // Take average and return it.
  76. return angle;
  77. }
  78. }
  79. }