Advertisement
Zalosin

Tema4_GEOMETRIE_FINAL_VERSION_ALPHA_BETA_CLOSED_GAMMA

Dec 5th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.02 KB | None | 0 0
  1. package geometrie;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.util.Scanner;
  5.  
  6. public class Tema4 {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner in;
  10.         try {
  11.             in = new Scanner(new java.io.File("date.in"));
  12.             Point[] points = new Point[4];
  13.             double x, y;
  14.  
  15.             for (int i = 0; i < 4; i++) {
  16.                 x = in.nextDouble();
  17.                 y = in.nextDouble();
  18.                 points[i] = new Point(x, y);
  19.             }
  20.  
  21.             // Ecuatia dreptei punctelor 1 si 2
  22.             double a12 = points[1].y - points[2].y;
  23.             double b12 = points[2].x - points[1].x;
  24.             Point mid12 = new Point((points[1].x + points[2].x) / 2, (points[1].y + points[2].y) / 2);
  25.             double d12 = -(b12 * mid12.x + a12 * mid12.y);
  26.  
  27.             // Ecuatia dreptei punctelor 2 si 0
  28.             double a20 = points[2].y - points[0].y;
  29.             double b20 = points[2].x - points[0].x;
  30.             Point mid20 = new Point((points[2].x + points[0].x) / 2, (points[2].y + points[0].y) / 2);
  31.             double d20 = -(b20 * mid20.x + a20 * mid20.y);
  32.  
  33.             // Delta
  34.             double delta = (a12 * b20) - (a20 * b12);
  35.             double d1 = (d20 * b12) - (d12 * b20);
  36.             double d2 = (a20 * d12) - (a12 * d20);
  37.  
  38.             // Centrul cercului circumscris
  39.             Point center = new Point(d2 / delta, d1 / delta);
  40.             System.out.println("Center " + center);
  41.  
  42.             // Raza
  43.             double radius = distance(center, points[0]);
  44.             System.out.println("Radius " + radius);
  45.  
  46.             double dist = distance(center, points[3]);
  47.  
  48.             if (dist < radius) {
  49.                 System.out.println("4th point inside circle");
  50.             } else if (Math.abs(dist - radius) < 0.01) {
  51.                 System.out.println("4th point on circle");
  52.             } else {
  53.                 System.out.println("4th point outside cricle");
  54.             }
  55.            
  56.             double edge01 = norm(vector(points[0], points[1]));
  57.             double edge23 = norm(vector(points[2], points[3]));;
  58.             double edge03 = norm(vector(points[0], points[3]));;
  59.             double edge12 = norm(vector(points[1], points[2]));;
  60.            
  61.             if (edge01 + edge23 == edge03 + edge12) {
  62.                 System.out.println("Quadrilateral is circumscribable");
  63.             } else {
  64.                 System.out.println("Quadrilateral is not circumscribable");
  65.             }
  66.  
  67.         } catch (FileNotFoundException ex) {
  68.             System.out.println("File not found!");
  69.         }
  70.     }
  71.  
  72.     private static double distance(Point p1, Point p2) {
  73.         double t1 = p2.x - p1.x;
  74.         double t2 = p2.y - p1.y;
  75. //        System.out.println("t1 " + t1);
  76. //        System.out.println("t2 " + t2);
  77.         return Math.sqrt((t1 * t1) + (t2 * t2));
  78.     }
  79.  
  80.     private static Point vector(Point p1, Point p2) {
  81.         return new Point(p2.x - p1.x, p2.y - p1.y);
  82.     }
  83.  
  84.     private static double scalar(Point p1, Point p2) {
  85.         return p1.x * p2.x + p1.y * p2.y;
  86.     }
  87.  
  88.     private static double norm(Point p) {
  89.         return Math.sqrt(p.x * p.x + p.y * p.y);
  90.     }
  91.  
  92.     static class Point implements Comparable {
  93.  
  94.         double x, y;
  95.  
  96.         Point(double x, double y) {
  97.             this.x = x;
  98.             this.y = y;
  99.         }
  100.  
  101.         boolean between(Point p1, Point p2) {
  102.             double minx = (p1.x < p2.x) ? p1.x : p2.x;
  103.             double maxx = (p1.x > p2.x) ? p1.x : p2.x;
  104.             double miny = (p1.y < p2.y) ? p1.y : p2.y;
  105.             double maxy = (p1.y > p2.y) ? p1.y : p2.y;
  106.             if ((this.x >= minx && this.x <= maxx) && (this.y >= miny && this.y <= maxy)) {
  107.                 return true;
  108.             }
  109.             return false;
  110.         }
  111.  
  112.         @Override
  113.         public int compareTo(Object o) {
  114.             Point p = (Point) o;
  115.             if (x > p.x) {
  116.                 return 1;
  117.             } else if (x == p.x) {
  118.                 if (y > p.y) {
  119.                     return 1;
  120.                 } else if (y == p.y) {
  121.                     return 0;
  122.                 } else {
  123.                     return -1;
  124.                 }
  125.             }
  126.             return -1;
  127.         }
  128.  
  129.         @Override
  130.         public String toString() {
  131.             return "Point{" + "x=" + x + ", y=" + y + '}';
  132.         }
  133.  
  134.         public Point copy() {
  135.             return new Point(x, y);
  136.         }
  137.  
  138.         @Override
  139.         public boolean equals(Object obj) {
  140.             if (obj == null) {
  141.                 return false;
  142.             }
  143.             if (getClass() != obj.getClass()) {
  144.                 return false;
  145.             }
  146.             final Point other = (Point) obj;
  147.             if (Double.doubleToLongBits(this.x) != Double.doubleToLongBits(other.x)) {
  148.                 return false;
  149.             }
  150.             if (Double.doubleToLongBits(this.y) != Double.doubleToLongBits(other.y)) {
  151.                 return false;
  152.             }
  153.             return true;
  154.         }
  155.  
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement