Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. static class Point {
  2. double x;
  3. double y;
  4.  
  5. public Point(String s) {
  6. String[] split = s.split(",");
  7. double x1 = Double.parseDouble(split[0].replaceAll("\\(", ""));
  8. double y1 = Double.parseDouble(split[1].replaceAll("\\)", ""));
  9. this.x = x1;
  10. this.y = y1;
  11. }
  12.  
  13. public Point(double x, double y) {
  14. this.x = x;
  15. this.y = y;
  16. }
  17. }
  18.  
  19. static public class Rational {
  20.  
  21. private int num, denom;
  22.  
  23. public Rational(double d) {
  24. String s = String.valueOf(d);
  25. int digitsDec = s.length() - 1 - s.indexOf('.');
  26.  
  27. int denom = 1;
  28. for (int i = 0; i < digitsDec; i++) {
  29. d *= 10;
  30. denom *= 10;
  31. }
  32. int num = (int) Math.round(d);
  33.  
  34. this.num = num;
  35. this.denom = denom;
  36. }
  37.  
  38. public String toString() {
  39. return String.valueOf(num) + "/" + String.valueOf(denom);
  40. }
  41. }
  42.  
  43. public static String IntersectingLines(String[] strArr) {
  44. Point p1 = new Point(strArr[0]);
  45. Point p2 = new Point(strArr[1]);
  46. Point p3 = new Point(strArr[2]);
  47. Point p4 = new Point(strArr[3]);
  48. Point intersection = lineLineIntersection(p1, p2, p3, p4);
  49. if (intersection == null) {
  50. return "no intersection";
  51. } else {
  52. double i1 = intersection.x;
  53. String n1 = "";
  54. if (i1 == (int) i1) {
  55. n1 = String.valueOf((int) i1);
  56. } else {
  57. n1 = new Rational(intersection.x).toString();
  58. }
  59. double i2 = intersection.y;
  60. String n2 = "";
  61. if (i2 == (int) i2) {
  62. n2 = String.valueOf((int) i2);
  63. } else {
  64. n2 = new Rational(intersection.y).toString();
  65. }
  66. return "(" + n1 + "," + n2 + ")";
  67. }
  68. }
  69.  
  70. static Point lineLineIntersection(Point A, Point B, Point C, Point D) {
  71. double a1 = B.y - A.y;
  72. double b1 = A.x - B.x;
  73. double c1 = a1 * (A.x) + b1 * (A.y);
  74.  
  75. double a2 = D.y - C.y;
  76. double b2 = C.x - D.x;
  77. double c2 = a2 * (C.x) + b2 * (C.y);
  78.  
  79. double determinant = a1 * b2 - a2 * b1;
  80.  
  81. if (determinant == 0) {
  82. return null;
  83. } else {
  84. double x = (b2 * c1 - b1 * c2) / determinant;
  85. double y = (a1 * c2 - a2 * c1) / determinant;
  86. return new Point(x, y);
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement