# Finding the X,Y Center using a script

Please take a moment to consider if this thread is worth bumping.

## Recommended Posts

Hi, im not realy a scriptor but i know what i wanna do.

can somebody help me to find the x,y center of a peice of land using a script

this working code below already finds the land borders, so what we need to do is

find the point of intersection

```float gJumpAmount = 0.5;

// *************************************************
// Find a land border by searching out from a
// specific coordinate for a change in land ownership.
// *************************************************
vector findBorder(string axis, vector start_vec, float jump_amount)
{
float x = 0;
float y = 0;
float z = 0;
float num = 0;
vector newVec = ZERO_VECTOR;
vector lastVec = ZERO_VECTOR;
key owner1 = NULL_KEY;
key owner2 = NULL_KEY;
//
// Validate axis
//
if ((axis != "x") && (axis != "y"))
{
// llSay(0,"Invalid axis passed to findBorder()");
return ZERO_VECTOR;
}

//
// Validate jump_amount
//
if ((integer)jump_amount >= 255)
{
// llSay(0,"Invalid jump amount");
return ZERO_VECTOR;
}
//
// Validate start_vec
//
x = getVectorAxis(0,start_vec);
y = getVectorAxis(1,start_vec);
z = getVectorAxis(2,start_vec);
if (x <= 0 || y <= 0 || z <= 0)
{
// llSay(0,"Invalid vector passed to findBorder()");
return ZERO_VECTOR;
}

if (x >= 255 || y >= 255 || z >= 255)
{
// llSay(0,"Invalid vector passed to findBorder()");
return ZERO_VECTOR;
}
//
// Are we too close to a sim border?
//
if (axis == "x")
{
if ((x + jump_amount >= 255) ||(x + jump_amount <= 0))
{
return ZERO_VECTOR;
}
}

else
{
if ((y + jump_amount >= 255) ||(y + jump_amount <= 0))
{
return ZERO_VECTOR;
}
}
//
// Get owner of land at start vector
//
owner1 = llGetLandOwnerAt(start_vec);
if (owner1 == NULL_KEY)
{
// llSay(0,"Could not determine land owner at the location");
return ZERO_VECTOR;
}
//
//
lastVec = start_vec;
if (axis == "x")
{
num = x;
}

else
{
num = y;
}
//
// Loop through until we find a different landowner
//
while ((num >= 0) && (num <= 255))
{
if (axis == "x")
{
newVec = <num, y, z> + <jump_amount, 0, 0>;
}

else
{
newVec = <x, num, z> + <0, jump_amount, 0>;
}
owner2 = llGetLandOwnerAt(newVec);
if ((owner2 == NULL_KEY) || (owner1 != owner2))
{
return lastVec;
}
num += jump_amount;
if ((num <= 0) || (num >= 255))
{
return lastVec;
}
lastVec = newVec;
}
return ZERO_VECTOR;
}

// *************************************************
// Return a particular axis from a vector
// *************************************************
float getVectorAxis(integer axis, vector vec)
{
if ((axis < 0) || (axis > 2))
{
return -1;
}

list junk = llParseString2List((string)vec,[",","<",">"], []);
if (llGetListLength(junk) != 3)
{
return -1;
}
return llList2Float(junk,axis);
}

//
// default state starts here
//
default
{
state_entry()
{
llSay(0,"Touch the object to determine borders");
}

touch_start(integer num_detected)
{
// Uncomment to disallow anyone but the owner to use it
//if (llDetectedKey(0) != llGetOwner())
//{
// return;
//}

vector BOUND_EAST = ZERO_VECTOR;
vector BOUND_NORTH = ZERO_VECTOR;
vector BOUND_SOUTH = ZERO_VECTOR;
vector BOUND_WEST = ZERO_VECTOR;
vector HOME_POS = llGetPos();
//
// Determine the northern boundary
//
BOUND_NORTH = findBorder("y",HOME_POS,gJumpAmount);
if (BOUND_NORTH == ZERO_VECTOR)
{
llSay(0,"Failed to locate northern boundary");
llResetScript();
}
//
// Determine the southern boundary
//

BOUND_SOUTH = findBorder("y",HOME_POS,-gJumpAmount);
if (BOUND_SOUTH == ZERO_VECTOR)
{
llSay(0,"Failed to locate southern boundary");
llResetScript();
}
//
// Determine the eastern boundary
//
BOUND_EAST = findBorder("x",HOME_POS,gJumpAmount);
if (BOUND_EAST == ZERO_VECTOR)
{
llSay(0,"Failed to locate eastern boundary");
llResetScript();
}
//
// Determine the western boundary
//
BOUND_WEST = findBorder("x",HOME_POS,-gJumpAmount);
if (BOUND_WEST == ZERO_VECTOR)
{
llSay(0,"Failed to locate western boundary");
llResetScript();
}
//
// Format some results for displaying
//
float borderNorth = getVectorAxis(1,BOUND_NORTH);
float borderSouth = getVectorAxis(1,BOUND_SOUTH);
float borderEast = getVectorAxis(0,BOUND_EAST);
float borderWest = getVectorAxis(0,BOUND_WEST);
float zCoord = getVectorAxis(2,HOME_POS);
vector vectorNW = <borderNorth, borderWest, zCoord>;
vector vectorNE = <borderNorth, borderEast, zCoord>;
vector vectorSE = <borderSouth, borderEast, zCoord>;
vector vectorSW = <borderSouth, borderWest, zCoord>;
//
// Display the results
//

llSay(0,"");
llSay(0,"Northern Boundary Detected: " + (string)BOUND_NORTH);
llSay(0,"Southern Boundary Detected: " + (string)BOUND_SOUTH);
llSay(0,"Eastern Boudary Detected: " + (string)BOUND_EAST);
llSay(0,"Western Boundary Detected: " + (string)BOUND_WEST);
llSay(0,"");
llSay(0,"Northern Border Line: " + (string)borderNorth);
llSay(0,"Southern Border Line: " + (string)borderSouth);
llSay(0,"Eastern Border Line: " + (string)borderEast);
llSay(0,"Western Border Line: " + (string)borderWest);
llSay(0,"");
llSay(0,"Northwest Coords: " + (string)vectorNW);
llSay(0,"Northeast Coords: " + (string)vectorNE);
llSay(0,"Southeast Coords: " + (string)vectorSE);
llSay(0,"Southwest Coords: " + (string)vectorSW);
}

on_rez(integer num)
{
llResetScript();
}
}     // end```

##### Share on other sites

If the land is rectangular and it looks like that is the only case you are dealing with, then:

`vector center = 0.5*(vectorNE + vectorSW);`

##### Share on other sites

I would suppose it depends on a definition of "center". While trivial for rectangular parcels as Dora noted, it may not be trivial for other shapes, like L-shape, T-shape, etc. I made a gadget that maps a parcel or a region as a matrix of 10x10 cells for any parcel shape. Then finding a center cell is a matter of its definition.

##### Share on other sites

Whats the purpose of this?

Finding the center of a rectangle is trivial as described above.

The center of an L-shape for example can be outside of the land - not usable for placing an object then.

##### Share on other sites

Yeah, this comes up periodically, and although it's trivial for rectangular parcels, the general case is very ugly. At one point, like Eva, I had a script that scans the entire region for pieces of a parcel (I used 4x4 cells), but there are many cases where the centroid of the parcel-matching cells is not itself inside the parcel, as Nova mentions.  (Indeed, the reason it's necessary to scan the entire region is that a parcel may be not only concave but even disjoint.)