Advertisement
Guest User

kovzanka

a guest
May 27th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. using System;
  2.  
  3. namespace kovzanka
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. #region input
  10. Globals.rectangle = new Rectangle();
  11. int[] p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
  12. Globals.rectangle.v1 = new Point(p[0], p[1]);
  13.  
  14. p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
  15. Globals.rectangle.v2 = new Point(p[0], p[1]);
  16.  
  17. p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
  18. Globals.rectangle.v3 = new Point(p[0], p[1]);
  19.  
  20.  
  21. int N = int.Parse(Console.ReadLine());
  22. Globals.points = new Point[N];
  23.  
  24. for (int i = 0; i < N; i++)
  25. {
  26. p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
  27. Globals.points[i] = new Point(p[0], p[1]);
  28. }
  29. #endregion input
  30.  
  31. double max = 0;
  32.  
  33. for (int i = 0; i < N; i++)
  34. {
  35. double tmp = ShortestDistance(Globals.points[i], Globals.rectangle);
  36. if (tmp > max)
  37. {
  38. max = tmp;
  39. }
  40. }
  41.  
  42. Console.WriteLine(max);
  43. }
  44.  
  45. public static double ShortestDistance(Point p, Rectangle r)
  46. {
  47. double d1 = GetDistanceToSegment(r.v1.X, r.v1.Y, r.v2.X, r.v2.Y, p.X, p.Y);
  48. double d2 = GetDistanceToSegment(r.v2.X, r.v2.Y, r.v3.X, r.v3.Y, p.X, p.Y);
  49. double d3 = GetDistanceToSegment(r.v3.X, r.v3.Y, r.v1.X, r.v1.Y, p.X, p.Y);
  50. return (Math.Min(d1, Math.Min(d2, d3)));
  51. }
  52.  
  53. public static class Globals
  54. {
  55. public static Point[] points;
  56. public static Rectangle rectangle;
  57. }
  58.  
  59. public struct Point
  60. {
  61. public int X, Y;
  62. public Point(int x, int y)
  63. {
  64. this.X = x;
  65. this.Y = y;
  66. }
  67. }
  68.  
  69. public struct Rectangle
  70. {
  71. public Point v1, v2, v3;
  72. }
  73.  
  74. public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
  75. {
  76. double ak = Math.Sqrt((x - ax) * (x - ax) + (y - ay) * (y - ay));
  77. double kb = Math.Sqrt((x - bx) * (x - bx) + (y - by) * (y - by));
  78. double ab = Math.Sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
  79.  
  80. double mulScalarAKAB = (x - ax) * (bx - ax) + (y - ay) * (by - ay);
  81. double mulScalarBKAB = (x - bx) * (-bx + ax) + (y - by) * (-by + ay);
  82.  
  83.  
  84. if (ab == 0) return ak;
  85.  
  86. else if (mulScalarAKAB >= 0 && mulScalarBKAB >= 0)
  87. {
  88.  
  89. double p = (ak + kb + ab) / 2.0;
  90. double s = Math.Sqrt(Math.Abs((p * (p - ak) * (p - kb) * (p - ab))));
  91.  
  92. return (2.0 * s) / ab;
  93. }
  94.  
  95. else if (mulScalarAKAB < 0 || mulScalarBKAB < 0)
  96. {
  97. return Math.Min(ak, kb);
  98. }
  99.  
  100. else return 0;
  101. }
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement