-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
96 lines (90 loc) · 2.89 KB
/
Copy pathTriangle.java
File metadata and controls
96 lines (90 loc) · 2.89 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
// Triangle
import java.util.ArrayList;
public class Triangle implements Shape{
public double sideLength;
public double x;
public double y;
public double topX; // top right x
public double topY; // top right y
public double lX; // bottom left x
public double lY; // bottom left y
public double rX; // bottom right x
public double rY; // bottom right y
public Triangle(double sideLength, double x, double y){
this.sideLength = sideLength;
this.x = x;
this.y = y;
// Setting all the points needed for the triangle using the side length and some trig
this.topX = this.x;
this.topY = this.y + ((Math.sqrt(3)/3)*this.sideLength);
this.lX = this.x - (this.sideLength/2);
this.lY = this.y - ((Math.sqrt(3)/6)*sideLength);
this.rX = this.x + (this.sideLength/2);
this.rY = this.lY;
}
@Override
public double area() {
return (Math.sqrt(3)/4)*(Math.pow(sideLength, 2));
}
@Override
public double perimeter() {
return sideLength*3;
}
/**
* 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.topX = this.x;
this.topY = this.y + ((Math.sqrt(3)/3)*this.sideLength);
this.lX = this.x - (this.sideLength/2);
this.lY = this.y - ((Math.sqrt(3)/6)*sideLength);
this.rX = this.x + (this.sideLength/2);
this.rY = this.lY;
}
/**
* This method returns an array list of size two,
* the first entry being the x value of the centroid,
* and the second entry being the y value.
* @return centroid coordinates
*/
public ArrayList<Double> getCenter(){
ArrayList<Double> a = new ArrayList<>();
a.add(this.x);
a.add(this.y);
return a;
}
/**
* This method allows one to change the centroid's
* coordinates to inputted values sX and sY.
* @param sX
* @param sY
*/
public void setCenter(double sX, double sY){
this.x = sX;
this.y = sY;
}
/**
* This method allows one to set the side length.
* @param length
*/
public void setSideLength(double length){
this.sideLength = length;
}
/**
* This method returns the side length of the triangle.
* @return
*/
public double getSideLength(){
return this.sideLength;
}
@Override
public String toString(){
return "Triangle Info: \nCentroid Coordinate: ("+this.x + ","+this.y+")" + "\nSide length: " + this.sideLength;
}
}