Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace kovzanka
- {
- class Program
- {
- static void Main(string[] args)
- {
- #region input
- Globals.rectangle = new Rectangle();
- int[] p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
- Globals.rectangle.v1 = new Point(p[0], p[1]);
- p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
- Globals.rectangle.v2 = new Point(p[0], p[1]);
- p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
- Globals.rectangle.v3 = new Point(p[0], p[1]);
- int N = int.Parse(Console.ReadLine());
- Globals.points = new Point[N];
- for (int i = 0; i < N; i++)
- {
- p = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
- Globals.points[i] = new Point(p[0], p[1]);
- }
- #endregion input
- double max = 0;
- for (int i = 0; i < N; i++)
- {
- double tmp = ShortestDistance(Globals.points[i], Globals.rectangle);
- if (tmp > max)
- {
- max = tmp;
- }
- }
- Console.WriteLine(max);
- }
- public static double ShortestDistance(Point p, Rectangle r)
- {
- double d1 = GetDistanceToSegment(r.v1.X, r.v1.Y, r.v2.X, r.v2.Y, p.X, p.Y);
- double d2 = GetDistanceToSegment(r.v2.X, r.v2.Y, r.v3.X, r.v3.Y, p.X, p.Y);
- double d3 = GetDistanceToSegment(r.v3.X, r.v3.Y, r.v1.X, r.v1.Y, p.X, p.Y);
- return (Math.Min(d1, Math.Min(d2, d3)));
- }
- public static class Globals
- {
- public static Point[] points;
- public static Rectangle rectangle;
- }
- public struct Point
- {
- public int X, Y;
- public Point(int x, int y)
- {
- this.X = x;
- this.Y = y;
- }
- }
- public struct Rectangle
- {
- public Point v1, v2, v3;
- }
- public static double GetDistanceToSegment(double ax, double ay, double bx, double by, double x, double y)
- {
- double ak = Math.Sqrt((x - ax) * (x - ax) + (y - ay) * (y - ay));
- double kb = Math.Sqrt((x - bx) * (x - bx) + (y - by) * (y - by));
- double ab = Math.Sqrt((ax - bx) * (ax - bx) + (ay - by) * (ay - by));
- double mulScalarAKAB = (x - ax) * (bx - ax) + (y - ay) * (by - ay);
- double mulScalarBKAB = (x - bx) * (-bx + ax) + (y - by) * (-by + ay);
- if (ab == 0) return ak;
- else if (mulScalarAKAB >= 0 && mulScalarBKAB >= 0)
- {
- double p = (ak + kb + ab) / 2.0;
- double s = Math.Sqrt(Math.Abs((p * (p - ak) * (p - kb) * (p - ab))));
- return (2.0 * s) / ab;
- }
- else if (mulScalarAKAB < 0 || mulScalarBKAB < 0)
- {
- return Math.Min(ak, kb);
- }
- else return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement