@@ -16,16 +16,6 @@ class Polygon {
16
16
this . points = points ;
17
17
}
18
18
19
- get area ( ) {
20
- let value = 0 ;
21
- let d = this . points [ this . points . length - 1 ] ;
22
- for ( const p of this . points ) {
23
- value += p . x * d . y - d . x * p . y ;
24
- d = p ;
25
- }
26
- return Math . abs ( value ) / 2 ;
27
- }
28
-
29
19
toString ( ) {
30
20
return this . points . map ( ( p ) => p . toString ( ) ) . join ( '; ' ) ;
31
21
}
@@ -62,23 +52,34 @@ class Geometry {
62
52
point . y = x * sin + y * cos ;
63
53
}
64
54
}
55
+
56
+ static area ( polygon ) {
57
+ const { points } = polygon
58
+ let value = 0 ;
59
+ let d = points [ points . length - 1 ] ;
60
+ for ( const p of points ) {
61
+ value += p . x * d . y - d . x * p . y ;
62
+ d = p ;
63
+ }
64
+ return Math . abs ( value ) / 2 ;
65
+ }
65
66
}
66
67
67
68
// Usage
68
69
69
70
const rect = new Rect ( 10 , 10 , 30 , - 10 ) ;
70
71
console . log ( rect ) ;
71
- console . log ( rect . area ) ;
72
+ console . log ( Geometry . area ( rect ) ) ;
72
73
73
74
console . log ( 'Rotate 45' ) ;
74
75
Geometry . rotate ( rect , 45 ) ;
75
76
console . log ( rect ) ;
76
- console . log ( rect . area ) ;
77
+ console . log ( Geometry . area ( rect ) ) ;
77
78
78
79
const triangle = new Triangle ( 0 , 0 , 15 , 0 , 0 , 15 ) ;
79
80
console . log ( triangle ) ;
80
81
console . log ( 'Rotate 90' ) ;
81
82
Geometry . rotate ( triangle , 90 ) ;
82
83
console . log ( triangle ) ;
83
- console . log ( triangle . area ) ;
84
+ console . log ( Geometry . area ( triangle ) ) ;
84
85
console . log ( `${ triangle } ` ) ;
0 commit comments