Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.InputMismatchException;
- public class segmentintersection {
- public static void main(String[] args) {
- StringBuilder sb = new StringBuilder(1000);
- InputReader in = new InputReader(System.in);
- for (int tests = in.nextInt(); tests > 0; tests--) {
- LineSegment ls1 = new LineSegment(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
- LineSegment ls2 = new LineSegment(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
- sb.append(ls1.getAnswer(ls2));
- sb.append('\n');
- }
- System.out.println(sb.toString());
- }
- static class LineSegment {
- private static final int INF = Integer.MAX_VALUE;
- public static final double EPS = 0.000000001;
- public final long x1, y1, x2, y2;
- public final long a, b, c;
- LineSegment(int x1, int y1, int x2, int y2) {
- if (x1 < x2 || x1 == x2 && y1 < y2) {
- this.x1 = x1; this.x2 = x2; this.y1 = y1; this.y2 = y2;
- } else {
- this.x1 = x2; this.x2 = x1; this.y1 = y2; this.y2 = y1;
- }
- long ta = y2-y1, tb = x1-x2, tc = -ta*x1-tb*y1;
- if (ta == 0 && tb == 0) { // point
- a = INF; b = INF; c = INF;
- return;
- }
- long g = gcd(gcd(ta, tb), tc);
- ta /= g; tb /= g; tc /= g;
- int sign = ta < 0 || ta == 0 && tb < 0 ? -1 : 1;
- a = ta*sign; b = tb*sign; c = tc*sign;
- }
- private boolean checkBounds(LineSegment a, LineSegment b) {
- boolean x = Math.max(a.x1, b.x1) <= Math.min(a.x2, b.x2);
- 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));
- }
- public String getAnswer(LineSegment o) {
- if (!checkBounds(this, o)) return "none";
- if (a == INF && o.a == INF) //if both are points
- return x1==o.x1 && y1 ==o.y1 ? getPrint(x1,y1) : "none";
- if (a == INF) return o.a*x1 + o.b*y1 == -o.c ? getPrint(x1,y1) : "none";
- if (o.a == INF) return a*o.x1 + b*o.y1 == -c ? getPrint(o.x1,o.y1) : "none";
- if (a == o.a && b == o.b) {//if they are parallel
- if (c != o.c) return "none"; // 0 0 -5 -5 -2 -2 10 10 -> -2 -2 0 0
- boolean swap = x1 < o.x1 || x1 == o.x1 && y1 < o.y1;
- LineSegment low = swap ? this : o;
- LineSegment high = swap ? o : this;
- if (low.x2 < high.x1 || low.x2 == high.x1 && low.y2 < high.y1) return "none";
- if (low.x2 == high.x1 && low.y2 == high.y1) return getPrint(low.x2,low.y2);
- String end = high.x2 < low.x2 || high.x2 == low.x2 && high.y2 < low.y2 ? getPrint(high.x2, high.y2) : getPrint(low.x2, low.y2);
- return getPrint(high.x1,high.y1) + " " + end;
- }
- double i = (double)a * o.b - (double)o.a * b;
- double x = -((double)c*o.b - (double)o.c*b)/ i;
- double y = -((double)a*o.c - (double)o.a*c)/ i;
- boolean inX = x >= o.x1-EPS && x <= o.x2+EPS && x >= x1-EPS && x <= x2+EPS;
- 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;
- return inX && inY ? getPrint(x, y) : "none";
- }
- private String getPrint(double x, double y) {
- return String.format("%.2f %.2f", x == -0 ? 0:x, y == -0 ? 0:y);
- }
- private long gcd(long a, long b) {
- return a == 0 ? b : gcd(b%a, a);
- }
- }
- static class InputReader {
- private final InputStream stream;
- private final byte[] buf = new byte[8192];
- private int curChar, snumChars;
- private SpaceCharFilter filter;
- public InputReader(InputStream stream) {
- this.stream = stream;
- }
- public int snext() {
- if (snumChars == -1)
- throw new InputMismatchException();
- if (curChar >= snumChars) {
- curChar = 0;
- try {
- snumChars = stream.read(buf);
- } catch (IOException e) {
- throw new InputMismatchException();
- }
- if (snumChars <= 0)
- return -1;
- }
- return buf[curChar++];
- }
- public int nextInt() {
- int c = snext();
- while (isSpaceChar(c)) {
- c = snext();
- }
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = snext();
- }
- int res = 0;
- do {
- if (c < '0' || c > '9')
- throw new InputMismatchException();
- res *= 10;
- res += c - '0';
- c = snext();
- } while (!isSpaceChar(c));
- return res * sgn;
- }
- public long nextLong() {
- int c = snext();
- while (isSpaceChar(c)) {
- c = snext();
- }
- int sgn = 1;
- if (c == '-') {
- sgn = -1;
- c = snext();
- }
- long res = 0;
- do {
- if (c < '0' || c > '9')
- throw new InputMismatchException();
- res *= 10;
- res += c - '0';
- c = snext();
- } while (!isSpaceChar(c));
- return res * sgn;
- }
- public int[] nextIntArray(int n) {
- int a[] = new int[n];
- for (int i = 0; i < n; i++) {
- a[i] = nextInt();
- }
- return a;
- }
- public String readString() {
- int c = snext();
- while (isSpaceChar(c)) {
- c = snext();
- }
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = snext();
- } while (!isSpaceChar(c));
- return res.toString();
- }
- public String nextLine() {
- int c = snext();
- while (isSpaceChar(c))
- c = snext();
- StringBuilder res = new StringBuilder();
- do {
- res.appendCodePoint(c);
- c = snext();
- } while (!isEndOfLine(c));
- return res.toString();
- }
- public boolean isSpaceChar(int c) {
- if (filter != null)
- return filter.isSpaceChar(c);
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
- }
- private boolean isEndOfLine(int c) {
- return c == '\n' || c == '\r' || c == -1;
- }
- public interface SpaceCharFilter {
- public boolean isSpaceChar(int ch);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement