-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmisc-rectangle-plotter.js
138 lines (72 loc) · 3.89 KB
/
misc-rectangle-plotter.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*** Rectangle Plotter
/*** by Andrew Kramer
This program creates a class named Rectangle that has four properties: x, y, width, and height, and two methods: toString() and shift(). The toString() method returns a string representation of the rectangle. The shift() method shifts the rectangle by a given amount in the x and y directions.
*** PSEUDOCODE
- Create a class named Rectangle with four properties: x, y, width, and height.
- Create a constructor that takes four arguments and assigns them to the properties.
- Create a method named toString() that returns a string representation of the rectangle.
- Create a method named shift() that shifts the rectangle by a given amount in the x and y directions.
- Create a method named offset() that returns a new rectangle that is the same as the original but shifted by a given amount in the x and y directions.
- Create a new rectangle named r1 with x = 10, y = 20, width = 30, and height = 40.
- Display the rectangle r1.
- Display the string representation of the rectangle r1.
- Shift the rectangle r1 by -10 in the x direction and -20 in the y direction.
- Display the rectangle r1 after shifting.
- Display the string representation of the rectangle r1 after shifting.
- Create a new rectangle named r2 that is the same as r1 but shifted by 100 in the x direction and 100 in the y direction.
- Display the rectangle r1 after offset.
- Display the rectangle r2 after offset.
- Display the string representation of the rectangle r1 after offset.
- Display the string representation of the rectangle r2 after offset.
*/
// Create a class named Rectangle with four properties: x, y, width, and height.
class Rectangle {
// Create a constructor that takes four arguments and assigns them to the properties.
constructor( x, y, width, height ) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
// Create a method named toString() that returns a string representation of the rectangle.
toString() {
return `x = ${this.x}, y = ${this.y}, width = ${this.width}, height = ${this.height}`
}
// Create a method named shift() that shifts the rectangle
// by a given amount in the x and y directions.
shift( changeInX, changeinY ) {
this.x = changeInX + this.x;
this.y = changeinY + this.y;
}
// Create a method named offset() that returns a new rectangle that is the same as the
// original but shifted by a given amount in the x and y directions.
offset( changeinX, changeinY ) {
return new Rectangle(this.x + changeinX, this.y + changeinY, this.width, this.height);
}
}
// Create a new rectangle named r1 with x = 10, y = 20, width = 30, and height = 40.
r1 = new Rectangle(10, 20, 30, 40);
// Display the rectangle r1.
console.log('\n\n\n\nr1\t\t\t\t\t', r1)
// Display the string representation of the rectangle r1.
console.log('\n\nnr1.toString()\t\t\t\t', r1.toString())
console.log('\n\n')
// Shift the rectangle r1 by -10 in the x direction and -20 in the y direction.
r1.shift(-10, -20)
// Display the rectangle r1 after shifting.
console.log('\n\nr1 after shift\t\t\t\t', r1)
// Display the string representation of the rectangle r1 after shifting.
console.log('\n\nr1.toString() after shift\t\t', r1.toString());
console.log('\n\n')
// Create a new rectangle named r2 that is the same as r1 but shifted
r2 = r1.offset(100, 100);
// Display the rectangle r1 after offset.
console.log('\n\nr1 after offset\t\t\t\t', r1)
// Display the rectangle r2 after offset.
console.log('\n\nr2 after offset\t\t\t\t', r2)
console.log('\n\n')
// Display the string representation of the rectangle r1 after offset.
console.log('\n\nr1.toString() after offset\t\t', r1.toString());
// Display the string representation of the rectangle r2 after offset.
console.log('\n\nr2.toString() after offset\t\t', r2.toString());
console.log('\n\n')