blob: bf9f21f16412a4230509134d484d5115394683a9 (
plain)
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
|
public class MyTriangle implements Shape2d {
private MyPoint a, b, c;
/**
* Creates a new triangle with the 3 corners a, b, c
*/
public MyTriangle(MyPoint a, MyPoint b, MyPoint c) {
this.a = a;
this.b = b;
this.c = c;
}
// https://www.mathopenref.com/coordtrianglearea.html
public double area() {
return Math.abs((
this.a.getX() * (this.b.getY() - this.c.getY()) +
this.b.getX() * (this.c.getY() - this.a.getY()) +
this.c.getX() * (this.a.getY() - this.b.getY())) / 2);
}
public double perimeter() {
// Calculate the length of the 3 lines between points
double ab = b.sub(a).modulo();
double bc = c.sub(b).modulo();
double ca = a.sub(c).modulo();
return ab + bc + ca;
}
}
|