-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmultiplication.html
More file actions
283 lines (260 loc) · 11 KB
/
multiplication.html
File metadata and controls
283 lines (260 loc) · 11 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
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
<link rel="stylesheet" href="https://code.getmdl.io/1.3.0/material.indigo-pink.min.css">
<link rel="stylesheet" href="style.css">
<script defer src="https://code.getmdl.io/1.3.0/material.min.js"></script>
<script type="text/javascript">
var ii = 0;
function drag(obj)
{
var obj = document.getElementById(obj);
var qo = obj.style.left;
var po = obj.style.top;
var co = obj.style.backgroundColor;
obj.orig_index=obj.style.zIndex;
obj.onmousedown=function (a){
ii++;
var div = document.createElement('div');
div.style.left = qo;
div.style.top = po;
div.style.height = obj.style.height;
div.style.width = obj.style.width;
div.style.backgroundColor = co;
div.style.position = "absolute";
div.style.border = "0.5px solid #F00";
div.setAttribute("id", "newD"+ii);
content.appendChild(div);
drag("newD"+ ii);
this.style.cursor="move";
this.style.zIndex=1000;
var d=document;
if(!a) a=window.event;
var x=a.clientX-document.body.scrollLeft-obj.offsetLeft;
var y=a.clientY-document.body.scrollTop-obj.offsetTop;
d.onmousemove=function(a){
if(!a) a=window.event;
obj.style.left=a.clientX+document.body.scrollLeft-x;
obj.style.top=a.clientY+document.body.scrollTop-y;
}
d.onmouseup=function (){
document.onmousemove=null;
document.onmouseup = null;
obj.style.zIndex = 100;
obj.style.cursor="normal";
var q = Math.round(obj.offsetLeft / 40) * 40;
var p = Math.round(obj.offsetTop / 40) * 40 ;
obj.style.left = q;
obj.style.top = p;
if (q < 400 || q > 1200 - obj.offsetWidth) {obj.style.left = qo; obj.style.top = po;}
if (p < 40 || p > 840 - obj.offsetHeight) {obj.style.left = qo; obj.style.top = po;}
}
}
}
</script>
</head>
<!-- Always shows a header, even in smaller screens. -->
<div class="mdl-layout mdl-js-layout mdl-layout--fixed-header">
<header class="mdl-layout__header" id="navbar">
<div class="mdl-layout__header-row">
<!-- Title -->
<span class="mdl-layout-title">Y2DHS Math Webapp</span>
<!-- Add spacer, to align navigation to the right -->
<div class="mdl-layout-spacer"></div>
<!-- Navigation. We hide it in small screens. -->
<nav class="mdl-navigation mdl-layout--large-screen-only">
<a class="mdl-navigation__link" href="index.html">Home</a>
<a class="mdl-navigation__link" href="summary.html">Summary</a>
<a class="mdl-navigation__link" href="multiplication.html">Multiplication Frame</a>
</nav>
</div>
</header>
<body>
<div class="mdl-layout__drawer mdl-layout--small-screen-only">
<span class="mdl-layout-title">Y2DHS Math Webapp</span>
<nav class="mdl-navigation">
<a class="mdl-navigation__link" href="index.html">Home</a>
<a class="mdl-navigation__link" href="summary.html">Summary</a>
<a class="mdl-navigation__link" href="multiplication.html">Multiplication Frame</a>
</nav>
</div>
<main class="mdl-layout__content">
<div class="page-content">
<div id="content">
<form name="form1" action="" style="font-size:200%">
<br>A / Coefficient of x<sup>2</sup><br>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" pattern="-?[0-9]*(\.[0-9]+)?" id="sample1" name="ap">
<label class="mdl-textfield__label" for="sample1">Number...</label>
<span class="mdl-textfield__error">Input is not a number!</span>
</div>
<br><br>B / Coefficient of x<br>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" pattern="-?[0-9]*(\.[0-9]+)?" id="sample2" name="bp">
<label class="mdl-textfield__label" for="sample2">Number...</label>
<span class="mdl-textfield__error">Input is not a number!</span>
</div>
<br><br>C / Constant<br>
<div class="mdl-textfield mdl-js-textfield">
<input class="mdl-textfield__input" type="text" pattern="-?[0-9]*(\.[0-9]+)?" id="sample3" name="cp">
<label class="mdl-textfield__label" for="sample3">Number...</label>
<span class="mdl-textfield__error">Input is not a number!</span>
</div>
</form>
<br>
<button class="mdl-button mdl-js-button mdl-button--fab mdl-js-ripple-effect mdl-button--colored" onclick="factorise(form1.ap.value,form1.bp.value,form1.cp.value)">
<i class="material-icons">done</i>
</button>
<button class="mdl-button mdl-js-button mdl-button--fab mdl-button--colored" onclick="refresh()">
<i class="material-icons">refresh</i>
</button>
<br><br>
<div id="demo" style="font-size: 20px;line-height: 50px;}"></div>
<canvas id="qw" width="1" height="1"></canvas>
<div id="div1" style="position: absolute; z-index: 100; width:79px; height:79px; left:1200px; top:50px; background-color:#C8FFFF; border:0.5px solid #F00"> </div>
<div id="div2" style="position: absolute; z-index: 101; width:79px; height:39px; left:1200px; top:135px; background-color:#C8FFFF; border:0.5px solid #F00"></div>
<div id="div3" style="position: absolute; z-index: 102; width:39px; height:79px; left:1285px; top:50px; background-color:#C8FFFF; border:0.5px solid #F00"></div>
<div id="div4" style="position: absolute; z-index: 103; width:39px; height:39px; left:1285px; top:135px; background-color:#C8FFFF; border:0.5px solid #F00"></div>
<div id="canv" style="position: absolute; z-index: 55; width:780px; height:780px; left:400px; top:40px; background-color:#white; border:1px solid black">
</div>
</main>
</div>
<script type="text/javascript">
var tArray = new Array();
for(var k=400;k<=1180;k++){
tArray[k]=new Array();
for(var j=40;j<=820;j++){
tArray[k][j]=0;
}
}
drag("div1");
drag("div2");
drag("div3");
drag("div4");
</script>
</body>
<script>
var x1z,x1m,x2z,x2m = 0;
var canvas = document.getElementById("qw");
var ctx = canvas.getContext("2d");
ctx.save();
ctx.setTransform(1, 0, 0, 1, 0, 0);
//refresh
function refresh()
{location.reload(true);}
/*function factor(m)
{
if (m
<0) {h = -m;} else {h = m;}
var n = [];var i = 2;var k = 1;
n[0] = 1;
while (h % i == 0)
{
n[k] = i;
k++;
h = Math.floor(h/i) ;
}
i = 3;
while (i < Math.round(Math.sqrt(Math.abs(m)))){
while (h % i == 0)
{n[k] = i;
k++;
h = Math.floor(h/i);}
i = i + 2;
}
if (h > 1) {n[k] = h;}
return n;
}*/
//find the roots of equation. Store the numerator and denominator.
function solve(q,w,e)
{
x1z = -w + Math.round(Math.sqrt(w * w- 4 * q * e));
x1m = 2 * q;
x2z = -w - Math.round(Math.sqrt(w * w- 4 * q * e));
x2m = 2 * q;
}
//factorise(ax2+bx+c)
function factorise(a,b,c)
{
document.getElementById("demo").innerHTML ="";
ctx.clearRect(0,0,canvas.width,canvas.height);
//judge the condition of a,b,c
if (a == 0) {document.getElementById("demo").innerHTML = "Error."+"<br>"+"Please make sure A does not equal to 0.";return;}
if (a % 1 != 0 || b % 1 != 0 || c % 1 != 0) {document.getElementById("demo").innerHTML ="Error."+"<br>"+"Please make sure all the coefficients are integer.";return;}
if (b*b-4*a*c<0) {document.getElementById("demo").innerHTML ="Cannot factorise."+"<br>"+"Please make sure the discriminant is not smaller than 0.";return;}
if (Math.sqrt(b*b-4*a*c) % 1 != 0) {document.getElementById("demo").innerHTML ="Sorry. The function fails to factorise the expression because the discriminant is not perfect square.";return;}
//when b=c=0
if (a != 1 && b == 0 && c == 0) {document.getElementById("demo").innerHTML =a+"x"+"<sup>2</sup>";}
if (a == 1 && b == 0 && c == 0) {document.getElementById("demo").innerHTML ="x"+"<sup>2</sup>";}
//ensure a is positive
if (a<0) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"-";
a = -a; b = -b; c = -c; var pin = -1;} else {var pin = 1;}
solve(a,b,c);
//record the result of factorisation
x1z = x1z / x1m;
x2z = x2z / x2m;
var num = 1;
while (x1z*num % 1 !=0) {num++;}
x1z = x1z * num;
x1m = num;
num = 1;
while (x2z*num % 1 !=0) {num++;}
x2z = x2z * num;
x2m = num;
pin = pin * a/x1m/x2m;
//output the result of factorisation
if (c!=0 || b!=0){
if (Math.abs(pin) != 1) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+Math.abs(pin);}
if (x1z != 0) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"(";}
if (x1m != 1) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+x1m+"x";}
else {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"x";}
if (x1z < 0) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"+"+ -x1z +")";}
if (x1z > 0) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"-"+ x1z +")";}
if (x2z != 0) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"(";}
if (x2m != 1) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+x2m+"x";}
else {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"x";}
if (x2z < 0) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"+"+ -x2z +")";}
if (x2z > 0) {document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML+"-"+ x2z +")";}
}
//output the illustration
var xhg = pin * x1m;lhg = -pin * x1z;xsg = x2m;lsg = -x2z;
line = xhg * 65 + lhg * 25;shu=xsg * 65 + lsg * 25;
canvas.height = shu + 20;
canvas.width = line;
ctx.moveTo(0,0);
if (xhg < 0 || lhg < 0 || xsg < 0 || lsg < 0) {return;}
for (var yp = 0; yp <= xhg; yp++)
{
ctx.moveTo(yp * 65 ,0);
ctx.lineTo(yp * 65 ,shu);
ctx.stroke();
}
for (yp = xhg + 1; yp-xhg <= lhg; yp++ )
{
ctx.moveTo((yp-xhg) * 25 + xhg * 65,0);
ctx.lineTo((yp-xhg) * 25 + xhg * 65,shu);
ctx.stroke();
}
for (yp = 0; yp <= xsg; yp++)
{
ctx.moveTo(0,yp * 65);
ctx.lineTo(line,yp * 65);
ctx.stroke();
}
for (yp = xsg + 1; yp-xsg <= lsg; yp++)
{
ctx.moveTo(0,(yp-xsg) * 25 + xsg * 65);
ctx.lineTo(line,(yp-xsg) * 25 + xsg * 65);
ctx.stroke();
}
div1.style.display = "";
div2.style.display = "";
div3.style.display = "";
div4.style.display = "";
canv.style.display = "";
}
</script>
</html>