Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm checking out this programming tutorial on raycasting at and have a probably very simple question about the math used.

I'm having trouble understanding the calcuation of deltaDistX and deltaDistY, which in a square grid are the distances between the x sides or the y sides of each square given a vector of a certain direction moving through them. See the screenshot below.

enter image description here

In the tutorial they are calculated as follows, but without much explanation:

//length of ray from one x or y-side to next x or y-side
double deltaDistX = sqrt(1 + (rayDirY * rayDirY) / (rayDirX * rayDirX));
double deltaDistY = sqrt(1 + (rayDirX * rayDirX) / (rayDirY * rayDirY));

Where rayDirY and rayDirX are the direction of a ray moving through the squares.

How do you get these formulas? It looks like pythagorean theorem is part of it, but somehow there's division involved here. Can anyone clue me in as to what mathematical knowledge I'm missing here, or "prove" the formula by showing how it's derived?

share|cite|improve this question
up vote 0 down vote accepted

The question was answered at the game dev section where I asked the same question.

Don't know if I should delete this thread or not since it's a duplicate. I'll keep it here in case anyone's interested and leave that up to the mods!

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.