-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPolygonCheck.java
More file actions
90 lines (90 loc) · 2.41 KB
/
PolygonCheck.java
File metadata and controls
90 lines (90 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import java.util.*;
/**
* Main Class
*
*/
public class PolygonCheck {
/**
* This method checks whether a polygon can be formed with given side lengths
* @param arrLen The array that contains the lengths of sides of polygon
* @return whether the polygon can be formed or not
*/
public static boolean polyCheck(int[] arrLen) {
int max_val = 0, sum = 0;
for(int i=0;i<arrLen.length;i++) {
sum += arrLen[i];
max_val = Math.max(max_val, arrLen[i]);
}
if((sum-max_val)>max_val)
return true;
return false;
}
/**
* Check whether the formed polygon is regular or not
* @param arrLen The array that contains the lengths of sides of polygon
* @return whether is polygon is regular or not
*/
public static boolean checkRegPoly(int[] arrLen) {
boolean poly = polyCheck(arrLen);
if(poly){
int first = arrLen[0];
for(int i=1;i<arrLen.length;i++){
if(arrLen[i]!=first)
return false;
}
}
return true;
}
/**
* Function to perform operations on regular polygons
* @param arrLen The array that contains the lengths of sides of polygon
*/
public static void regPoly(int[] arrLen) {
int n=arrLen.length;
int sum = 0;
for(int i=0;i<arrLen.length;i++) {
sum += arrLen[i];
}
double area, perimeter, interior, exterior,side = arrLen[0];
area = (n*Math.pow(side, 2))/4*(Math.tan((Math.PI)/n));
perimeter = side*n;
interior = (n-2)*180;
exterior = 360/n;
System.out.println("Area - " + area + "\nPerimeter - " + perimeter + "\nSum of Interior Angles - " + interior + "\nSum of Exterior Angles - " + exterior);
}
/**
* Function to perform operations on non regular polynomials
* @param arrLen The array that contains the lengths of sides of polygon
*/
public static void irregPoly(int[] arrLen) {
int n=arrLen.length;
int sum = 0;
for(int i=0;i<arrLen.length;i++) {
sum += arrLen[i];
}
double perimeter, diagonals;
perimeter = sum;
diagonals = (Math.pow(n, 2) - 3*n)/2;
System.out.println("\nPerimeter - " + perimeter + "\nNumber of Diagonals " + diagonals);
}
/**
* Driver Function
* @param args
*/
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] sides = new int[n];
for(int i=0;i<n;i++) {
sides[i] = sc.nextInt();
}
boolean isPoly = polyCheck(sides);
if(isPoly){
boolean regular = checkRegPoly(sides);
if(regular)
regPoly(sides);
else
irregPoly(sides);
}
}
}