Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.04 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import java.util.InputMismatchException;
  4.  
  5. public class segmentintersection {
  6.     public static void main(String[] args) {
  7.         StringBuilder sb = new StringBuilder(1000);
  8.         InputReader in = new InputReader(System.in);
  9.         for (int tests = in.nextInt(); tests > 0; tests--) {
  10.             LineSegment ls1 = new LineSegment(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
  11.             LineSegment ls2 = new LineSegment(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
  12.             sb.append(ls1.getAnswer(ls2));
  13.             sb.append('\n');
  14.         }
  15.         System.out.println(sb.toString());
  16.     }
  17.  
  18.     static class LineSegment {
  19.         private static final int INF = Integer.MAX_VALUE;
  20.         public static final double EPS = 0.000000001;
  21.         public final long x1, y1, x2, y2;
  22.         public final long a, b, c;
  23.  
  24.         LineSegment(int x1, int y1, int x2, int y2) {
  25.             if (x1 < x2 || x1 == x2 && y1 < y2) {
  26.                 this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
  27.             } else {
  28.                 this.x1 = x2; this.x2 = x1; this.y1 = y2; this.y2 = y1;
  29.             }
  30.             long ta = y2-y1, tb = x1-x2, tc = -ta*x1-tb*y1;
  31.             if (ta == 0 && tb == 0) { // point
  32.                 a = INF; b = INF; c = INF;
  33.                 return;
  34.             }
  35.  
  36.             long g = gcd(gcd(ta, tb), tc);
  37.             ta /= g; tb /= g; tc /= g;
  38.             int sign = ta < 0 || ta == 0 && tb < 0 ? -1 : 1;
  39.             a = ta*sign; b = tb*sign; c = tc*sign;
  40.         }
  41.  
  42.         private boolean checkBounds(LineSegment a, LineSegment b) {
  43.             boolean x = Math.max(a.x1, b.x1) <= Math.min(a.x2, b.x2);
  44.             return x && Math.max(Math.min(a.y1, a.y2),Math.min(b.y1, b.y2)) <= Math.min(Math.max(a.y1, a.y2),Math.max(b.y1, b.y2));
  45.         }
  46.  
  47.         public String getAnswer(LineSegment o) {
  48.             if (!checkBounds(this, o)) return "none";
  49.             if (a == INF && o.a == INF) //if both are points
  50.                 return x1==o.x1 && y1 ==o.y1 ? getPrint(x1,y1) : "none";
  51.             if (a == INF) return o.a*x1 + o.b*y1 == -o.c ? getPrint(x1,y1) : "none";
  52.             if (o.a == INF) return a*o.x1 + b*o.y1 == -c ? getPrint(o.x1,o.y1) : "none";
  53.             if (a == o.a && b == o.b) {//if they are parallel
  54.                 if (c != o.c) return "none"; // 0 0 -5 -5 -2 -2 10 10 -> -2 -2 0 0
  55.                 boolean swap = x1 < o.x1 || x1 == o.x1 && y1 < o.y1;
  56.                 LineSegment low = swap ? this : o;
  57.                 LineSegment high = swap ? o : this;
  58.                 if (low.x2 < high.x1 || low.x2 == high.x1 && low.y2 < high.y1) return "none";
  59.                 if (low.x2 == high.x1 && low.y2 == high.y1) return getPrint(low.x2,low.y2);
  60.                 String end = high.x2 < low.x2 || high.x2 == low.x2 && high.y2 < low.y2 ? getPrint(high.x2, high.y2) : getPrint(low.x2, low.y2);
  61.                 return getPrint(high.x1,high.y1) + " " + end;
  62.             }
  63.             double i = (double)a * o.b - (double)o.a * b;
  64.             double x = -((double)c*o.b - (double)o.c*b)/ i;
  65.             double y = -((double)a*o.c - (double)o.a*c)/ i;
  66.             boolean inX = x >= o.x1-EPS && x <= o.x2+EPS && x >= x1-EPS && x <= x2+EPS;
  67.             boolean inY = y >= Math.min(o.y1, o.y2)-EPS && y <= Math.max(o.y1, o.y2)+EPS && y >= Math.min(y1, y2)-EPS && y <= Math.max(y1, y2)+EPS;
  68.             return inX && inY ? getPrint(x, y) : "none";
  69.         }
  70.         private String getPrint(double x, double y) {
  71.             return String.format("%.2f %.2f", x == -0 ? 0:x, y == -0 ? 0:y);
  72.         }
  73.  
  74.         private long gcd(long a, long b) {
  75.             return a == 0 ? b : gcd(b%a, a);
  76.         }
  77.     }
  78.     static class InputReader {
  79.         private final InputStream stream;
  80.         private final byte[] buf = new byte[8192];
  81.         private int curChar, snumChars;
  82.         private SpaceCharFilter filter;
  83.  
  84.         public InputReader(InputStream stream) {
  85.             this.stream = stream;
  86.         }
  87.  
  88.         public int snext() {
  89.             if (snumChars == -1)
  90.                 throw new InputMismatchException();
  91.             if (curChar >= snumChars) {
  92.                 curChar = 0;
  93.                 try {
  94.                     snumChars = stream.read(buf);
  95.                 } catch (IOException e) {
  96.                     throw new InputMismatchException();
  97.                 }
  98.                 if (snumChars <= 0)
  99.                     return -1;
  100.             }
  101.             return buf[curChar++];
  102.         }
  103.  
  104.         public int nextInt() {
  105.             int c = snext();
  106.             while (isSpaceChar(c)) {
  107.                 c = snext();
  108.             }
  109.             int sgn = 1;
  110.             if (c == '-') {
  111.                 sgn = -1;
  112.                 c = snext();
  113.             }
  114.             int res = 0;
  115.             do {
  116.                 if (c < '0' || c > '9')
  117.                     throw new InputMismatchException();
  118.                 res *= 10;
  119.                 res += c - '0';
  120.                 c = snext();
  121.             } while (!isSpaceChar(c));
  122.             return res * sgn;
  123.         }
  124.  
  125.         public long nextLong() {
  126.             int c = snext();
  127.             while (isSpaceChar(c)) {
  128.                 c = snext();
  129.             }
  130.             int sgn = 1;
  131.             if (c == '-') {
  132.                 sgn = -1;
  133.                 c = snext();
  134.             }
  135.             long res = 0;
  136.             do {
  137.                 if (c < '0' || c > '9')
  138.                     throw new InputMismatchException();
  139.                 res *= 10;
  140.                 res += c - '0';
  141.                 c = snext();
  142.             } while (!isSpaceChar(c));
  143.             return res * sgn;
  144.         }
  145.  
  146.         public int[] nextIntArray(int n) {
  147.             int a[] = new int[n];
  148.             for (int i = 0; i < n; i++) {
  149.                 a[i] = nextInt();
  150.             }
  151.             return a;
  152.         }
  153.  
  154.         public String readString() {
  155.             int c = snext();
  156.             while (isSpaceChar(c)) {
  157.                 c = snext();
  158.             }
  159.             StringBuilder res = new StringBuilder();
  160.             do {
  161.                 res.appendCodePoint(c);
  162.                 c = snext();
  163.             } while (!isSpaceChar(c));
  164.             return res.toString();
  165.         }
  166.  
  167.         public String nextLine() {
  168.             int c = snext();
  169.             while (isSpaceChar(c))
  170.                 c = snext();
  171.             StringBuilder res = new StringBuilder();
  172.             do {
  173.                 res.appendCodePoint(c);
  174.                 c = snext();
  175.             } while (!isEndOfLine(c));
  176.             return res.toString();
  177.         }
  178.  
  179.         public boolean isSpaceChar(int c) {
  180.             if (filter != null)
  181.                 return filter.isSpaceChar(c);
  182.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  183.         }
  184.  
  185.         private boolean isEndOfLine(int c) {
  186.             return c == '\n' || c == '\r' || c == -1;
  187.         }
  188.  
  189.         public interface SpaceCharFilter {
  190.             public boolean isSpaceChar(int ch);
  191.         }
  192.     }
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement