# How to solve this basic math problem?

I'm having this problem on some game I'm coding which I think it's just math and has nothing to do with coding, that's why I'm posting it here and I'm going to simplify the problem without describing anything related to the actual code. In case you want to take a look, please see this question on Stack Overflow: http://stackoverflow.com/questions/5822740/how-to-correctly-get-the-terrain-height-at-point-x-z-in-a-scaled-terrain

Let's say I want to draw a line on screen, with width w. To draw this line I need to do it in steps, sw, by default 1 unit steps. But I do allow this width to be scaled to any other value. After selecting the new width, dw, the new step needs to be calculated and I do it like this: sw = dw / w. With this, the line is properly scaled and I can draw it just fine.

Now, for a given x of that line I need to do some other calculations. Since I allowed the line to be scaled and it's not the original width. I need to calculate the real x, nx, before anything else. I just need to work on the step value calculated before, like this: nx = x / sw.

This will give me exactly the right nx value I'm looking for. Everything's working so far.

Now, my real problem is when I need to introduce a little change to the step calculation. Instead of calculating the step like I said before (sw = dw / w) I need to calculate it like this: sw = dw / (w - 1).

The problem I'm actually having is in the next step. Given x, how do I correctly calculate nx as I did before? Math is not my strong suite and I tried many things that I would think were right but it was mostly trial and error. Suffice to say, nothing worked.

-
I'm not familiar with the elements you are able to draw, can you draw a line one unit wide. I would think it'd be sw=dw*w... That means I don't know your notation, can you explain, or just tell me what sort of reference I should look at? Thanks I'm curious and might even be able to help if I knew what was going on. –  a little don Apr 28 '11 at 19:57
Oh I think I see now... SW is the step width, bugger steps scales. Ok. –  a little don Apr 28 '11 at 19:59
I'm not sure I understand your question. I basically have a default w and sw, let's say 100 and 1. This will draw a line from 0 to 100 in steps of 1 (think of it as points in the line). If I want to scale the line to 150, sw will become 1.5. I still draw the line with points from 0 to 100 but now in steps of 1.5, which will make the line end at 150 and not 100 as before. For a given x, for instance, 10. What's nx? Without scaling, x=10 => nx=10, x=100 => nx=100. With scaling, x=10 => nx=6.66, x=150 => nx=100. This is what I want. –  Ricardo Amaral Apr 28 '11 at 20:28
Now I just need to do the same but with that different step calculation as I mentioned in the question. I'm sorry, I can't explain any better than this. –  Ricardo Amaral Apr 28 '11 at 20:30
Just curious.. why $sw = dw / (w - 1)$? –  Tpofofn Apr 29 '11 at 1:05

I am not sure that I understood your question correctly, but what makes you think that the very same expression

nx = x / sw


does not work?

After all, the only change is that you replaced an arbitrary value $w$ by an arbitrary value $w-1$.

-
Because I tested it and it doesn't work as expected. That's why I thought I was missing something else... –  Ricardo Amaral Apr 29 '11 at 15:39
@Nazgulled: Why doesn't it work as expected? You want to get a total "width" (which it seems is more appropriately called "length", but nevermind) of x, and each step is sw units, so the number of steps nx is nx = x / sw, since sw units x/sw times will give x. So this expression is the logically correct one — assuming that I've understood the rest of your question. Or else clarify. –  ShreevatsaR Apr 29 '11 at 21:24

Might it be that $w$ sometimes equals one ? Then $w - 1$ would yield zero and trigger a division by zero error. Else, it'd help if you told us exactly what kind of error you're getting. I, for once, have no clue.

Good luck with that code!

-
I never mentioned any kind of error, there's no error. –  Ricardo Amaral Apr 28 '11 at 20:32