#### Archived

This topic is now archived and is closed to further replies.

# GetDist and GetDir

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

## Recommended Posts

I have a formula for GetDist, but the problem is, there was no operator for multiplying the number times its self. (IE: x²) Here is what i ended up with:
long ffatom::GetDist(ffatom *target){
return (long)sqrt(((target->x-x)*(target->x-x)) + ((target->y-y)*(target->y-y)));
}

but i would like to know if there is an operator for x² Also, i need an equation that gives the direction from one atom to another. An atom has the X and Y variables, for storing location on the map (X and Y represent the TILE they are on, rather than the pixel location). I have no idea how to figure out direction, could anyone help? Thanks, --Fireking Owner/Leader Genetics 3rd Dimension Development

##### Share on other sites
Nope, no exponent operator. There is a power function, but the way you are doing it now is by far the fastest, easiest, most understandable way. Don''t sweat it

quote:
Also, i need an equation that gives the direction from one atom to another. An atom has the X and Y variables, for storing location on the map (X and Y represent the TILE they are on, rather than the pixel location).

Direction comes in several different forms. Are you looking for an angle, or just a vector describing the displacement? If it''s a vector, then (x2-x1,y2-y1) should do the trick. And once you have that, you can find the angle between them using atan( y2-y1 / x2-x1). Just keep in mind the domain and range of atan so you can find the real angle and not just some reference angle.

##### Share on other sites
There are two languages that have power-of operators. FORTRAN and Basic. In FORTRAN it would be:

x2 = x**2

And in Basic,

x2 = x^2

Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.

##### Share on other sites
sorry, i should have clarified

im talking about NORTH, SOUTH, EAST, WEST, NORTHWEST, NORTHEAST, SOUTHWEST, SOUTHEAST.

When you compare the two atoms, it should give you one of those. I know i can do it with a simple comparing x and y from target to x and y from source. But is that effecient? Or is there some sort of equation that can do this?

Thanks,

--Fireking

Genetics 3rd Dimension Development

##### Share on other sites
It is efficient. One solution that I proposed in another post was to compare the distance vector with the eight direction vectors using the dot product. If all 8 direction vectors are of equal length, then the direction vector that yields the largest dot product for a given vector is the one that is the closest.

Cédric

##### Share on other sites
If you have the angle between the two atoms, there is a much easier and faster way of computing the direction - (N,S,E,W,NE,NW,SE,SW).

Take your angle and divide it by PI/8. This logically divides your system into 16 sections and determines which 16th the other atom is in. If this is an odd number, we add 1. This number tells you the direction as follows:

0 = EAST
2 = NORTHEAST
4 = NORTH
6 = NORTHWEST
8 = WEST
10 = SOUTHWEST
12 = SOUTH
14 = SOUTHEAST

The reason we divide it into 16 parts and not 8 parts is because if the atom is in between two of the directions, we need a logical way of determining the better direction. For example, if our TRUE direction is ENE or greater (but still less than NE), then that would "round up" to NE when we add 1. Anything less than ENE, and it would "round down" to E because of the integer division.

Codewise, it would look something like this:
int direction = ((Angle*8) / PI);direction = (direction & 1) ? direction+1 : direction;

Then compare it against the table. Hopefully someone can think of some clever operator schematic that avoids the second conditional check. It''s too early in the morning for me

BTW, what''s great about this method is that it allows you to create as many directions as you want by decreasing the divisor. As long as EAST is 0, and each intermediary direction value Xn equals Xn-1 + 2, you can compute the closest direction.

##### Share on other sites
cant do that, i must use 0 - 7, because vital data is stored in array, and that information is accessed according to your direction, IE: atom.anim[dir][anim.currentframe].x or .y

to receieve the current animation frame of your direction

so i guess i will use a switch statement, and return 0 through 7

--Fireking

Genetics 3rd Dimension Development

##### Share on other sites
let me clarify that last post...

#define FF_VERSION 0x102010/*0  1  2		NW N NE                       3  +  4		W  +  E                       5  6  7		SW S SE*/                         #define NORTHWEST	0#define NORTH		1#define NORTHEAST	2#define WEST		3#define EAST		4#define SOUTHWEST	5#define SOUTH		6#define SOUTHEAST	7

--Fireking

Genetics 3rd Dimension Development

[edited by - fireking on September 18, 2002 5:42:00 PM]

1. 1
2. 2
3. 3
Rutin
24
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633650
• Total Posts
3013123
×