forked from mbanquiero/TallerAlgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_2.htm
154 lines (117 loc) · 2.37 KB
/
ws_2.htm
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
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var canvas;
var ctx;
var time = 0;
var elapsed_time = 0.01;
var vel = {x:-30,y:30};
var pos = {x:100,y:100};
var radio = 20;
var OX = 200;
var OY = 100;
var DX = 400;
var DY = 500;
// imagen
var img = new Image();
// Algebra de vectores
// -------------------------------------------------------------
// Suma de Vectores
// w = u + v
function add(u , v)
{
return {x: u.x + v.x , y:u.y + v.y};
}
// Resta de Vectores
// w = u - v
function substract(u , v)
{
return {x: u.x - v.x , y:u.y - v.y};
}
// Multiplicacion de Vectores
// w = u * k
function mul(u , k)
{
return {x: u.x*k , y:u.y*k};
}
// producto interno
function dot(u , v)
{
return u.x*v.x+u.y*v.y;
}
// norma o modulo
function length(u)
{
return Math.sqrt(u.x*u.x + u.y*u.y);
}
// normalizar
function normalize(u)
{
var len = length(u);
u.x /= len;
u.y /= len;
}
// computar el vector de reflexion
function reflect(i,n)
{
// v = i - 2 * dot(i, n) * n
return add(i , mul(n,-2*dot(i, n)));
}
//---------------------------------------------------------------------------------------------
function draw()
{
if (canvas.getContext)
{
ctx.fillStyle = 'rgba(255,255,255,255)';
ctx.fillRect(0,0,1000,700);
ctx.fillStyle = 'rgba(192,255,192,255)';
ctx.fillRect(OX ,OY ,DX,DY);
time+=elapsed_time;
// 1- integro la velocidad
var D = mul(vel,elapsed_time);
pos = add(pos , D);
// 2- verifico los limites
if(pos.x<radio)
{
// rebote izquierdo
pos.x = 2*radio - pos.x;
vel.x *= -1;
}
else
if(pos.x>DX-radio)
{
// rebote derecho
pos.x = (DX-radio)*2 - pos.x;
vel.x *= -1;
}
if(pos.y<radio)
{
// rebote abajo
pos.y = 2*radio-pos.y;
vel.y *= -1;
}
else
if(pos.y>DY-radio)
{
// rebote arriba
pos.y = (DY-radio)*2-pos.y;
vel.y *= -1;
}
// 3* dibujo la bolita
ctx.drawImage(img,OX + pos.x - radio, OY + pos.y - radio, 2*radio,2*radio);
}
}
function animate()
{
canvas = document.getElementById('mycanvas');
ctx = canvas.getContext('2d');
img.src = 'ball.png';
setInterval(draw, 1);
}
</script>
</head>
<body onload="animate();">
<canvas id="mycanvas" width="1000" height="700"></canvas>
</body>
</html>