forked from mbanquiero/TallerAlgebra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
point_in_tri_test2.htm
183 lines (142 loc) · 3.2 KB
/
point_in_tri_test2.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
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
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var canvas;
var ctx;
//
// Point in Triangle Test
//
var A = {x:350, y:120};
var B = {x:50, y:530};
var C = {x:720, y:200};
var P = {x:50, y:100};
// Algebra de vectores
// -------------------------------------------------------------
function add(u , v)
{
return {x: u.x + v.x , y:u.y + v.y};
}
function substract(u , v)
{
return {x: u.x - v.x , y:u.y - v.y};
}
function substract3d(u , v)
{
return {x: u.x - v.x , y:u.y - v.y , z:u.z - v.z};
}
function mul(u , k)
{
return {x: u.x*k , y:u.y*k};
}
function dot(u , v)
{
return u.x*v.x+u.y*v.y;
}
function dot3d(u , v)
{
return u.x*v.x+u.y*v.y+u.z*v.z;
}
function length(u)
{
return Math.sqrt(u.x*u.x + u.y*u.y);
}
function normalize(u)
{
var len = length(u);
u.x /= len;
u.y /= len;
}
function normal(u)
{
return {x: -u.y , y:u.x};
}
function cross(u , v)
{
return { x: u.y * v.z - u.z * v.y ,
y: u.z * v.x - u.x * v.z ,
z: u.x * v.y - u.y * v.x };
}
// devuelve 1 si el punto p1 y el p2 estan del mismo lado del segmento a-b
function mismo_lado(p1,p2, a,b)
{
var rta = 0;
var U = cross(substract3d(b,a), substract3d(p1,a));
var V = cross(substract3d(b,a), substract3d(p2,a));
if(dot3d(U, V) >= 0)
rta = 1;
return rta;
}
function pto_inside(q,p1,p2,p3)
{
var rta = 0
// los paso a 3d con Z = 0
var p = {x:q.x , y:q.y , z:0};
var a = {x:p1.x , y:p1.y , z:0};
var b = {x:p2.x , y:p2.y , z:0};
var c = {x:p3.x , y:p3.y , z:0};
if(mismo_lado(p,a, b,c) && mismo_lado(p,b, a,c) && mismo_lado(p,c, a,b))
rta = 1;
return rta;
}
function draw()
{
if (canvas.getContext)
{
ctx.fillStyle = 'rgba(255,255,255,255)';
ctx.fillRect(0,0,1000,700);
var pto_adentro = pto_inside(P,A,B,C);
// dibujo el triangulo
ctx.fillStyle = pto_adentro ? 'rgba(255,255,0,255)' : 'rgba(255,0,0,255)';
ctx.beginPath();
ctx.moveTo(A.x, A.y);
ctx.lineTo(B.x, B.y);
ctx.lineTo(C.x, C.y);
ctx.lineTo(A.x, A.y);
ctx.closePath();
ctx.fill();
// dibujo los 3 puntos
ctx.fillStyle = 'rgba(120,50,50,255)';
ctx.beginPath();
ctx.arc(A.x, A.y, 5, 0, Math.PI*2,true);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.arc(B.x, B.y, 5, 0, Math.PI*2,true);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.arc(C.x, C.y, 5, 0, Math.PI*2,true);
ctx.closePath();
ctx.stroke();
// dibujo el punto
ctx.fillStyle = 'rgba(0,0,0,255)';
ctx.beginPath();
ctx.arc(P.x, P.y, 5, 0, Math.PI*2,true);
ctx.closePath();
ctx.fill();
ctx.font = "12px Arial";
ctx.fillText("A", A.x, A.y-10);
ctx.fillText("B", B.x, B.y-10);
ctx.fillText("C", C.x, C.y-10);
}
}
function onMouseMove()
{
P.x = window.event.offsetX;
P.y = window.event.offsetY;
draw();
}
function init()
{
canvas = document.getElementById('mycanvas');
ctx = canvas.getContext('2d');
document.addEventListener( "mousemove", onMouseMove, true);
draw();
}
</script>
</head>
<body onload="init();">
<canvas id="mycanvas" width="1000" height="700"></canvas>
</body>
</html>