Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package geometrie;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class Tema3 {
- public static void main(String[] args) throws FileNotFoundException {
- int POINTS_NUM;
- POINTS_NUM = 4;
- int index = 0;
- boolean result = false;
- boolean done = false;
- Point[] points = new Point[POINTS_NUM];
- //INPUT
- Scanner in = new Scanner(new java.io.File("date.in"));
- for (int j = 0; j < POINTS_NUM; j++) {
- points[j] = new Point(in.nextDouble(), in.nextDouble());
- }
- double a1 = points[0].y - points[1].y;
- double a2 = points[2].y - points[3].y;
- double b1 = points[0].x - points[1].x;
- double b2 = points[2].x - points[3].x;
- double c1 = (points[0].x * points[1].y) - (points[1].x * points[0].y);
- double c2 = (points[2].x * points[3].y) - (points[3].x * points[2].y);
- double delta = (a1 * b2) - (a2 * b1);
- if (delta != 0) {
- double d1 = (c2 * b1) - (c1 * b2);
- double d2 = (a2 * c1) - (a1 * c2);
- Point intersection = new Point(d1 / delta, -d2 / delta);
- if (intersection.between(points[0], points[1]) && intersection.between(points[2], points[3])) {
- System.out.println("Intersection point is = I(" + intersection.x + "," + intersection.y + ")");
- } else {
- System.out.println("Intersection point outside segments");
- }
- } else if (((a1 * c2) - (a2 * c1) != 0) || ((b1 * c2) - (b2 * c1) != 0)) {
- System.out.println("Null intersection");
- } else if ((a1 != 0) && (a2 != 0) && (b1 != 0) && (b2 != 0) && (c2 != 0) && (c1 != 0)) {
- System.out.println("Segments are equal");
- } else {
- Point p0;
- Point p1;
- Point p2;
- Point p3;
- if (points[0].compareTo(points[1]) < 0) {
- p0 = points[0];
- p1 = points[1];
- } else {
- p0 = points[1];
- p1 = points[0];
- }
- if (points[2].compareTo(points[3]) < 0) {
- p2 = points[2];
- p3 = points[3];
- } else {
- p2 = points[3];
- p3 = points[2];
- }
- System.out.println("p0 " + p0);
- System.out.println("p1 " + p1);
- System.out.println("p2 " + p2);
- System.out.println("p3 " + p3);
- if ((p1.compareTo(p2) < 0) || (p3.compareTo(p0) < 0)) {
- System.out.println("Intersection is null.");
- return;
- }
- if (p2.between(p0, p1)) {
- if (p3.between(p0, p1)) {
- System.out.println("Intersection is [" + p2 + " " + p3 + "]");
- } else {
- System.out.println("Intersection is [" + p2 + " " + p1 + "]");
- }
- } else {
- if (p3.between(p0, p1)) {
- System.out.println("Intersection is [" + p0 + " " + p3 + "]");
- } else {
- System.out.println("Intersection is [" + p0 + " " + p1 + "]");
- }
- }
- }
- }
- static class Point implements Comparable{
- double x, y;
- Point(double x, double y) {
- this.x = x;
- this.y = y;
- }
- boolean between(Point p1, Point p2) {
- double minx = (p1.x < p2.x) ? p1.x : p2.x;
- double maxx = (p1.x > p2.x) ? p1.x : p2.x;
- double miny = (p1.y < p2.y) ? p1.y : p2.y;
- double maxy = (p1.y > p2.y) ? p1.y : p2.y;
- if ((this.x >= minx && this.x <= maxx) && (this.y >= miny && this.y <= maxy)) {
- return true;
- }
- return false;
- }
- @Override
- public int compareTo(Object o) {
- Point p = (Point) o;
- if (x > p.x) {
- return 1;
- } else if (x == p.x) {
- if (y > p.y)
- return 1;
- else if (y == p.y)
- return 0;
- else
- return -1;
- }
- return -1;
- }
- @Override
- public String toString() {
- return "Point{" + "x=" + x + ", y=" + y + '}';
- }
- public Point copy() {
- return new Point(x, y);
- }
- @Override
- public int hashCode() {
- int hash = 7;
- return hash;
- }
- @Override
- public boolean equals(Object obj) {
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final Point other = (Point) obj;
- if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
- return false;
- }
- if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
- return false;
- }
- return true;
- }
- }
- }
Add Comment
Please, Sign In to add comment