-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPentagon.java
More file actions
151 lines (144 loc) · 5.49 KB
/
Copy pathPentagon.java
File metadata and controls
151 lines (144 loc) · 5.49 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Pentagon Class
import java.lang.Math;
import java.util.ArrayList;
public class Pentagon implements Shape {
public double sideLength;
public double x;
public double y;
public double r;
public double topX;
public double topY;
public double leftX; // leftmost point x value
public double leftY; // left most point y value
public double rightX; // rightmost point x value
public double rightY; // rightmost y value
public double bLX; // bottom left point X value
public double bLY; // bottom left Y value
public double bRX; // bottom right x
public double bRY; // bottom right y
public Pentagon(double sideLength, double x, double y){
this.sideLength = sideLength;
this.x = x;
this.y = y;
// the four variables z,c,k,h are the rearranged formula to get the radius from the area of the pentagon
double z = (this.area()*4);
double c = (5+Math.sqrt(5))/2;
double k = Math.sqrt(c);
double h = k*5;
this.r = Math.sqrt(z/h); // this is the radius of the pentagon from the midpoint
// These four variables below are used to calculate the points based off the radius
double cons = this.r*Math.cos(0.314159);
double sons = this.r*Math.sin(0.314159);
double cons2 = this.r*Math.cos(0.942478);
double sons2 = this.r*Math.sin(-0.942478);
this.topX = this.x;
this.topY = this.r + this.y;
this.rightX = (cons)+this.x;
this.rightY = (sons)+this.y;
this.leftX = this.x - (cons);
this.leftY = this.rightY;
this.bLX = this.x - cons2;
this.bLY = this.y + sons2;
this.bRX = (x+cons2);
this.bRY = this.bLY;
}
@Override
public double area() {
return (0.25*(Math.sqrt(5*(5+(2*Math.sqrt(5))))))*(Math.pow(this.sideLength, 2));
}
@Override
public double perimeter() {
return sideLength*5;
}
/**
* This method moves a shape to another location
* in relation to its center, by adding the params offsetX and offsetY to its
* center coordinate.
* @param offsetX
* @param offsetY
*/
public void move(double offsetX, double offsetY){
this.x = this.x + offsetX;
this.y = this.y + offsetX;
double z = (this.area()*4);
double c = (5+Math.sqrt(5))/2;
double k = Math.sqrt(c);
double h = k*5;
this.r = Math.sqrt(z/h); // this is the radius of the pentagon from the midpoint
// These four variables below are used to calculate the points based off the radius
double cons = this.r*Math.cos(0.314159);
double sons = this.r*Math.sin(0.314159);
double cons2 = this.r*Math.cos(0.942478);
double sons2 = this.r*Math.sin(-0.942478);
this.topX = this.x;
this.topY = this.r + this.y;
this.rightX = (cons)+this.x;
this.rightY = (sons)+this.y;
this.leftX = this.x - (cons);
this.leftY = this.rightY;
this.bLX = this.x - cons2;
this.bLY = this.y + sons2;
this.bRX = (x+cons2);
this.bRY = this.bLY;
}
public ArrayList<Double> getCenter(){
ArrayList<Double> a = new ArrayList<>();
a.add(this.x);
a.add(this.y);
return a;
}
public void setCenter(double sX, double sY){
this.x = sX;
this.y = sY;
double z = (this.area()*4);
double c = (5+Math.sqrt(5))/2;
double k = Math.sqrt(c);
double h = k*5;
this.r = Math.sqrt(z/h); // this is the radius of the pentagon from the midpoint
// These four variables below are used to calculate the points based off the radius
double cons = this.r*Math.cos(0.314159);
double sons = this.r*Math.sin(0.314159);
double cons2 = this.r*Math.cos(0.942478);
double sons2 = this.r*Math.sin(-0.942478);
this.topX = this.x;
this.topY = this.r + this.y;
this.rightX = (cons)+this.x;
this.rightY = (sons)+this.y;
this.leftX = this.x - (cons);
this.leftY = this.rightY;
this.bLX = this.x - cons2;
this.bLY = this.y + sons2;
this.bRX = (x+cons2);
this.bRY = this.bLY;
}
public void setSideLength(double length){
this.sideLength = length;
double z = (this.area()*4);
double c = (5+Math.sqrt(5))/2;
double k = Math.sqrt(c);
double h = k*5;
this.r = Math.sqrt(z/h); // this is the radius of the pentagon from the midpoint
// These four variables below are used to calculate the points based off the radius
double cons = this.r*Math.cos(0.314159);
double sons = this.r*Math.sin(0.314159);
double cons2 = this.r*Math.cos(0.942478);
double sons2 = this.r*Math.sin(-0.942478);
this.topX = this.x;
this.topY = this.r + this.y;
this.rightX = (cons)+this.x;
this.rightY = (sons)+this.y;
this.leftX = this.x - (cons);
this.leftY = this.rightY;
this.bLX = this.x - cons2;
this.bLY = this.y + sons2;
this.bRX = (x+cons2);
this.bRY = this.bLY;
}
public double getSideLength(){
return this.sideLength;
}
@Override
public String toString(){
return "Pentagon Info: \nCentroid Coordinate: ("+this.x + ","+this.y+")" + "\nSide length: " + this.sideLength;
}
}