Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Gramahm2
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. //var points = new List<Point>
  14. //{
  15. // new Point { X=1.9, Y=4.3, Name="A" },
  16. // new Point { X=2.1, Y=8.4, Name="B" },
  17. // new Point { X=2.8, Y=5.3, Name="C" },
  18. // new Point { X=3.2, Y=10.7, Name="D" },
  19. // new Point { X=3.3, Y=3.1, Name="E" },
  20. // new Point { X=8.5, Y=9.9, Name="F" },
  21. // new Point { X=6, Y=3, Name="G" },
  22. // new Point { X=0.1, Y=0.1, Name="H" },
  23. //};
  24.  
  25. //Bugged
  26. var points = new List<Point>
  27. {
  28. new Point { X=1, Y=1, Name="A" },
  29. new Point { X=3, Y=1, Name="B" },
  30. new Point { X=5, Y=1, Name="C" },
  31. new Point { X=7, Y=1, Name="D" },
  32. new Point { X=7, Y=3, Name="E" },
  33. new Point { X=7, Y=5, Name="F" },
  34. new Point { X=7, Y=7, Name="G" },
  35. new Point { X=5, Y=5, Name="H" },
  36. new Point { X=3, Y=3, Name="I" },
  37. };
  38.  
  39. var p0value = points.Min(p => p.Y);
  40. var p0 = points.First(p => p.Y == p0value);
  41.  
  42. Console.WriteLine("P0:");
  43. Console.WriteLine(p0);
  44. Console.WriteLine();
  45.  
  46. var sortedByAngle = points.OrderBy(p => Math.Atan2(p.Y - p0.Y, p.X - p0.X));
  47.  
  48. Console.WriteLine("Sorted by angle:");
  49. foreach (var p in sortedByAngle)
  50. Console.WriteLine(p);
  51. Console.WriteLine();
  52.  
  53. foreach (var p in sortedByAngle)
  54. p.distanceFromP0 = p.Distance(p0);
  55.  
  56. for(var i = 0; i < sortedByAngle.Count()-1; i++)
  57. {
  58. var next = sortedByAngle.ElementAt(i + 1);
  59.  
  60. if (next == sortedByAngle.ElementAt(sortedByAngle.Count() - 1))
  61. continue;
  62.  
  63. if(sortedByAngle.ElementAt(i).)
  64. }
  65.  
  66. /*
  67. // remove duplicate angles'
  68. for (auto& p : sorted_by_angle)
  69. p->distance_from_p0 = p->distance(p0);
  70.  
  71.  
  72. for (auto it = sorted_by_angle.begin(); it != sorted_by_angle.end(); it++)
  73. {
  74. auto next = std::next(it);
  75. if (next == sorted_by_angle.end())
  76. continue;
  77.  
  78. if ((*it)->angle != (*next)->angle)
  79. continue;
  80.  
  81. auto duplication_end = next;
  82. while (next != sorted_by_angle.end() && (*next)->angle == (*it)->angle)
  83. duplication_end = next++;
  84.  
  85. std::sort(it, duplication_end+1, by_distance(p0));
  86.  
  87. it = duplication_end;
  88. }
  89.  
  90. auto it = sorted_by_angle.begin();
  91. while (it != sorted_by_angle.end())
  92. {
  93. if ((it + 1) == sorted_by_angle.end())
  94. break;
  95.  
  96. if ((*it)->angle == (*(it+1))->angle)
  97. {
  98. sorted_by_angle.erase(it + 1);
  99. }
  100. else ++it;
  101. }
  102.  
  103. //std::sort(sorted_by_angle.begin(), sorted_by_angle.end(), by_distance(p0));
  104.  
  105. printf("Sorted by angle w/o duplicates:\n");
  106. for (auto& p : sorted_by_angle)
  107. p->print();
  108.  
  109. */
  110.  
  111. Stack<Point> output = new Stack<Point>();
  112. output.Push(sortedByAngle.ElementAt(0));
  113. output.Push(sortedByAngle.ElementAt(1));
  114. output.Push(sortedByAngle.ElementAt(2));
  115.  
  116. for (int i = 3; i < sortedByAngle.Count(); i++)
  117. {
  118. Point top = output.Peek();
  119. output.Pop();
  120.  
  121. while (output.Count() > 0 && Point.Sign(output.Peek(), top, sortedByAngle.ElementAt(i)) <= 0)
  122. {
  123. top = output.Peek();
  124. output.Pop();
  125. }
  126.  
  127. output.Push(top);
  128. output.Push(sortedByAngle.ElementAt(i));
  129. }
  130.  
  131. output.Push(p0);
  132.  
  133. Console.WriteLine("Otoczka:");
  134. foreach (var p in output)
  135. Console.WriteLine(p);
  136.  
  137. Console.ReadKey();
  138. }
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement