-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFigureClasses.js
More file actions
54 lines (45 loc) · 1.42 KB
/
FigureClasses.js
File metadata and controls
54 lines (45 loc) · 1.42 KB
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
class Figure {
constructor(canvasContext) {
if (this.constructor === Figure) {
throw new AbstractClassException("Figure is an abstract class and there cannot be an instance of it.");
}
this.canvasContext = canvasContext;
}
draw() { };
}
class Circle extends Figure {
// constructor() { }
constructor(canvasContext, sideLength) {
super(canvasContext);
let halfSideLength = Math.floor(sideLength / 2);
this.sx = halfSideLength;
this.sy = halfSideLength;
this.r = halfSideLength;
}
draw() {
this.canvasContext.beginPath();
this.canvasContext.arc(this.sx, this.sy, this.r, 0, 2 * Math.PI);
this.canvasContext.stroke();
}
}
class Point extends Figure {
// constructor() { }
constructor(canvasContext, sideLength) {
super(canvasContext);
this.x = Math.floor(Math.random() * sideLength);
this.y = Math.floor(Math.random() * sideLength);
}
draw(color, size) {
this.canvasContext.fillStyle = color;
this.canvasContext.fillRect(this.x, this.y, size, size);
}
checkIfInCircle(circle){
return Math.pow((this.x - circle.sx), 2) + Math.pow((this.y - circle.sy), 2) <= Math.pow(circle.r, 2);
}
}
class AbstractClassException extends Error {
constructor(message) {
super(message);
this.name = "AbstractClassException";
}
}