get closest point?
#1

e,g i have 4 points, i want to get the one closest to the player
Reply
#2

You can use a for loop for that

Pseudo:
pawn Код:
new Float:min = 9999.9;
new minindex;
new Float:distance;
new Float:px, Float:py, Float:pz;
GetPlayerPos(playerid, px, py, pz);
// pointx/y/z[] is an array, that contains all the points you want to get the nearest from
for(new i = 0; i < sizeof(pointx); i ++)
{
    distance = PointToPoint(px, py, pz, pointx[i], pointy[i], pointz[i]);
    //Change this to your function to get the distance
    if(distance < min)
    {
        minindex = i;
        min = distance;
    }
}
Reply
#3

whats pointtopoint? this?

pawn Код:
stock Float:GetDistanceBetweenPoints(Float:x1, Float:y1, Float:z1, Float:x2, Float:y2, Float:z2)
{
    x1 -= x2;
    y1 -= y2;
    z1 -= z2;
    return floatsqroot((x1 * x1) + (y1 * y1) + (z1 * z1));
}
btw i want to return the x and y of the closest point
Reply
#4

Yes, you can use that function, it does the same.
Reply
#5

did you make a mistake?

pawn Код:
distance = PointToPoint(px, py, pz, ClosestBurgerShots[i], ClosestBurgerShots[i], ClosestBurgerShots[z]);
u put 2 i's and one z. im just wondering where did the z come from, and

pawn Код:
if(distance < min)
                    {
                        minindex = i;
                        min = distance;
                        new Loc[MAX_ZONE_NAME];
                        Get2DZone(Loc,ClosestBurgerShots[i], ClosestBurgerShots[i], ClosestBurgerShots[z]);
                        SetPlayerRouting(playerid, 1559.5081, -1290.5549,"Burger Shot In %s",Loc);
                    }
how do i set the routing to the closest one?
//edit also
pawn Код:
new Float:ClosestBurgerShots[][3] = {
{2366.3240,2071.0330,10.8203},
{2472.1331,2034.2130,11.0625},
{1873.4980,2071.7546,11.0625}
};
Reply
#6

Oops, yep, the z is wrong, corrected it.

In your case, the x/y/z are part of the array too, so you have to do it like this:

distance = PointToPoint(px, py, pz, ClosestBurgerShots[i][0], ClosestBurgerShots[i][1], ClosestBurgerShots[i][2]);

so that the x/y/z values of every point are used.
After using the code, ClosestBurgerShots[minindex] will be the closest point, so use your code after the for loop:

pawn Код:
//the whole code above here

new Loc[MAX_ZONE_NAME];
Get2DZone(Loc,ClosestBurgerShots[minindex][0], ClosestBurgerShots[minindex][1], ClosestBurgerShots[minindex][2]);
SetPlayerRouting(playerid, 1559.5081, -1290.5549,"Burger Shot In %s",Loc);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)