Advertisement

Damn maths.

Started by February 25, 2003 04:51 AM
13 comments, last by Tooko 21 years, 8 months ago
Damn maths, why''d I have to fail. Oh, hi. I was wondering, I have a 2D array (eg char c[5][5]) and I was wondering. If I have a point (say, 1,6) what formula can I use to see how far away that is from the center point? Say, if I had a function with the following definition:
int GetDist(int X, int Y, int MiddleX, int MiddleY) 
How would the code look? I know I sound like a noob @ programming, but I just can''t work it out. I''m really, no good when it comes to maths. Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
r = √((X - MiddleX)² + (Y - MiddleY)² )

Pythagorus.

[edited by - Useless Hacker on February 25, 2003 6:06:06 AM]
[ PGD - The Home of Pascal Game Development! ] [ Help GameDev.net fight cancer ]
Advertisement
Draw the problem on a piece of paper and the solution should hit you in the face (Hint: starts with ''P'' ends in ''oras'')

Skizz
How would I write that (eg the indicies etc) in C++? Sorry, but I''m kinda... dumb.

Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
Well, if your function prototype is really going to be what''s in your original post, you don''t need to index into an array at all. Hell, you don''t even need to have an array. It''s basic algebra + the Pythagorean theorem.

500x7 >_<
I''m a little confused as to why there is an array there in the first place too I don''t think you need it. But like the people above mentioned, definetly take a look at the pythagorean theorem (seeing as you you have a difference in x''s, and a difference in y''s, it should be easy to find the distance)

Make sure to use variables of type float for this too; I often found myself (and still do, to some degree ) defaulting to type int.
Peon
Advertisement
No, I''m not using an array. I was having trouble of how to word the question, so the word ''array '' came to mind. Sorry...

Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
Now I''m even more confused because you wrote:

quote: Oh, hi. I was wondering, I have a 2D array (eg char c[5][5]) and I was wondering...


That char c[5][5] is definetly an array Is that part of the original questions, or... ?
Peon
Ok, let me rephrase the question. If I had four integers (int X1,Y1,X2,Y2) how can I get the distance between the two?
If that is still confusing, yeah.

Yo Tyler, I hear your mum''s goin out with... SQUEEK!
the future is just like the past, just later. - TANSTAAFL
Quite easy
sqrt((X2-X1)<sub>2</sub> + (Y2-Y1)<sub>2</sub>

This topic is closed to new replies.

Advertisement