File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ class Polygon {
1313
}
1414

1515
get area() {
16-
// implement area calculation
16+
let value = 0;
17+
let d = this.points[this.points.length - 1];
18+
for (const p of this.points) {
19+
value += p.x * d.y - d.x * p.y;
20+
d = p;
21+
}
22+
return Math.abs(value) / 2;
1723
}
1824
}
1925

@@ -54,12 +60,16 @@ class Geometry {
5460

5561
const rect = new Rect(10, 10, 30, -10);
5662
console.dir(rect);
63+
console.dir(rect.area);
64+
5765
console.log('Rotate 45');
5866
Geometry.rotate(rect, 45);
5967
console.dir(rect);
68+
console.dir(rect.area);
6069

6170
const triangle = new Triangle(0, 0, 15, 0, 0, 15);
6271
console.dir(triangle);
6372
console.log('Rotate 90');
6473
Geometry.rotate(triangle, 90);
6574
console.dir(triangle);
75+
console.dir(triangle.area);

0 commit comments

Comments
 (0)