Advertisement
philcrafts

Crafts/ Calvao CSC-220 PolyHW

Sep 22nd, 2017
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. /*
  2.  * Name: Philip Crafts/ Luis Calvao
  3.  * Date: 9/22/17
  4.  * Course Number: CSC 220
  5.  * Course Name: Data Structures and Algorithims
  6.  * Problem Number: N/A
  7.  * Returns the area of a convex polygon from points entered clockwise
  8.  */
  9.  
  10. import java.util.Scanner;
  11.  
  12.  
  13. public class polygonHW {
  14.    
  15.    
  16.     private static void process(Scanner sc, String args[])  {
  17.         // Code here is merely a sample
  18.         int size = 0;
  19.        
  20.         while(size < 3)
  21.         {
  22.         System.out.print("Enter number of points (must be more than 3)");
  23.         size = sc.nextInt();
  24.         }
  25.            
  26.         Double[] x = new Double[size];
  27.         Double[] y = new Double[size];
  28.        
  29.         System.out.println("Enter points in Rectangular form (seperated by spaces not commas)");
  30.         getPoints(sc, x,y);
  31.         printPoints(x,y);
  32.         printArea(x,y);
  33.        
  34.         sc.nextLine();
  35.     }
  36.    
  37.      private static boolean doThisAgain(Scanner sc, String prompt) {
  38.             System.out.print(prompt);
  39.             String doOver = sc.nextLine();
  40.             return doOver.equalsIgnoreCase("Y");
  41.         }
  42.      
  43.      public static void main(String args[]) {
  44.             final String TITLE = "Crafts/Calvao polygonHW";
  45.             final String CONTINUE_PROMPT = "Do this again? [y/N] ";
  46.            
  47.             System.out.println("Welcome to " + TITLE);
  48.             Scanner sc = new Scanner(System.in);
  49.             do {
  50.                 process(sc, args);
  51.             } while (doThisAgain(sc, CONTINUE_PROMPT));
  52.             sc.close();
  53.             System.out.println("Thank you for using " + TITLE);
  54.         }
  55.  
  56.    
  57.     private static void printArea(Double[] x, Double[] y) {
  58.        
  59.         double theta = 0;
  60.         double hyp = 0;
  61.         double base = 0;
  62.         double height = 0;
  63.         double Area = 0;
  64.         double heronCheck = 0;
  65.        
  66.         x = orientArr(x);
  67.         y = orientArr(y);
  68.        
  69.         for(int i = 1; i < x.length -1 ; i++ )
  70.         {
  71.             base = simpleDist(x[i] , y[i]);
  72.             hyp = simpleDist(x[i+1] , y[i+1]);
  73.            
  74.             theta = Math.atan2(y[i], x[i]) - Math.atan2(y[i+1], x[i+1]);
  75.                
  76.             height = Math.sin(theta) * hyp;
  77.            
  78.             heronCheck = (base * height)/2;
  79.            
  80.             if(heronCheck <= 0)
  81.             {
  82.                 System.out.println("not a convex polygon");
  83.                 return;
  84.             }
  85.                
  86.             Area += heronCheck;
  87.         }
  88.        
  89.         System.out.printf("Area = %.2f\n", Area);
  90.         //System.out.print("\n");
  91.     }
  92.    
  93.    
  94.    
  95.  
  96.     //sets points stored at [0] as origin
  97.     private static Double[] orientArr(Double[] x) {
  98.        
  99.         double xCancel = x[0];
  100.        
  101.         for(int i = 0; i < x.length ; i++ )
  102.         {          
  103.             x[i] -= xCancel;
  104.         }
  105.        
  106.         return x;
  107.     }
  108.  
  109.    
  110.     private static double simpleDist(Double x, Double y) {
  111.        
  112.     return  Math.sqrt(Math.pow(x,2d) + Math.pow(y,2));
  113.    
  114.     }
  115.  
  116.     private static void printPoints(Double[] x, Double[] y) {
  117.         System.out.println("Collected Points");
  118.         for(int i = 0; i < x.length ; i++ ) {
  119.             System.out.println(x[i] +" , " +  y[i]);
  120.         }
  121.        
  122.     }
  123.  
  124.     private static void getPoints(Scanner sc, Double[] x, Double[] y) {
  125.        
  126.         int hold;
  127.        
  128.         for(int i = 0; i < x.length ; i++ ) {
  129.             hold = i+1;
  130.             System.out.println("point " + hold + ": ");
  131.             x[i] = sc.nextDouble();
  132.             y[i] = sc.nextDouble();
  133.         }
  134.        
  135.        
  136.     }
  137.    
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement