SA-MP Forums Archive
Math issue... - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Math issue... (/showthread.php?tid=371457)



Math issue... (Middle point) - Michael@Belgium - 23.08.2012

How to get the middle point of a costum map ?
So i need some help with something new. I need to know how to get the middle pos of a costum map !

Currently using mysql and a 2 stocks:


pawn Код:
stock Spectating(playerid)
{
    new query[256];
    IsPlayerInRound[playerid] = 0;
    PlayerIsSpectating[playerid] = 1;
    PlayerTextDrawHide(playerid,HEALTH);
    PlayerTextDrawHide(playerid,SPEEDOS);
    PlayerTextDrawHide(playerid,NAME);
    TextDrawShowForPlayer(playerid,Textdraw4);
    TextDrawShowForPlayer(playerid,Textdraw5);

    if(SpecMod[playerid][Map] == true)
    {
        new Float:X,Float:Y,Float:Z;

        new Max[3][16], Min[3][16];
        format(query,sizeof(query),"SELECT  MIN(Xpos), MAX(Xpos), MIN(Ypos), MAX(Ypos), MIN(Zpos), MAX(Zpos) FROM `Map_Submits` WHERE `MapName` = '%s' ORDER BY RAND()",MapData[MapName]);
        mysql_query(query);
        mysql_store_result();
        mysql_retrieve_row();

        if(mysql_num_rows())
        {
            mysql_get_field("MIN(Xpos)",Min[0]);
            mysql_get_field("MAX(Xpos)",Max[0]);
            mysql_get_field("MIN(Ypos)",Min[1]);
            mysql_get_field("MAX(Ypos)",Max[1]);
            mysql_get_field("MIN(Zpos)",Min[2]);
            mysql_get_field("MAX(Zpos)",Max[2]);
        }
        mysql_free_result();
        print("DEBUG EX - mysql end");
        GetMidPoint(floatstr(Min[0]),floatstr(Min[1]),floatstr(Min[2]),floatstr(Max[0]),floatstr(Max[1]),floatstr(Max[2]),X,Y,Z);

        CallLocalFunction("CameraSpectate","ifff",playerid,X,Y,Z);
    }
//....

}

stock GetMidPoint(Float:minx,Float:miny,Float:minz,Float:maxx,Float:maxy,Float:maxz,&Float:x, &Float:y,&Float:z)
{
      x=floatdiv(floatadd(maxx,minx),2);
      y=floatdiv(floatadd(maxy,miny),2);
      z=floatdiv(floatadd(maxz,minz),2);
}
So it's something with the custom map, the objects of each map are stored in a database. As you see i need a few parameters for GetMidPoint, so getting the middle point of a map (it should). So ye i tried some mysql functions:

pawn Код:
format(query,sizeof(query),"SELECT  MIN(Xpos), MAX(Xpos), MIN(Ypos), MAX(Ypos), MIN(Zpos), MAX(Zpos) FROM `Map_Submits` WHERE `MapName` = '%s' ORDER BY RAND()",MapData[MapName]);
Example output:

Quote:

1360.4931640625 | 1394.9139404296875 | 2160.2431640625 | 2196.622802734375 | 9.522600173950195 | 10.234399795532227

I really dunno, but this is just the highest and the lowest value of x,y and z pos. And with that i tried GetMidPos()

And ye ofcourse, it doesn't work :-/

Maybe a few info that can help you solve this:


Re: Math issue... - scottyishere - 23.08.2012

Pythagoras (GREEN LINE)^2 = height^2 + otherline^2

x^2=x*x


Re: Math issue... - Niko_boy - 23.08.2012

mm nvm


Re: Math issue... - Michael@Belgium - 19.09.2012

I have a new question about math, topic edited.


Re: Math issue... - Sinner - 19.09.2012

Out of the top of my head, I'd say find the minX, maxX, minY, maxY, minZ, maxZ coordinates of your map (I see you did).

middleX = minX + ((maxX - minX) / 2)
middleY = minY + ((maxY - minY) / 2)
middleZ = minZ + ((maxZ - minZ) / 2)

It takes the difference between min and max (say min = 500, max = 700, diff = 200) and divides it by 2 (200/2 = 100) to get the middle point of the difference, then adds the min to get the middle point relative to the center of GTA (min = 500, 500 + 100 = 600, so your middle point for the given min and max would be 600). It does this for X, Y, Z. Tell me if it works.

EDIT: God fucking damnit ****** you beat me to it.


Re: Math issue... - Babul - 19.09.2012

a line consists 2 points only - i think some maps are built of more objects?
in that case, sum up all object coordinates per axis/dimension into 1 variable. at the end, divide those 3 values by the objects' amount.

Objects=100;//little map
pawn Код:
new Float:MidX,Float:MidY,Float:MidZ;
new Float:ObjX,Float:ObjY,Float:ObjZ;
for(new o=0;o<Objects;o++)
{
    GetObjectPos(o,ObjX,ObjY,ObjZ);
    MidX+=ObjX;
    MidY+=ObjY;
    MidZ+=ObjZ;
}
MidX/=Objects;
MidY/=Objects;
MidZ/=Objects;
MidX, MidY, MidZ equals the "middle" point on each axis per map.
not tested - but checked for typos, heh


Re: Math issue... - clarencecuzz - 19.09.2012

Quote:
Originally Posted by Sinner
Посмотреть сообщение
Out of the top of my head, I'd say find the minX, maxX, minY, maxY, minZ, maxZ coordinates of your map (I see you did).

middleX = minX + ((maxX - minX) / 2)
middleY = minY + ((maxY - minY) / 2)
middleZ = minZ + ((maxZ - minZ) / 2)

It takes the difference between min and max (say min = 500, max = 700, diff = 200) and divides it by 2 (200/2 = 100) to get the middle point of the difference, then adds the min to get the middle point relative to the center of GTA (min = 500, 500 + 100 = 600, so your middle point for the given min and max would be 600). It does this for X, Y, Z. Tell me if it works.

EDIT: God fucking damnit ****** you beat me to it.
Isn't finding a mid-point just:
(MaxX + MinX) / 2
(MaxY + MinY) / 2
(MaxZ + MinZ) / 2
??

That's how we were taught. I won't argue with ******' code for obvious reasons, but I'm sure this is how you find mid-points.


Re: Math issue... - Michael@Belgium - 19.09.2012

Quote:
Originally Posted by ******
Посмотреть сообщение
The midpoint of a line is:

Код:
mid = (max - min) / 2 + min
Just apply that in multiple dimensions.
Quote:
Originally Posted by Sinner
Посмотреть сообщение
Out of the top of my head, I'd say find the minX, maxX, minY, maxY, minZ, maxZ coordinates of your map (I see you did).

middleX = minX + ((maxX - minX) / 2)
middleY = minY + ((maxY - minY) / 2)
middleZ = minZ + ((maxZ - minZ) / 2)

It takes the difference between min and max (say min = 500, max = 700, diff = 200) and divides it by 2 (200/2 = 100) to get the middle point of the difference, then adds the min to get the middle point relative to the center of GTA (min = 500, 500 + 100 = 600, so your middle point for the given min and max would be 600). It does this for X, Y, Z. Tell me if it works.

EDIT: God fucking damnit ****** you beat me to it.
Well, for now this is the best solution. It works more than previously, ofcourse it has disadvantages also. Using this on custom maps above the sea, it works perfect. But just a map on main land, with a few objects, doesn't work so good. It's not so bad, but i just spectate something else.