Skip to content

Instantly share code, notes, and snippets.

@joac
Created June 5, 2013 22:12
Show Gist options
  • Save joac/5717766 to your computer and use it in GitHub Desktop.
Save joac/5717766 to your computer and use it in GitHub Desktop.
/*"basado en lo que llego a entender
* Hay cuatro puntos p1, p2, p3, p4 de traking y el punto k
* considero:
* p1----p2
* | |
* | |
* p3----p4
* |
* |
* k
* Lo que queremos es hallar un punto Q tal que es el punto mas cercano a k de la recta que pasa por p1p2
*
* p1 Q p2
*---+---+---+---------
* |
* |
* |
* |
* + k
*
*/
function dot_product(a, b) {
return a.x * b.x + a.y * b.y;
};
function cuadratic_norm(a) {
return Math.pow(a.x, 2) + Math.pow(a.y, 2);
};
function norm(a){
return Math.sqrt(cuadratic_norm(a));
}
function vector_sum(a, b) {
return dot(a.x + b.x, a.y + b.y);
};
function vector_diff(a, b) {
return dot(a.x - b.x, a.y - b.y);
};
/*
* Factory for make dots
*/
function dot(x, y) {
return {x:x, y:y};
};
function scalar_product(v, a) {
return dot(v.x * a, v.y *a);
}
function proyection(v, w) {
var a = dot_product(v, w) / cuadratic_norm(w);
return scalar_product(w, a);
};
function nearest_point(p, q, r) {
var pr = vector_diff(r, p),
pq = vector_diff(q, p),
py = proyection(pr, pq);
return vector_sum(p, py)
};
var p1 = dot(0, 10), p2 = dot(5, 5), k = dot(2, 3);
Q = nearest_point(p1, p2, k)
/* Para saber si la cabeza esta girada a izquierda o a la derecha, y la magnitud del giro
* hay que ver la resta entre el vector Qp1 y Qp2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment