Zalosin

Tema3_geometrie

Dec 4th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. package geometrie;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5.  
  6. public class Tema3 {
  7.  
  8. public static void main(String[] args) throws FileNotFoundException {
  9.  
  10. int POINTS_NUM;
  11. POINTS_NUM = 4;
  12. int index = 0;
  13. boolean result = false;
  14. boolean done = false;
  15. Point[] points = new Point[POINTS_NUM];
  16.  
  17. //INPUT
  18. Scanner in = new Scanner(new java.io.File("date.in"));
  19. for (int j = 0; j < POINTS_NUM; j++) {
  20. points[j] = new Point(in.nextDouble(), in.nextDouble());
  21. }
  22. double a1 = points[0].y - points[1].y;
  23. double a2 = points[2].y - points[3].y;
  24. double b1 = points[0].x - points[1].x;
  25. double b2 = points[2].x - points[3].x;
  26. double c1 = (points[0].x * points[1].y) - (points[1].x * points[0].y);
  27. double c2 = (points[2].x * points[3].y) - (points[3].x * points[2].y);
  28.  
  29. double delta = (a1 * b2) - (a2 * b1);
  30.  
  31. if (delta != 0) {
  32. double d1 = (c2 * b1) - (c1 * b2);
  33. double d2 = (a2 * c1) - (a1 * c2);
  34. Point intersection = new Point(d1 / delta, -d2 / delta);
  35. if (intersection.between(points[0], points[1]) && intersection.between(points[2], points[3])) {
  36. System.out.println("Intersection point is = I(" + intersection.x + "," + intersection.y + ")");
  37. } else {
  38. System.out.println("Intersection point outside segments");
  39. }
  40.  
  41. } else if (((a1 * c2) - (a2 * c1) != 0) || ((b1 * c2) - (b2 * c1) != 0)) {
  42. System.out.println("Null intersection");
  43. } else if ((a1 != 0) && (a2 != 0) && (b1 != 0) && (b2 != 0) && (c2 != 0) && (c1 != 0)) {
  44. System.out.println("Segments are equal");
  45. } else {
  46.  
  47. Point p0;
  48. Point p1;
  49. Point p2;
  50. Point p3;
  51.  
  52. if (points[0].compareTo(points[1]) < 0) {
  53. p0 = points[0];
  54. p1 = points[1];
  55. } else {
  56. p0 = points[1];
  57. p1 = points[0];
  58. }
  59.  
  60. if (points[2].compareTo(points[3]) < 0) {
  61. p2 = points[2];
  62. p3 = points[3];
  63. } else {
  64. p2 = points[3];
  65. p3 = points[2];
  66. }
  67.  
  68. System.out.println("p0 " + p0);
  69. System.out.println("p1 " + p1);
  70. System.out.println("p2 " + p2);
  71. System.out.println("p3 " + p3);
  72.  
  73. if ((p1.compareTo(p2) < 0) || (p3.compareTo(p0) < 0)) {
  74. System.out.println("Intersection is null.");
  75. return;
  76. }
  77.  
  78. if (p2.between(p0, p1)) {
  79. if (p3.between(p0, p1)) {
  80. System.out.println("Intersection is [" + p2 + " " + p3 + "]");
  81. } else {
  82. System.out.println("Intersection is [" + p2 + " " + p1 + "]");
  83. }
  84. } else {
  85. if (p3.between(p0, p1)) {
  86. System.out.println("Intersection is [" + p0 + " " + p3 + "]");
  87. } else {
  88. System.out.println("Intersection is [" + p0 + " " + p1 + "]");
  89. }
  90. }
  91. }
  92. }
  93.  
  94. static class Point implements Comparable{
  95.  
  96. double x, y;
  97.  
  98. Point(double x, double y) {
  99. this.x = x;
  100. this.y = y;
  101. }
  102.  
  103. boolean between(Point p1, Point p2) {
  104. double minx = (p1.x < p2.x) ? p1.x : p2.x;
  105. double maxx = (p1.x > p2.x) ? p1.x : p2.x;
  106. double miny = (p1.y < p2.y) ? p1.y : p2.y;
  107. double maxy = (p1.y > p2.y) ? p1.y : p2.y;
  108. if ((this.x >= minx && this.x <= maxx) && (this.y >= miny && this.y <= maxy)) {
  109. return true;
  110. }
  111. return false;
  112. }
  113.  
  114. @Override
  115. public int compareTo(Object o) {
  116. Point p = (Point) o;
  117. if (x > p.x) {
  118. return 1;
  119. } else if (x == p.x) {
  120. if (y > p.y)
  121. return 1;
  122. else if (y == p.y)
  123. return 0;
  124. else
  125. return -1;
  126. }
  127. return -1;
  128. }
  129.  
  130. @Override
  131. public String toString() {
  132. return "Point{" + "x=" + x + ", y=" + y + '}';
  133. }
  134.  
  135. public Point copy() {
  136. return new Point(x, y);
  137. }
  138.  
  139. @Override
  140. public int hashCode() {
  141. int hash = 7;
  142. return hash;
  143. }
  144.  
  145. @Override
  146. public boolean equals(Object obj) {
  147. if (obj == null) {
  148. return false;
  149. }
  150. if (getClass() != obj.getClass()) {
  151. return false;
  152. }
  153. final Point other = (Point) obj;
  154. if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
  155. return false;
  156. }
  157. if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
  158. return false;
  159. }
  160. return true;
  161. }
  162.  
  163.  
  164. }
  165. }
Add Comment
Please, Sign In to add comment