Combine two rectangles - find new length and height

Hi

I struggle with this little math issue.

I have one big rectangle and I want it to “eat” another smaller rectangle. When the big one does this, I want him to grow the size of the little rectangle he just ate.

But I cant get my head around how. Can anyone give me a hand? :slight_smile:

problem might need to be a bit better defined, since you said “rectangles” instead of “squares”, then it’ll  be straightforward to answer.

so, larger rectangle’s total area should become sum of smaller+larger rectangles’ areas, correct?

old_large_area = large.width * large.height

old_small_area = small.width * small.height

new_large_ area = old_large_area + old_small_area

that’s the easy part, but you still have some sort of unspecified constraint left to deal with before you can convert that new area into the new width/height of the large rect…  (if they were squares then it’d just be a sqrt())

what “shape” must the larger rectangle maintain?  preserve large rect’s original aspect ratio?  some weighted-by-area percentage (lerp) toward the aspect ratio of the smaller rect?  or straight average of the two?  or???

You know, I’ll try to just make a new square, it might work just fine. And I see that figuring out the new shape of two rectangles can be tricky.

Thanks! :slight_smile:

@christian.fagerland

Do you actually want the Rectangle / Square to actually grow the entire area of the smaller one eaten object?

Maybe a simpler approach would be to just get the height and width of the smaller object - divide them in half, and add it to the height and width for the objects new size.

Maybe something like this.

local myBigRectangle = display.newRect( 0, 0, 150, 50 ) local mySmallRectangle = display.newRect( 0, 0, 15, 50 ) --- your collision function here -- local sH = myBigRectangle.height + ( mySmallRectangle.height /2 ) local sW = myBigRectangle.width + ( mySmallRectangle.width /2 ) -- Used transition.to as an effect only I think you can set the properties directly if you want transition.to( myBigRectangle , { time=1000, height=sH , width= sW } )

Unless your doing something that requires actual duplication of size ( like some kids teaching app ) this should give you the effect you are looking for ( at least that’s my theory ).

Good Luck,

Larry

problem might need to be a bit better defined, since you said “rectangles” instead of “squares”, then it’ll  be straightforward to answer.

so, larger rectangle’s total area should become sum of smaller+larger rectangles’ areas, correct?

old_large_area = large.width * large.height

old_small_area = small.width * small.height

new_large_ area = old_large_area + old_small_area

that’s the easy part, but you still have some sort of unspecified constraint left to deal with before you can convert that new area into the new width/height of the large rect…  (if they were squares then it’d just be a sqrt())

what “shape” must the larger rectangle maintain?  preserve large rect’s original aspect ratio?  some weighted-by-area percentage (lerp) toward the aspect ratio of the smaller rect?  or straight average of the two?  or???

You know, I’ll try to just make a new square, it might work just fine. And I see that figuring out the new shape of two rectangles can be tricky.

Thanks! :slight_smile:

@christian.fagerland

Do you actually want the Rectangle / Square to actually grow the entire area of the smaller one eaten object?

Maybe a simpler approach would be to just get the height and width of the smaller object - divide them in half, and add it to the height and width for the objects new size.

Maybe something like this.

local myBigRectangle = display.newRect( 0, 0, 150, 50 ) local mySmallRectangle = display.newRect( 0, 0, 15, 50 ) --- your collision function here -- local sH = myBigRectangle.height + ( mySmallRectangle.height /2 ) local sW = myBigRectangle.width + ( mySmallRectangle.width /2 ) -- Used transition.to as an effect only I think you can set the properties directly if you want transition.to( myBigRectangle , { time=1000, height=sH , width= sW } )

Unless your doing something that requires actual duplication of size ( like some kids teaching app ) this should give you the effect you are looking for ( at least that’s my theory ).

Good Luck,

Larry