Function
#1

Hello all, I came here with a question ^^. Does anybody haves a function which would calculate distance from point X to point Y?

For example,

Point X - SFPD
Point Y - LSPD.

It would calculate distance from SFPD to LSPD in metres.
Reply
#2

pawn Код:
//Distances:
new Float:Distances[3];
Distances[0] = floatsub(X1,X2);//LSPDX,SFPDX - X Offset Distance
Distances[1] = floatsub(Y1,Y2);//LSPDY,SFPDY - Y Offset Distance
Distances[2] = floatsub(Z1,Z2);//LSPDZ,SFPDZ - Z Offset Distance
Guessed, but in theory it should work
Reply
#3

Quote:
Originally Posted by coole210
Посмотреть сообщение
pawn Код:
//Distances:
new Float:Distances[3];
Distances[0] = floatsub(X1,X2);//LSPDX,SFPDX - X Offset Distance
Distances[1] = floatsub(Y1,Y2);//LSPDY,SFPDY - Y Offset Distance
Distances[2] = floatsub(Z1,Z2);//LSPDZ,SFPDZ - Z Offset Distance
Guessed, but in theory it should work
Utter fail. This is the correct way:
Код:
floatsqroot((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2));
That calculates distance in game units (I assume it's feet).To convert that to meters, divide it by 3.28083989501312.
Reply
#4

YJIET, thank you, but is it going to work?

For metres:

pawn Код:
new Float: Distance = floatsqroot((SFPD_X - LSPD_X) * (SFPD_X - LSPD_X) + (SFPD_Y - LSPD_Y) * (SFPD_Y - LSPD_Y) + (SFPD_Z - LSPD_Z) * (SFPD_Z - LSPD_Z)),
    Float: Metres = 3.28083989501312,
    Float: Result
;
Result = floatdiv(Distance / Metres);
printf("Distance from LSPD to LSPD (in metres): %d", Result);
For kilometres:

pawn Код:
new Float: Distance = floatsqroot((SFPD_X - LSPD_X) * (SFPD_X - LSPD_X) + (SFPD_Y - LSPD_Y) * (SFPD_Y - LSPD_Y) + (SFPD_Z - LSPD_Z) * (SFPD_Z - LSPD_Z)),
    Float: Metres = 3.28083989501312,
    Float: Kilometres,
    Float: Result
;
Result     = floatdiv(Distance, Metres);
Kilometres = floatdiv(Result, 1000);
printf("Distance from SFPD to LSPD (in kilometres): %d", Kilometres);
Reply
#5

Yeah, that works but you can make it simpler:
pawn Код:
//meter
distance = floatsqroot((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)) / 3.2808;  
//kilometer
distance = floatsqroot((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)) / 3.2808 / 1000.0;
Reply
#6

Okay, thank you very much. And one more question: the print will be like 5.555555555555, so If I want to show only first number after ., I must use 0.1f?
Reply
#7

San Andreas uses the metric system, so it is actually meters already.
Reply
#8

@BaubaS: Yes.
Quote:
Originally Posted by Vince
Посмотреть сообщение
San Andreas uses the metric system, so it is actually meters already.
You learn something new every day.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)