-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTriangle.java
More file actions
84 lines (62 loc) · 2.59 KB
/
Triangle.java
File metadata and controls
84 lines (62 loc) · 2.59 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
/**
* [Triangle.java]
* Constructs a Triangle
* @author Edward Yang
* @version 1.0
* Oct 8, 2020
*/
import java.lang.Math; //Math class needed to calculate vertices
public class Triangle extends Shapes{
/**serial Version UID */
private static final long serialVersionUID = 1L;
/**
* Constructor to create object "Triangle"
* @param name The name or type of the shape that is created
* @param xCoordinates The x-values of the shape's coordinates
* @param yCoordinates The y-values of the shape's coordinates
*/
Triangle(String name, int[] xCoordinates, int[] yCoordinates,int color){
super(name, xCoordinates, yCoordinates, color);
}
/**
* @return the area of the shape
*/
public double getArea(){
/**side Lengths of the variables */
double sideLength1;
/**side Lengths of the variables */
double sideLength2;
/**side Lengths of the variables */
double sideLength3;
/**x coordinates of vertices */
int[] xCoords = getXCoord();
/**Ycoordinates of vertices */
int[] yCoords = getYCoord();
sideLength1 = Math.sqrt(Math.pow(xCoords[0]-xCoords[1],2) + Math.pow(yCoords[0]-yCoords[1],2));
sideLength2 = Math.sqrt(Math.pow(xCoords[0]-xCoords[2],2) + Math.pow(yCoords[0]-yCoords[2],2));
sideLength3 = Math.sqrt(Math.pow(xCoords[2]-xCoords[1],2) + Math.pow(yCoords[2]-yCoords[1],2));
//calculate area using huron's formula
/**total side length of triangle */
double s = sideLength1 + sideLength2 + sideLength3;
/**area of triangle */
double area = Math.sqrt(s*(s - sideLength1)*(s - sideLength2)*(s - sideLength3));
return area;
}// end of method
/**
* @return the perimeter of the shape
*/
public double getPerimeter(){
//declaring the sides of the triangles
double sideLength1;
double sideLength2;
double sideLength3;
//getting the coordinates
int[] xCoords = getXCoord();
int[] yCoords = getYCoord();
//calculating 3 side lengths
sideLength1 = Math.sqrt(Math.pow(xCoords[0]-xCoords[1],2) + Math.pow(yCoords[0]-yCoords[1],2));
sideLength2 = Math.sqrt(Math.pow(xCoords[0]-xCoords[2],2) + Math.pow(yCoords[0]-yCoords[2],2));
sideLength3 = Math.sqrt(Math.pow(xCoords[2]-xCoords[1],2) + Math.pow(yCoords[2]-yCoords[1],2));
return sideLength1 + sideLength2 + sideLength3;
} //end of method
}//end of class