aboutsummaryrefslogtreecommitdiff
path: root/sem5/oop/m4/shapes/src/MyTriangle.java
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-09-15 18:51:31 +0200
committerJulian T <julian@jtle.dk>2020-09-15 18:51:31 +0200
commit4bfbc29c7ea0b711f96346deb25dde0b2298ecde (patch)
treea2df2df4da19fe2ffe6527efc2c3917e009f4d3a /sem5/oop/m4/shapes/src/MyTriangle.java
parent8cc618ebc9fa9dbcf04b17a9d8e23ba1281b419e (diff)
Added more oop assignments
Diffstat (limited to 'sem5/oop/m4/shapes/src/MyTriangle.java')
-rw-r--r--sem5/oop/m4/shapes/src/MyTriangle.java30
1 files changed, 30 insertions, 0 deletions
diff --git a/sem5/oop/m4/shapes/src/MyTriangle.java b/sem5/oop/m4/shapes/src/MyTriangle.java
new file mode 100644
index 0000000..bf9f21f
--- /dev/null
+++ b/sem5/oop/m4/shapes/src/MyTriangle.java
@@ -0,0 +1,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;
+ }
+}