-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquare.java
More file actions
94 lines (82 loc) · 2.51 KB
/
Square.java
File metadata and controls
94 lines (82 loc) · 2.51 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
// Square
import java.util.ArrayList;
public class Square implements Shape {
public double sideLength;
public double x;
public double y;
public double topLeftX;
public double topLeftY;
public double topRightX;
public double topRightY;
public double botLeftX;
public double botLeftY;
public double botRightX;
public double botRightY;
public Square(double sideLength, double x, double y){
this.sideLength = sideLength;
this.x = x;
this.y = y;
// Top left point
this.topLeftX = x - (sideLength/2);
this.topLeftY = y + (sideLength/2);
// top right point
this.topRightX = x + (sideLength/2);
this.topRightY = topLeftY;
// bottom left point
this.botLeftX = topLeftX;
this.botLeftY = y - (sideLength/2);
// bottom right point
this.botRightX = topRightX;
this.botRightY = botLeftY;
}
@Override
public double area() {
return Math.pow(sideLength, 2);
}
@Override
public double perimeter() {
return sideLength*4;
}
/**
* 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;
this.topLeftX = x - (sideLength/2);
this.topLeftY = y + (sideLength/2);
// top right point
this.topRightX = x + (sideLength/2);
this.topRightY = topLeftY;
// bottom left point
this.botLeftX = topLeftX;
this.botLeftY = y - (sideLength/2);
// bottom right point
this.botRightX = topRightX;
this.botRightY = botLeftY;
}
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;
}
public void setSideLength(double length){
this.sideLength = length;
}
public double getSideLength(){
return this.sideLength;
}
@Override
public String toString(){
return "Square Info: \nCentroid Coordinate: ("+this.x + ","+this.y+")" + "\nSide length: " + this.sideLength;
}
}