Content-Length: 1726 | pFad | http://github.com/HowProgrammingWorks/ObjectOrientedProgramming/pull/4.patch
thub.com
From f0e8249359641c9621382453108cb557c775b192 Mon Sep 17 00:00:00 2001
From: nieopierzony
Date: Fri, 18 Feb 2022 21:27:07 +0300
Subject: [PATCH] Move area method to Geometry (#1)
---
JavaScript/8-geometry.js | 27 ++++++++++++++-------------
1 file changed, 14 insertions(+), 13 deletions(-)
diff --git a/JavaScript/8-geometry.js b/JavaScript/8-geometry.js
index 619d49a..fe21359 100644
--- a/JavaScript/8-geometry.js
+++ b/JavaScript/8-geometry.js
@@ -16,16 +16,6 @@ class Polygon {
this.points = points;
}
- get area() {
- let value = 0;
- let d = this.points[this.points.length - 1];
- for (const p of this.points) {
- value += p.x * d.y - d.x * p.y;
- d = p;
- }
- return Math.abs(value) / 2;
- }
-
toString() {
return this.points.map((p) => p.toString()).join('; ');
}
@@ -62,23 +52,34 @@ class Geometry {
point.y = x * sin + y * cos;
}
}
+
+ static area(polygon) {
+ const { points } = polygon
+ let value = 0;
+ let d = points[points.length - 1];
+ for (const p of points) {
+ value += p.x * d.y - d.x * p.y;
+ d = p;
+ }
+ return Math.abs(value) / 2;
+ }
}
// Usage
const rect = new Rect(10, 10, 30, -10);
console.log(rect);
-console.log(rect.area);
+console.log(Geometry.area(rect));
console.log('Rotate 45');
Geometry.rotate(rect, 45);
console.log(rect);
-console.log(rect.area);
+console.log(Geometry.area(rect));
const triangle = new Triangle(0, 0, 15, 0, 0, 15);
console.log(triangle);
console.log('Rotate 90');
Geometry.rotate(triangle, 90);
console.log(triangle);
-console.log(triangle.area);
+console.log(Geometry.area(triangle));
console.log(`${triangle}`);
--- a PPN by Garber Painting Akron. With Image Size Reduction included!Fetched URL: http://github.com/HowProgrammingWorks/ObjectOrientedProgramming/pull/4.patch
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy