• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • 🏆 Hive's 6th HD Modeling Contest: Mechanical is now open! Design and model a mechanical creature, mechanized animal, a futuristic robotic being, or anything else your imagination can tinker with! 📅 Submissions close on June 30, 2024. Don't miss this opportunity to let your creativity shine! Enter now and show us your mechanical masterpiece! 🔗 Click here to enter!

[vJASS] Divide Rect

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,184
Basically I want to divide a rect into smaller ones. I managed to do this but I did not manage to make it dynamic.

At the moment the rect is divided into 4 parts, but I want this to be configurable.
I don't think it's that difficult to create but my brain just gets confused when creating this with loops for whatever reason.

JASS:
//The code is the converted edition from the jasshelper after I made a syntax error on purpose.
//It's originally written in Zinc which might be confusing to some so I think this is preferable?
function divide takes nothing returns nothing
        local rect r=corpsRegions[myCorps.size-1]
        local real xMax=GetRectMaxX(r)
        local real xMin=GetRectMinX(r)
        local real xDif=xMax-xMin
        local real yMax=GetRectMaxY(r)
        local real yMin=GetRectMinY(r)
        local real yDif=yMax-yMin
        set subRects[myCorps.size-1][0]=Rect(xMin,yMin,xMax-(xDif/2),yMax-(yDif/2))
        set subRects[myCorps.size-1][1]=Rect(xMin,yMin+(yDif/2),xMax-(xDif/2),yMax)
        set subRects[myCorps.size-1][2]=Rect(xMin+(xDif/2),yMin,xMax,yMax-(yDif/2))
        set subRects[myCorps.size-1][3]=Rect(xMin+(xDif/2),yMin+(yDif/2),xMax,yMax,s)
endfunction


JASS:
		function divide()
		{
			rect r = corpsRegions[myCorps.size - 1];
			real xMax = GetRectMaxX(r);
			real xMin = GetRectMinX(r);
			real xDif = xMax - xMin;
			real yMax = GetRectMaxY(r);
			real yMin = GetRectMinY(r);
			real yDif = yMax - yMin;
			subRects[myCorps.size - 1][0] = Rect(xMin,              yMin,              xMax - (xDif / 2), yMax - (yDif / 2));
			subRects[myCorps.size - 1][1] = Rect(xMin,              yMin + (yDif / 2), xMax - (xDif / 2), yMax);
			subRects[myCorps.size - 1][2] = Rect(xMin + (xDif / 2), yMin,              xMax,              yMax - (yDif / 2));
			subRects[myCorps.size - 1][3] = Rect(xMin + (xDif / 2), yMin + (yDif / 2), xMax,              yMax);
		}
I used the following image while working with the calculation, maybe that will clearify what I want.
f6ffe59d625b0d7f1481ed797eddc3ef.png
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
I guess something like this would do it, hope no mistakes.

if divX = 4 and divY = 7
rect[0] topright
rect[1] 1 below topright
rect[6] bottomright
rect[21] topleft
JASS:
function divide takes integer divX, integer divY returns nothing
        local rect r=corpsRegions[myCorps.size-1]

        local real xMax=GetRectMaxX(r)
        local real xMin=GetRectMinX(r)
        local real xDif=(xMax-xMin)/divX         

        local real yMax=GetRectMaxY(r)
        local real yMin=GetRectMinY(r)
        local real yDif=(yMax-yMin)/divY

        local integer index = 0
        local real maxx
        local real maxy
        
        // divX = 4, divY = 7 --> total 28 rects
        loop
            exitwhen divX == 0,
            
            loop
                exitwhen divY == 0
                // I assume rect function parameters were like this -> Rect(xmin, ymin, xmax, ymax)
                 
                // find the top right coordinates of new subrect
                set maxx = xMin + xDif * divX
                set maxy = yMin + yDif * divY

                set subRects[myCorps.size-1][index]=Rect(maxx - xDif, maxy - yDif, maxx, maxy)
                set index = index + 1
                set divY = divY - 1
            endloop
        set divX = divX - 1
        endloop
endfunction
 
Last edited:

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,184
I had to convert it to zinc in order for it to compile, but it did not work for me.
JASS:
		function divide(integer divX, integer divY)
		{
			rect r=corpsRegions[myCorps.size-1];

			real xMax=GetRectMaxX(r);
			real xMin=GetRectMinX(r);
			real xDif=(xMax-xMin)/divX;         

			real yMax=GetRectMaxY(r);
			real yMin=GetRectMinY(r);
			real yDif=(yMax-yMin)/divY;

			integer index = 0;
			real maxx;
			real maxy;
			
			// divX = 4, divY = 7 --> total 28 rects
			while(divX > 0)
			{
				while(divY > 0)
				{
					// I assume rect function parameters were like this -> Rect(xmin, ymin, xmax, ymax)
					 
					// find the top right coordinates of new subrect
					maxx = xMin + xDif * divX;
					maxy = yMin + yDif * divY;
					BJDebugMsg(I2S(myCorps.size - 1) + ", " + I2S(index));
					subRects[myCorps.size-1][index]=Rect(maxx - xDif, maxy - yDif, maxx, maxy);
					index += 1;
					divY -= 1;
				}      
			divX -= 1;
			}
		}

The debug message displays:
682bd21ae4ef17544e4845c897d4e610.png

But it should go to 0, 27

But I call:
divide(7,4);

EDIT:
HOLD UP A SEC, I think I screwed up the loops.. give me a min.

Edit2:
I screwed up the loops slightly it became exitwhen <= 0 instead of == 0, but it did not change the outcome.
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
I don't get what is wrong? It doesn't continue displaying after 0,3 ?

Edit: sorry I made a mistake reupload code 1 min.


JASS:
function divide takes integer divX, integer divY returns nothing
        local rect r=corpsRegions[myCorps.size-1]

        local real xMax=GetRectMaxX(r)
        local real xMin=GetRectMinX(r)
        local real xDif=(xMax-xMin)/divX         

        local real yMax=GetRectMaxY(r)
        local real yMin=GetRectMinY(r)
        local real yDif=(yMax-yMin)/divY

        local integer index = 0
        local real maxx
        local real maxy
        local integer divYbackup = divY
        
        // divX = 4, divY = 7 --> total 28 rects
        loop
            exitwhen divX == 0,
            
            loop
                exitwhen divY == 0
                // I assume rect function parameters were like this -> Rect(xmin, ymin, xmax, ymax)
                 
                // find the top right coordinates of new subrect
                set maxx = xMin + xDif * divX
                set maxy = yMin + yDif * divY

                set subRects[myCorps.size-1][index]=Rect(maxx - xDif, maxy - yDif, maxx, maxy)
                set index = index + 1
                set divY = divY - 1
            endloop
            set divY = divYbackup
            set divX = divX - 1
        endloop
endfunction
 
Status
Not open for further replies.
Top