-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrans.js
393 lines (364 loc) · 8.42 KB
/
trans.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
var mat;
function conv_rad(a) {
return (a * Math.PI / 180);
}
function setAttr(el, tag, value)
{
el.setAttribute(tag, value);
}
function getId(el)
{
return (document.getElementById(el));
}
function create(el)
{
return (document.createElement(el));
}
function append(p, ch)
{
p.appendChild(ch);
}
function round(v, p)
{
return (Math.round(v * p) / p);
}
function intToFloat(num, decPlaces) {
return num.toFixed(decPlaces);
}
function get_T(x, y)
{
intToFloat(x, 3);
intToFloat(y, 3);
return ([[1.000, 0.000, x],
[0.000, 1.000, y],
[0.000, 0.000, 1.000]]);
}
function get_H(x, y)
{
intToFloat(x, 3);
intToFloat(y, 3);
return ([[x, 0.000, 0.000],
[0.000, y, 0.000],
[0.000, 0.000, 1.000]]);
}
function get_R(a)
{
a = conv_rad(a);
return ([[Math.cos(a), -Math.sin(a), 0.000],
[Math.sin(a), Math.cos(a), 0.000],
[0.000, 0.000, 1.000]]);
}
function get_S(a)
{
a = conv_rad(2 * a);
return ([[Math.cos(a), Math.sin(a), 0.0000],
[Math.sin(a), -Math.cos(a), 0.000],
[0.000, 0.000, 1.000]]);
}
function prod31(mat1, mat2)
{
var i = 0;
var mat3 = new Array(3);
var j;
while (i < 3)
{
var num = 0;
var j = 0;
while (j < 3)
{
num += mat1[i][j] * mat2[j];
j++;
}
mat3[i] = num;
i++;
}
return (mat3);
}
function prod33(m1, m2) {
var result = [];
for(var i = 0; i < m2.length; i++) {
result[i] = [];
for (var j = 0; j < m1[i].length; j++)
result[i][j] = m1[i][0] * m2[0][j] + m1[i][1] * m2[1][j] + m1[i][2] * m2[2][j];
}
return result;
}
function prod(mat1, mat2)
{
if (typeof(mat2[0][0]) != 'undefined')
return (prod33(mat1, mat2));
else
return (prod31(mat1, mat2));
}
function use_mat(newMat) {
if (typeof (mat) === 'undefined')
mat = newMat;
else
mat = prod(newMat, mat);
}
function display_tab(element, tab)
{
var el = getId(element);
var table = create('tab');
setAttr(table, 'style', 'width:auto;height:auto;border-spacing: 40px 10px;');
var i = 0;
var j;
while (i < tab.length)
{
var tr = create('tr');
j = 0;
while (j < tab[i].length)
{
var td = create('td');
td.innerHTML = (tab[i][j] < 0) ? tab[i][j].toFixed(3): ' '+ new String(tab[i][j].toFixed(3));
append(tr, td);
j++;
}
append(table, tr);
i++;
}
append(el, table);
}
function get_C(x, y) {
this.x = x;
this.y = y;
this.z = 1.000;
this.mat = function () {
return ([this.x, this.y, this.z])
};
this.mat2 = function() {
return ([this.x, this.y])
};
this.trans = function(mat) {
this.x = round(mat[0], 100);
this.y = round(mat[1], 100);
this.z = round(mat[2], 100);
};
}
function Coord(x, y) {
intToFloat(x, 3);
intToFloat(y, 3);
this.origin = new get_C(x, y);
this.result = new get_C(x, y);
this.trans = function (mat) {
this.result.trans(prod(mat, this.result.mat()));
};
this.display = function (el) {
$("#start_coord").append('(' + this.origin.mat2() + ')');
$("#result").append('(' + this.result.mat2() + ')');
}
}
function display_mat(el)
{
display_tab(el, mat);
}
function check_trans(id)
{
var error = $(".error");
var element = $("#"+id);
var no_error = true;
var param1;
var param2;
if (element.hasClass("T"))
{
param1 = $("#translation-i-"+id).val();
param2 = $("#translation-j-"+id).val();
if (param1 == "" || param2 == "")
{
error.css("display", "block");
error.append("L'un des paramètres d'une transformation n'est pas renseigné.<br />");
no_error = false;
}
if (isNaN(param1) || isNaN(param2))
{
error.css("display", "block");
error.append("L'un des paramètres d'une transformation est incorrect.<br />");
no_error = false;
}
}
else if (element.hasClass("H"))
{
param1 = $("#homothetie-m-"+id).val();
param2 = $("#homothetie-n-"+id).val();
if (param1 == "" || param2 == "")
{
error.css("display", "block");
error.append("L'un des paramètres d'une transformation n'est pas renseigné.<br />");
no_error = false;
}
if (isNaN(param1) || isNaN(param2))
{
error.css("display", "block");
error.append("L'un des paramètres d'une transformation est incorrect.<br />");
no_error = false;
}
}
else if (element.hasClass("S"))
{
param1 = $("#symetrie-a-"+id).val();
if (param1 == "")
{
error.css("display", "block");
error.append("L'un des paramètres d'une transformation n'est pas renseigné.<br />");
no_error = false;
}
if (isNaN(param1))
{
error.css("display", "block");
error.append("L'un des paramètres d'une transformation est incorrect.<br />");
no_error = false;
}
}
else if (element.hasClass("R"))
{
param1 = $("#rotation-a-"+id).val();
if (param1 == "")
{
error.css("display", "block");
error.append("L'un des paramètres d'une transformation n'est pas renseigné.<br />");
no_error = false;
}
if (isNaN(param1))
{
error.css("display", "block");
error.append("L'un des paramètres d'une transformation est incorrect.<br />");
no_error = false;
}
}
return (no_error);
}
function check_param()
{
var error = $(".error");
var point_x = $("#coord-x").val();
var point_y = $("#coord-y").val();
var no_error = true;
var i = 1;
var id;
var param_trans;
var el = getId('el');
var divResult = getId('result');
while (el.hasChildNodes())
el.removeChild(el.lastChild);
while (divResult.hasChildNodes())
divResult.removeChild(divResult.lastChild);
error.html("");
error.css("display", "none");
$("h1").css("display", "none");
$("#liste_operation").css("display", "none").html("<h2>Liste des opérations :</h2>");
$("#start_coord").css("display", "none").html("<h2>Point d'origine</h2>");
$("#el").css("display", "none").html("<h2>Matrice à appliquer au point</h2>");
$("#result").css("display", "none").html("<h2>Nouvelle coordonnée</h2>");
if (point_x == "" || point_y == "")
{
error.append("La coordonnée du point n'est pas complet.<br />");
error.css("display", "block");
no_error = false;
}
if (isNaN(point_x) || isNaN(point_y))
{
error.append("La coordonnées du point doit être composé de nombres entier.<br />");
error.css("display", "block");
no_error = false;
}
if (nb_trans > 0)
{
while (i <= nb_trans)
{
if ($("#"+i).size != 0)
{
if (check_trans(i) == false)
return (false);
}
i++;
}
}
return (no_error);
}
function calcul()
{
var i = 1;
var element;
var param1;
var param2;
var point_x = $("#coord-x").val();
var point_y = $("#coord-y").val();
var liste = $("#liste_operation");
var coord = new Coord(parseFloat(point_x), parseFloat(point_y));
mat = undefined;
if (nb_trans > 0)
{
while (i <= nb_trans)
{
element = $("#"+i);
if (element.size != 0)
{
if (element.hasClass("T"))
{
param1 = $("#translation-i-"+i).val();
param2 = $("#translation-j-"+i).val();
use_mat(get_T(parseFloat(param1), parseFloat(param2)));
liste.append("Translation de vecteur ("+param1+", "+param2+").<br />");
}
else if (element.hasClass("H"))
{
param1 = $("#homothetie-m-"+i).val();
param2 = $("#homothetie-n-"+i).val();
use_mat(get_H(parseFloat(param1), parseFloat(param2)));
liste.append("Homothétie de rapport "+param1+" et "+param2+".<br />");
}
else if (element.hasClass("S"))
{
param1 = $("#symetrie-a-"+i).val();
use_mat(get_S(parseFloat(param1)));
liste.append("Symétrie par rapport à un axe incliné de "+param1+" degrés.<br />");
}
else if (element.hasClass("R"))
{
param1 = $("#rotation-a-"+i).val();
use_mat(get_R(parseFloat(param1)));
liste.append("Rotation d'angle "+param1+" degrés.<br />");
}
}
i++;
}
display_mat('el');
coord.trans(mat);
}
else
liste.append("Pas de transformation.<br />");
$("h1").css("display", "block");
$("#liste_operation").css("display", "block");
$("#start_coord").css("display", "block");
$("#el").css("display", "block");
$("#result").css("display", "block");
var result = create("span");
setAttr(result, 'style', 'margin: 0 0 0 41%;');
coord.display(result)
append(getId('result'), create('br'));
append(getId('result'), result);
}
function reset()
{
var i = 1;
if (nb_trans > 0)
{
while (i <= nb_trans)
{
element = $("#"+i);
if (element.size != 0)
element.remove();
i++;
}
}
$("#coord-x").val('');
$("#coord-y").val('');
$("#liste_trans").val('0');
nb_trans = 0;
}
function init()
{
$(".button_valider").on("click", function () { if (check_param() == true) calcul(); });
$(".button_reset").on("click", function () { reset(); });
}
window.onload = function(){init();};