# Perform intersection (horizontal and vertical) checking in raycasting

I want to check the horizontal and vertical intersections on my raycasting view, and I can't exactly understand what is ALPHA using this drawing I wrote something that makes the walls straights and with less stairs on top and bottom, but i have some columns which go out of the view row, i can't figure out where i failed my operations (rounds)

this is the code of the vertical and the horizontal intersections :

static inline void      inter_hor(t_wolf3d *w, int i, double angle){
t_vec2f a; // first intersection point
t_vec2f o; // offset
double  tangent;

tangent = ttan(angle) + 0.001;
a.y = (angle <= 180) ? floor(w->cam->position.y / 32) *
32 - 1 : ceil(w->cam->position.y / 32) * 32 + 32;
o.y = (angle < 180) ? -32 : 32;
o.x = (angle < 90 || angle > 270) ? 32 / tangent : -32 / tangent;
a.x = (angle != 180.0 && angle < 359.97) ? w->cam->position.x + (w->cam->position.y - a.y) / tangent :
w->cam->position.x + (w->cam->position.y - a.y) / -tangent; //w->mini_h is the w->map->h  * 32 and so on for w->mini_w
a.x = (angle >= 179.97 && angle <= 180) ? w->cam->position.x + (w->cam->position.y - a.y) / -tangent : a.x;
if (ceil(a.x / 32) < 0 || ceil(a.x / 32) >= w->map->w)
a.x = (ceil(a.x / 32) < 0) ? 0 : w->mini_w - 1;
while (w->map->board[(int)a.y / 32][(int)a.x / 32] == 0)
{
a.x += ((angle >= 90 && angle < 180) || angle >= 270) ? -o.x : o.x;
a.y += o.y;
if (ceil(a.x / 32) < 0 || ceil(a.x / 32) >= w->map->w)
a.x = (ceil(a.x / 32) < 0) ? 0 : w->mini_w - 1;
if (a.y < 0 || a.y > w->mini_h - 1)
break;
}
w->cam->interh[i].x = a.x;
w->cam->interh[i].y = a.y;}
static inline void      inter_ver(t_wolf3d *w, int i, double angle){
t_vec2f a; // intersection point
t_vec2f o; // offset
double  tangent;

tangent = ttan(angle) + 0.001;
a.x = (angle >= 90 && angle < 270) ? ceil(w->cam->position.x / 32) *
32 - 1 : floor(w->cam->position.x / 32) * 32 + 32;
o.x = (angle > 90 && angle < 270) ? -32 : 32;
o.y = (angle > 0 &&  angle < 180) ? -32 * tangent : 32 * tangent;
a.y = (angle != 270 && angle != 90) ? w->cam->position.y + (w->cam->position.x - a.x) * tangent :
w->cam->position.y + (w->cam->position.x - a.x) * -tangent;
if (ceil(a.y / 32) < 0 || ceil(a.y / 32) >= w->map->h)
a.y = (ceil(a.y / 32) < 0) ? 0 : w->mini_h - 1;
while (w->map->board[(int)ceil(a.y / 32)][(int)a.x / 32] == 0)
{
a.y += ((angle > 90 && angle < 180) || angle > 270) ? -o.y : o.y;
a.x += o.x;
if (ceil(a.y / 32) < 0 || ceil(a.y / 32) >=  w->map->h)
a.y = (ceil(a.y / 32) < 0) ? 0 : w->mini_h - 1;
if (a.x < 0 || a.x > w->mini_w - 1)
break;
}
w->cam->interv[i].x = a.x;
w->cam->interv[i].y = a.y;}


I could use the dir.y / dir.x computed before for my tangent, but i didn't cause i had some divisions by zero when my rays was facing at 180, 360, 0 degrees, angles are complexes and i dont understand completely the vectors method to do that way; these are some screeshots :

• In this answer, I show how to march a ray through a 2D grid, rasterization-style. It looks like that might be relevant to the problem you're solving? – DMGregory Apr 2 '19 at 18:27
• I am looking at it, can you recommand me some good books to learn mathematics for computer graphics from beginners to masters? Cause I feel like when you give me codes you use some tricky advanced mathematics and I want to learn the way you find that! – Num Lock Apr 3 '19 at 15:03
• It's mostly high school linear algebra. You can find lots of resources about this online, or any standard linear algebra textbook will do. – DMGregory Apr 3 '19 at 15:51
• Well! Feels like the fact that i am a french native speaker has something to do with it! Notions and notations are quite differents – Num Lock Apr 3 '19 at 16:33

$$\\alpha\$$ is the angle between the X-axis (horizontal) and the ray you're casting.
$$\Tan(\alpha)\$$ is also called the slope, which is $$\opposite/adjacent\$$ so in the triangle on the right it's $$\Ya/Xa = Ya/64\$$, thus, if you know the slope and one intersection you can get the next intersection in the grid by adding $$\64\times Tan(\alpha)\$$ to they coordinate and $$\64\$$ to the x coordinate.