Tutorial – L铆nea de Visi贸n (Line of Sight)
Existen dos funciones auxiliares llamadas GetSlopeRow y GetSlopeCol que son las que calculan el incremento que deber谩 acumularse en la fila y la columna:
| float GetSlopeRow(int x1, int x2, int y1, int y2)
| {
| if (fabs((float) y2-y1) > fabs((float) x2-x1))
| return (y2>y1 ? 1 : -1);
| else
| {
| float slope = fabs((y2-y1) / (float) (x2-x1));
| slope *= (y2>y1 ? 1 : -1);
|
| return slope;
| }
| }
| float GetSlopeCol(int x1, int x2, int y1, int y2)
| {
| if (fabs((float) x2-x1) > fabs((float) y2-y1))
| return (x2>x1 ? 1 : -1);
| else
| {
| float slope = fabs((x2-x1) / (float) (y2-y1));
| slope *= (x2>x1 ? 1 : -1);
| return slope;
| }
| }
Notar que si el intervalo de columnas es mayor al de filas, entonces el incremento de columna es unitario. Mientras que si el intervalo de filas es mayor, entonces su incremento ser谩 unitario.
Ejemplos
Aplicando el algoritmo a un mapa de ejemplo obtendr铆amos los siguientes valores:
Siendo:
(x1, y1) = (1, 10)
(x2, y2) = (7, 1)
Los incremento y los valores iteraci贸n tras iteraci贸n ser铆an calculados como:
SlopeRow: -1
SlopeCol: 1.5
row: 9 col: 2.5
row: 8 col: 4
row: 7 col: 5.5
row: 6 col: 7
row: 5 col: 8.5
row: 4 col: 10
row: 3 col: 11.5
row: 2 col: 13
row: 1 col: 14.5
.
.
.
.
Ubicando a los puntos A y B en otras partes del mapa obtendr铆amos:
SlopeRow: 0.777778
SlopeCol: 1
row: 1.77778 col: 1
row: 2.55556 col: 2
row: 3.33333 col: 3
row: 4.11111 col: 4
row: 4.88889 col: 5
row: 5.66667 col: 6
row: 6.44444 col: 7
row: 7.22222 col: 8
row: 8 col: 9
.
.
.
.
Y finalmente:
Y en otra posici贸n:
SlopeRow: 0.555556
SlopeCol: -1
row: 1.55556 col: 8
row: 2.11111 col: 7
row: 2.66667 col: 6
row: 3.22222 col: 5
row: 3.77778 col: 4
row: 4.33333 col: 3
row: 4.88889 col: 2
row: 5.44444 col: 1
row: 6 col: 0
.
.
.
.
Los resultados podr铆an variar ligeramente si en lugar de utilizar floor como funci贸n de redondeo se utilizara ceil.
Pages: 1 2








