Public Group

# Stupid maths question

This topic is 4221 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, I have what must be a stupid maths question. I'm using a tutorial from http://www.riemers.net/eng/Tutorials/DirectX/C++/Series1/tut10.php The tutorial basically goes through creating terrain by drawing triangles using 2 for loops. The bit of code I don't understand is this bit: // int width, height = 10; // short S_Indices[(WIDTH-1)*(HEIGHT-1)*6] for (int x=0;x< WIDTH-1;x++) { for (int y=0; y< HEIGHT-1;y++) { s_Indices[(x+y*(WIDTH-1))*6+2] = x+y*WIDTH; s_Indices[(x+y*(WIDTH-1))*6+1] = (x+1)+y*WIDTH; s_Indices[(x+y*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH; s_Indices[(x+y*(WIDTH-1))*6+3] = (x+1)+(y+1)*WIDTH; s_Indices[(x+y*(WIDTH-1))*6+4] = x+y*WIDTH; s_Indices[(x+y*(WIDTH-1))*6+5] = x+(y+1)*WIDTH; } } From what I can tell, this should be where the triangles are defined in an IndexBuffer, using vertices predefined in a vertexbuffer. Question : Ok, my problem is just that I'm doing the maths wrong somehow, lets say, for example, that on one pass of the for loops, x=0 y=1. This should be the triangle that is drawn at 0,1, and it would also be the 11th (or 21st) triangle drawn. So S_Indices would be at [58] [57] [56] [59] [60] [61]. Let's then say that on the next pass x=1 y=0, this should be the second triangle drawn(along the x axis anyway) and SHOULD be in S_Indices[8] [7] [6]. I don't understand how the result for the following two examples are returning different values: // x=0 y=1 s_Indices[(0+1*(WIDTH-1))*6+2] = x+y*WIDTH; s_Indices[(0+1*(WIDTH-1))*6+1] = (x+1)+y*WIDTH; s_Indices[(0+1*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH; // x=1 y=0 s_Indices[(1+0*(WIDTH-1))*6+2] = x+y*WIDTH; s_Indices[(1+0*(WIDTH-1))*6+1] = (x+1)+y*WIDTH; s_Indices[(1+0*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH; Just to note, I have compiled the example from the web page and it works, I guess I'm just looking at it wrong.

##### Share on other sites
Hi!

x1 + y1*WIDTH always equals x2 + y2*WIDTH for x1 = x2 and y1 = y2.
In other cases this doesn't have to be true.
Specifically for
x1 = 0
x2 = 1
y1 = 1
y2 = 0
it's only true if WIDTH = 1

##### Share on other sites
Hi, thanks for the fast reply,

I think I understand what you mean, so basically, because the for loop starts with a 0, the x and y in the equation also start with 0 and not 1 ?

so:

1 = 0
2 = 1
3 = 2

and so on ...

or, did you mean that the -1 in the parenthesis (brackets or whatever :) ) don't just apply to the WIDTH, but to the whole sum ?

The only part of your reply I didn't get was the 'its only true if width = 1'

Thx

##### Share on other sites
Quote:
 Original post by LintfordPickleI don't understand how the result for the following two examples are returning different values: // x=0 y=1 s_Indices[(0+1*(WIDTH-1))*6+2] = x+y*WIDTH; s_Indices[(0+1*(WIDTH-1))*6+1] = (x+1)+y*WIDTH; s_Indices[(0+1*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH; // x=1 y=0 s_Indices[(1+0*(WIDTH-1))*6+2] = x+y*WIDTH; s_Indices[(1+0*(WIDTH-1))*6+1] = (x+1)+y*WIDTH; s_Indices[(1+0*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH;Just to note, I have compiled the example from the web page and it works, I guess I'm just looking at it wrong.

Just do it out by hand, and remember your order of operations:
When x = 0, y = 1:
s_Indices[(0+1*(WIDTH-1))*6+2] = x+y*WIDTH;
Will evaluate to:
s_Indices[(WIDTH-1)*6+2] = x+y*WIDTH;
Because the 0+ and the 1* drop out.

When x = 1, y = 0:
s_Indices[(1+0*(WIDTH-1))*6+2] = x+y*WIDTH;
Will evaluate to:
s_Indices[(1)*6+2] = x+y*WIDTH;
Because the 0*() drops out.

So,
(WIDTH-1)*6+2 != (1)*6+2
Unless WIDTH is 2, in which case the outer loop would terminate before the second iteration to begin with.

• 36
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631359
• Total Posts
2999545
×