-
Notifications
You must be signed in to change notification settings - Fork 0
/
4-Matrix-Exercise.js
275 lines (252 loc) · 6.69 KB
/
4-Matrix-Exercise.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
* 4-Matrix-Exercise.js - code for workbook 4 page 4
* provides a non-working example for students to edit
*
* written by Michael Gleicher, January 2019
*
*/
// see other files for explanation of these comments
// @ts-check
/* jshint -W069, esversion:6 */
import { draggablePoints } from "./Libs/dragPoints.js";
const circRadius = 7;
/**
* TwoDots1 - a function for the student to write
* Notice that it gets the two points and the context as arguments
* This function should apply a transformation
*
* You must write this function using rotate, translate and scale
*
* @param {CanvasRenderingContext2D} context
* @param {number} x1
* @param {number} y1
* @param {number} x2
* @param {number} y2
*/
function twoDots1(context,x1,y1,x2,y2) {
context.translate(x1,y1);
context.scale(5,5);
}
/**
* TwoDots2 - another function for the student to write
*
* fill in the expressions for a,b,c,d,e,f
*
* @param {CanvasRenderingContext2D} context
* @param {number} x1
* @param {number} y1
* @param {number} x2
* @param {number} y2
*/
function twoDots2(context,x1,y1,x2,y2) {
let a = 5;
let b = 0;
let c = 0;
let d = 5;
let e = x2;
let f = y2;
// please leave this line - you should CHANGE the 6 lines above
context.transform(a,b,c,d,e,f);
}
/**
* TwoDots3 - another function for the student to write
*
* this should perform some transformation - you can decide how it works
*
* @param {CanvasRenderingContext2D} context
* @param {number} x1
* @param {number} y1
* @param {number} x2
* @param {number} y2
*/
function twoDots3(context,x1,y1,x2,y2) {
let a = 5;
let b = 0;
let c = 0;
let d = 5;
let e = x2;
let f = y2;
context.transform(a,b,c,d,e,f);
}
/**
* Drawing Box 2 -
* For each square, comment out the "transformations" and put
* the appropriate numbers in the transform. - the picture should
* look the same.
*
* I did square 2 for you
*
* @param {CanvasRenderingContext2D} context
*/
function drawBox3(context) {
// Square #1
context.save();
// transformation version (student should comment out)
context.translate(20,20);
context.scale(4,4);
// matrix version (student should replace the numbers)
context.transform(1,0,0,1,0,0);
//
markedSquare(context);
context.restore();
// Square #2 - I did this one for you!
context.save();
// transformation version (student should comment out)
// context.translate(20,20);
// context.scale(4,4);
// context.translate(15,0);
// matrix version (student should replace the numbers)
context.transform(4,0,0,4,80,20);
markedSquare(context);
//
context.restore();
// Square #3
context.save();
// transformation version (student should comment out)
context.translate(140,20);
context.scale(4,4);
context.rotate(Math.PI/2);
context.translate(0,-10);
// matrix version (student should replace the numbers)
context.transform(1,0,0,1,0,0);
//
markedSquare(context);
context.restore();
// Square #4
context.save();
// transformation version (student should comment out)
context.rotate(-Math.PI/2);
context.translate(-60,200);
context.scale(4,4);
// matrix version (student should replace the numbers)
context.transform(1,0,0,1,0,0);
//
markedSquare(context);
context.restore();
// Square #5
context.save();
// transformation version (student should comment out)
context.rotate(-Math.PI/2);
context.translate(-60,260);
context.scale(4,-4);
context.translate(0,-10);
// matrix version (student should replace the numbers)
context.transform(1,0,0,1,0,0);
//
markedSquare(context);
context.restore();
}
/**
*
* ShearX function - student should fill this in.
*
* @param {CanvasRenderingContext2D} context
* @param {number} s
*/
function shearX(context,s) {
}
/** Utility function: draw a 10x10 square with markings so we know which
* way is x and which way is Y
* don't use rect drawing - since it doesn't always transform correctly
* @param {CanvasRenderingContext2D} context
*/
function markedSquare(context) {
function square(x,y,size,color) {
context.save();
context.translate(x,y);
context.fillStyle = color;
context.beginPath();
context.moveTo(0,0);
context.lineTo(size,0);
context.lineTo(size,size);
context.lineTo(0,size);
context.closePath();
context.fill();
context.restore();
}
square(0,0,10,"goldenrod");
square(1,1,2,"red");
square(7,7,2,"green");
square(7,1,2,"white");
}
/**
*
* @param {CanvasRenderingContext2D} context
* @param {number} x
* @param {number} y
* @param {number} radius
* @param {string} color
*/
function circle(context,x,y,radius,color) {
context.save();
context.fillStyle = color;
context.beginPath();
context.arc(x,y,radius,0,Math.PI*2);
context.fill();
context.restore();
}
function box12()
{
function setup(id,funct) {
let canvas = /** @type {HTMLCanvasElement} */ (document.getElementById(id));
let context = canvas.getContext("2d");
let pts = [ [20,20], [50,50]]; // initial points
function draw1() {
context.clearRect(0,0,canvas.width,canvas.height);
context.save();
funct(context,pts[0][0],pts[0][1], pts[1][0], pts[1][1]);
markedSquare(context);
context.restore();
circle(context, pts[0][0], pts[0][1], circRadius, "red");
circle(context, pts[1][0], pts[1][1], circRadius, "green");
}
draggablePoints(canvas,pts,draw1,circRadius);
draw1();
}
setup("twodots1",twoDots1);
setup("twodots2",twoDots2);
setup("twodots3",twoDots3);
}
function box3()
{
let canvas = /** @type {HTMLCanvasElement} */ (document.getElementById("box3canvas"));
let context = canvas.getContext("2d");
drawBox3(context);
}
function box4()
{
let canvas = /** @type {HTMLCanvasElement} */ (document.getElementById("box4shearX"));
let context = canvas.getContext("2d");
context.save();
context.translate(20,20);
context.scale(4,4);
shearX(context,1);
markedSquare(context);
context.restore();
context.save();
context.translate(140,20);
context.scale(4,4);
shearX(context,-1);
markedSquare(context);
context.restore();
context.save();
context.translate(20,80);
context.scale(4,4);
shearX(context,2);
markedSquare(context);
context.restore();
context.save();
context.translate(220,80);
context.scale(4,4);
shearX(context,-2);
markedSquare(context);
context.restore();
}
/**
* Start things up correctly
*/
window.onload = function() {
box12();
box3();
box4();
};