Advertisement
philcrafts

csc-220 polyHW For Luis

Sep 20th, 2017
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. /*
  2.  * Name:
  3.  * Date:
  4.  * Course Number:
  5.  * Course Name:
  6.  * Problem Number:
  7.  * Email:
  8.  * Short Description of the Problem
  9.  */
  10.  
  11. import java.util.Scanner;
  12. import java.lang.*;
  13.  
  14. public class polygonHW {
  15.    
  16.    
  17.     //**********************************************
  18.    
  19.     public static void main(String[] args) {
  20.         int size;
  21.          
  22.         Scanner sc = new Scanner(System.in);
  23.        
  24.         System.out.print("Enter number of points (must be more than 3)");
  25.         size = sc.nextInt();
  26.        
  27.        
  28.         Double[] x = new Double[size];
  29.         Double[] y = new Double[size];
  30.         getPoints(sc, x,y);
  31.         printPoints(x,y);
  32.         printArea(x,y);
  33.        
  34.        
  35.     }
  36.  
  37.     // Important stuff is in this method
  38.     private static void printArea(Double[] x, Double[] y) {
  39.        
  40.         double theta = 0;
  41.         double hyp = 0;
  42.         double base = 0;
  43.         double height = 0;
  44.         double Area = 0;
  45.        
  46.         //calls orient method
  47.         x = orientArr(x);
  48.         y = orientArr(y);
  49.        
  50.        
  51.         //loop runs two less times than the array length, always starts at [1] because [0] is now the origin (0,0)
  52.         //The idea is that one point will always be 0,0 so we just need to worry about two points at a time
  53.         for(int i = 1; i < x.length -1 ; i++ )
  54.         {
  55.             base = simpleDist(x[i] , y[i]);
  56.             hyp = simpleDist(x[i+1] , y[i+1]);
  57.            
  58.             //subtracts arctan of theta point 1 with respect to origin from theta point 2 with respect to origin, gives us the angle of the triangle near the origin
  59.             theta = Math.atan2(y[i], x[i]) - Math.atan2(y[i+1], x[i+1]);
  60.                
  61.             height = Math.sin(theta) * hyp;
  62.            
  63.             //Heron's Formula I think he said he wanted to return an error if this produces a negative number
  64.             // not sure though, it might be more complicated
  65.             Area += (base * height)/2;
  66.         }
  67.        
  68.         System.out.println("Area = " + Area);
  69.        
  70.     }
  71.    
  72.    
  73.     //sets points stored at [0] as origin
  74.     private static Double[] orientArr(Double[] x) {
  75.        
  76.         double xCancel = x[0];
  77.        
  78.         for(int i = 0; i < x.length ; i++ )
  79.         {          
  80.             x[i] -= xCancel;
  81.         }
  82.        
  83.         return x;
  84.     }
  85.  
  86.     // returns a point's distance from origin
  87.     private static double simpleDist(Double x, Double y) {
  88.        
  89.     return  Math.sqrt(Math.pow(x,2d) + Math.pow(y,2));
  90.    
  91.     }
  92.  
  93.     private static void printPoints(Double[] x, Double[] y) {
  94.         System.out.println("Collected Points");
  95.         for(int i = 0; i < x.length ; i++ ) {
  96.             System.out.println(x[i] +" , " +  y[i]);
  97.         }
  98.        
  99.     }
  100.  
  101.     private static void getPoints(Scanner sc, Double[] x, Double[] y) {
  102.        
  103.         for(int i = 0; i < x.length ; i++ ) {
  104.                
  105.             x[i] = sc.nextDouble();
  106.             y[i] = sc.nextDouble();
  107.         }
  108.        
  109.        
  110.     }
  111.    
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement