Advertisement
philcrafts

Crafts/ Calvao CSC-220 PolyHW

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