forked from mbanquiero/TallerAlgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws_1.htm
122 lines (90 loc) · 1.91 KB
/
ws_1.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
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var canvas;
var ctx;
var time = 0;
var elapsed_time = 0.01;
var vel = {x:10,y:0};
var pos = {x:100,y:100};
var radio = 30;
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;
// integro la velocidad
var D = mul(vel,elapsed_time);
pos = add(pos , D);
// 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>