|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +class Point { |
| 4 | +constructor(x, y) { |
| 5 | +this.x = x; |
| 6 | +this.y = y; |
| 7 | +} |
| 8 | +} |
| 9 | + |
| 10 | +class Polygone { |
| 11 | +constructor(...points) { |
| 12 | +this.points = points; |
| 13 | +} |
| 14 | + |
| 15 | +get area() { |
| 16 | +// implement area calculation |
| 17 | +} |
| 18 | +} |
| 19 | + |
| 20 | +class Rect extends Polygone { |
| 21 | +constructor(x1, y1, x2, y2) { |
| 22 | +const a = new Point(x1, y1); |
| 23 | +const b = new Point(x2, y1); |
| 24 | +const c = new Point(x2, y2); |
| 25 | +const d = new Point(x1, y2); |
| 26 | +super(a, b, c, d); |
| 27 | +} |
| 28 | +} |
| 29 | + |
| 30 | +class Triangle extends Polygone { |
| 31 | +constructor(x1, y1, x2, y2, x3, y3) { |
| 32 | +const a = new Point(x1, y1); |
| 33 | +const b = new Point(x2, y2); |
| 34 | +const c = new Point(x3, y3); |
| 35 | +super(a, b, c); |
| 36 | +} |
| 37 | +} |
| 38 | + |
| 39 | +class Geometry { |
| 40 | +static rotate(polygone, angle) { |
| 41 | +const { points } = polygone; |
| 42 | +const radians = Math.PI / 180 * angle; |
| 43 | +const sin = Math.sin(radians); |
| 44 | +const cos = Math.cos(radians); |
| 45 | +for (const point of points) { |
| 46 | +const { x, y } = point; |
| 47 | +point.x = x * cos - y * sin; |
| 48 | +point.y = x * sin + y * cos; |
| 49 | +} |
| 50 | +} |
| 51 | +} |
| 52 | + |
| 53 | +// Usage |
| 54 | + |
| 55 | +const rect = new Rect(10, 10, 30, -10); |
| 56 | +console.dir(rect); |
| 57 | +console.log('Rotate 45'); |
| 58 | +Geometry.rotate(rect, 45); |
| 59 | +console.dir(rect); |
| 60 | + |
| 61 | +const triangle = new Triangle(0, 0, 15, 0, 0, 15); |
| 62 | +console.dir(triangle); |
| 63 | +console.log('Rotate 90'); |
| 64 | +Geometry.rotate(triangle, 90); |
| 65 | +console.dir(triangle); |
0 commit comments