Split Square Into Smaller Squares
#1

Hi

I'm looking for some code to basically split a square into lots of equal sized squares, rectangles would also work.
I need this so I can quickly and rapidly make a set of gangzones.

If I can figure out how to make a function, input the max x & y, and minimum x & y, and it out puts a bunch of co-ords for the max x & y and min x & y of smaller squares, for the gangzones.

Anyone any idea how I would achieve this sort of math in Pawn?

- Cheers
Reply
#2

You basically just have to split the rectangle/square into X parts, take that length and multiply it by X + the base coords to get the Xth square coordinates.

eg.

To split a 100.0, 100.0 sqaure into 10 squares you need to divide the coords by 10, which results in 10.0, 10.0.
To get the 5th square on the X axis you would do 5x 10.0 to get the minX value, add 10.0 to get maxX for that square.

That would result in the coords 50.0, 0.0, 60.0, 10.0 (minX, maxX, minY, maxY).

Код:
splitsquares(Float:sizeX = 100.0, Float:sizeY = 100.0, num = 5)
{
new Float:offsetX = sizeX / num, Float:offsetY = sizeY / num;

for(new x = 0; x < num; x ++) for(new y = 0; y < num; y ++)
{
new Float:minX = x * offsetX, Float:maxX = minX + sizeX;
new Float:minY = y * offsetY, Float:maxY = minY + sizeY;

// Do stuff with minX, maxX, minY, maxY...
}
}
Reply
#3

Quote:
Originally Posted by NaS
Посмотреть сообщение
Код:
splitsquares(Float:sizeX = 100.0, Float:sizeY = 100.0, num = 5)
{
new Float:offsetX = sizeX / num, Float:offsetY = sizeY / num;

for(new x = 0; x < num; x ++) for(new y = 0; y < num; y ++)
{
new Float:minX = x * offsetX, Float:maxX = minX + sizeX;
new Float:minY = y * offsetY, Float:maxY = minY + sizeY;

// Do stuff with minX, maxX, minY, maxY...
}
}
Correct me if wrong, but isn't that code going to generate 25 squares?

EDIT: I mean, I don't think that would work with a strict amount of squares.
Reply
#4

Quote:
Originally Posted by Marricio
Посмотреть сообщение
Correct me if wrong, but isn't that code going to generate 25 squares?
Yes, because 5x5 is 25 (which is correct for 5x5 zones).
Reply
#5

Quote:
Originally Posted by NaS
Посмотреть сообщение
Yes, because 5x5 is 25.
Welp, I'm assuming the "n" argument in your function is actually the square amount.
Reply
#6

Quote:
Originally Posted by Marricio
Посмотреть сообщение
Welp, I'm assuming the "n" argument in your function is actually the square amount.
Oh yea it's the amount of squares on the axes, if it should be for different amount of rectangles per axis it just requires a second num variable, one for X and one for Y.
Reply
#7

Would be nice if I could also specify the amount of squares to create - which would in turn also control how big each square is. IE 100 squares will be a lot smaller than 50 squares.

I'm all a little confused, algebra is not fortй at all
Really grateful for your replies so far.
Reply
#8

Quote:
Originally Posted by Redirect Left
Посмотреть сообщение
Would be nice if I could also specify the amount of squares to create - which would in turn also control how big each square is. IE 100 squares will be a lot smaller than 50 squares.

I'm all a little confused, algebra is not fortй at all
Really grateful for your replies so far.
If you want 5 from an equation 5*5, you must get the square root. So, to get the number 5 like in my example, you have to calculate the square root of 25.

If you want 64 squares, it's 8 since 8*8 = 64, etc.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)