Function -
BaubaS - 11.03.2012
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.
Re: Function -
coole210 - 11.03.2012
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
Re: Function -
Stylock - 11.03.2012
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.
Re: Function -
BaubaS - 11.03.2012
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);
Re: Function -
Stylock - 11.03.2012
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;
Re: Function -
BaubaS - 11.03.2012
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?
Re: Function -
Vince - 11.03.2012
San Andreas uses the metric system, so it is actually meters already.
Re: Function -
Stylock - 11.03.2012
@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.