Zalosin

Tema4_geometrie

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