Skip to content

Commit 4052a59

Browse files
committed
Implement polygon area
1 parent 78faf11 commit 4052a59

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

JavaScript/8-geometry.js

Lines changed: 11 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy