open.mp forum
[Pawn] Center of GangZoneCreate() - Printable Version

+- open.mp forum (https://forum.open.mp)
+-- Forum: SA-MP (https://forum.open.mp/forumdisplay.php?fid=3)
+--- Forum: Pawn Scripting (https://forum.open.mp/forumdisplay.php?fid=10)
+--- Thread: [Pawn] Center of GangZoneCreate() (/showthread.php?tid=2474)



Center of GangZoneCreate() - JR_Junior - 2023-10-22

Hello! Could anyone help me get the exact coordinates of the center of a Gang Zone automatically?

PHP Code:
new Float:minx = 1248.011; new Float:miny = 2072.804; new Float:maxx = 1439.348; new Float:maxy = 2204.319; new gangzone = GangZoneCreate(minx, miny, maxx, maxy); new Float:centerx = ? new Float:centery = ?



RE: Center of GangZoneCreate() - denNorske - 2023-10-23

In order to get the center coordinates, you need to know what you already have. You have the two edges in both X and Y direction (Min and Max values).

The center will just be the following:

PHP Code:
new Float:centerx = (minx + maxx) / 2.0; new Float:centery = (miny + maxy) / 2.0;

That will result as

PHP Code:
centerx = 1343.679  centery = 2138.5615



RE: Center of GangZoneCreate() - JR_Junior - 2023-10-23

(2023-10-23, 05:44 AM)denNorske Wrote: In order to get the center coordinates, you need to know what you already have. You have the two edges in both X and Y direction (Min and Max values).

The center will just be the following:

PHP Code:
new Float:centerx = (minx + maxx) / 2.0; new Float:centery = (miny + maxy) / 2.0;

That will result as

PHP Code:
centerx = 1343.679  centery = 2138.5615

Worked perfectly! Thanks!
PHP Code:
stock GetZoneCenterPos(Float:minx,Float:miny,Float:maxx,Float:maxy,&Float:centerx,&Float:centery) {     centerx = ((minx + maxx) / 2);     centery = ((miny + maxy) / 2); }



RE: Center of GangZoneCreate() - denNorske - 2023-10-26

Glad to hear!

A neat approach would be to perhaps store the data into a ZoneData enum, and instead of using GangZoneCreate, you use a new function called CreateZone;

PHP Code:
#define MAX_GANG_ZONES 100  //adjust as necessary enum gangZoneProperties {     Float:center_x,     Float:center_y,     Float:min_x,     Float:min_y,     Float:max_x,     Float:max_y,     Float:min_z,     Float:max_z,     name[35] }; new zoneData[MAX_GANG_ZONES][gangZoneProperties]; forward GetZoneName(zoneid); forward CreateZone(Float:minx, Float:miny, Float:maxx, Float:maxy, Float:minz = 0.0, Float:maxz = 0.0); public CreateZone(const name[], Float:minx, Float:miny, Float:maxx, Float:maxy, Float:minz = 0.0, Float:maxz = 0.0) {     new zoneid = GangZoneCreate(minx, miny, maxx, maxy);         if (zoneid != INVALID_GANG_ZONE && zoneid < MAX_GANG_ZONES)     {         zoneData[zoneid][center_x] = (minx + maxx) / 2.0;         zoneData[zoneid][center_y] = (miny + maxy) / 2.0;         zoneData[zoneid][min_x] = minx;         zoneData[zoneid][min_y] = miny;         zoneData[zoneid][max_x] = maxx;         zoneData[zoneid][max_y] = maxy;         zoneData[zoneid][min_z] = minz;         zoneData[zoneid][max_z] = maxz;         format(zoneData[zoneid][name], 35, "%s", name);     }         return zoneid; } public GetZoneName(zoneid) {     if (zoneid >= 0 && zoneid < MAX_GANG_ZONES)     {         return zoneData[zoneid][name];     }     return ""; }

Then you can create the zones without having to worry about getting the zone coordinates every time you want to get the center of it. Instead you can do something like:

PHP Code:
forward GetZoneCenterXY(zoneid, &Float:center_x, &Float:center_y) public GetZoneCenterXY(zoneid, &Float:center_x, &Float:center_y) {     if (zoneid >= 0 && zoneid < MAX_GANG_ZONES)     {         center_x = zoneData[zoneid][center_x];         center_y = zoneData[zoneid][center_y];     } }

And then you can use it like this (for example)

PHP Code:
new Float:x, Float:y; GetZoneCenter(the_zone_id, x, y);