-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path8-geometry.js
84 lines (72 loc) · 1.64 KB
/
8-geometry.js
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
toString() {
return `(${this.x}, ${this.y})`;
}
}
class Polygon {
constructor(...points) {
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('; ');
}
}
class Rect extends Polygon {
constructor(x1, y1, x2, y2) {
const a = new Point(x1, y1);
const b = new Point(x2, y1);
const c = new Point(x2, y2);
const d = new Point(x1, y2);
super(a, b, c, d);
}
}
class Triangle extends Polygon {
constructor(x1, y1, x2, y2, x3, y3) {
const a = new Point(x1, y1);
const b = new Point(x2, y2);
const c = new Point(x3, y3);
super(a, b, c);
}
}
class Geometry {
static rotate(polygon, angle) {
const { points } = polygon;
const radians = Math.PI / 180 * angle;
const sin = Math.sin(radians);
const cos = Math.cos(radians);
for (const point of points) {
const { x, y } = point;
point.x = x * cos - y * sin;
point.y = x * sin + y * cos;
}
}
}
// Usage
const rect = new Rect(10, 10, 30, -10);
console.log(rect);
console.log(rect.area);
console.log('Rotate 45');
Geometry.rotate(rect, 45);
console.log(rect);
console.log(rect.area);
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(`${triangle}`);