Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static class Point {
- double x;
- double y;
- public Point(String s) {
- String[] split = s.split(",");
- double x1 = Double.parseDouble(split[0].replaceAll("\\(", ""));
- double y1 = Double.parseDouble(split[1].replaceAll("\\)", ""));
- this.x = x1;
- this.y = y1;
- }
- public Point(double x, double y) {
- this.x = x;
- this.y = y;
- }
- }
- static public class Rational {
- private int num, denom;
- public Rational(double d) {
- String s = String.valueOf(d);
- int digitsDec = s.length() - 1 - s.indexOf('.');
- int denom = 1;
- for (int i = 0; i < digitsDec; i++) {
- d *= 10;
- denom *= 10;
- }
- int num = (int) Math.round(d);
- this.num = num;
- this.denom = denom;
- }
- public String toString() {
- return String.valueOf(num) + "/" + String.valueOf(denom);
- }
- }
- public static String IntersectingLines(String[] strArr) {
- Point p1 = new Point(strArr[0]);
- Point p2 = new Point(strArr[1]);
- Point p3 = new Point(strArr[2]);
- Point p4 = new Point(strArr[3]);
- Point intersection = lineLineIntersection(p1, p2, p3, p4);
- if (intersection == null) {
- return "no intersection";
- } else {
- double i1 = intersection.x;
- String n1 = "";
- if (i1 == (int) i1) {
- n1 = String.valueOf((int) i1);
- } else {
- n1 = new Rational(intersection.x).toString();
- }
- double i2 = intersection.y;
- String n2 = "";
- if (i2 == (int) i2) {
- n2 = String.valueOf((int) i2);
- } else {
- n2 = new Rational(intersection.y).toString();
- }
- return "(" + n1 + "," + n2 + ")";
- }
- }
- static Point lineLineIntersection(Point A, Point B, Point C, Point D) {
- double a1 = B.y - A.y;
- double b1 = A.x - B.x;
- double c1 = a1 * (A.x) + b1 * (A.y);
- double a2 = D.y - C.y;
- double b2 = C.x - D.x;
- double c2 = a2 * (C.x) + b2 * (C.y);
- double determinant = a1 * b2 - a2 * b1;
- if (determinant == 0) {
- return null;
- } else {
- double x = (b2 * c1 - b1 * c2) / determinant;
- double y = (a1 * c2 - a2 * c1) / determinant;
- return new Point(x, y);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement