2
$\begingroup$

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: https://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.

  • 0
    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.2011-04-28
  • 0
    Oh I think I see now... SW is the step width, bugger steps scales. Ok.2011-04-28
  • 0
    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.2011-04-28
  • 0
    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.2011-04-28
  • 2
    Just curious.. why $sw = dw / (w - 1)$?2011-04-29
  • 0
    The explanation for that is on the Stack Overflow post, but you do need to have some insight on heightmaps and how they are constructed. But that's irrelevant, I'm sure that formula is correct. I just need to find the right formula to calculate `nx` given that step calculation. In simple words, I need to stretch the whole thing by 1 unit and that will do the trick.2011-04-29
  • 0
    This is "basically a math problem" vs. a "programming problem". A "basic math problem", e.g., is thinking about $\lim_{n\to\infty}(1+{1\over n})$.2011-04-29
  • 0
    You wouldn't know this is a programming problem if I had never mentioned it. The problem described here could probably be applied to many different things.2011-04-29
  • 0
    BTW, the expression is "strong suit" (comes from card games like bridge), not "strong suite". I stared at the phrase for a long time before I realised what was bothering me. :-)2011-04-29

2 Answers 2

1

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$.

  • 0
    Because I tested it and it doesn't work as expected. That's why I thought I was missing something else...2011-04-29
  • 0
    @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.2011-04-29
0

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!

  • 0
    I never mentioned any kind of error, there's no error.2011-04-28