• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] Center of GangZoneCreate()
#1
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 = ? 
  Reply
#2
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 
Away
  Reply
#3
(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);
} 
  Reply
#4
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); 
Away
  Reply