rectangle covering other, calculate size of shared space?

Hi, let’s say I have 2 rectangles on the screen, for example one has center at position xy 100, 100, and second one 100,110. Now I want move 3rd rectangle on top of them and calculate with which object 3rd rectangle is sharing most of the xy. which rectangle is covering the most.

Any xy functions I can look into? some hint? 

local function hasCollided(obj1, obj2) if obj1 == nil then return false end if obj2 == nil then return false end local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end -- circle based local function hasCollidedCircle(obj1, obj2) if obj1 == nil then return false end if obj2 == nil then return false end local sqrt = math.sqrt local dx = obj1.x - obj2.x; local dy = obj1.y - obj2.y; local distance = sqrt(dx\*dx + dy\*dy); local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) if distance \< objectSize then return true end return false end

something like this will help me :slight_smile:

Question - Is this question answered?

I ask, because you marked it ‘answered’, but then a the end of you last post you say

something like this will help me  :slight_smile:

This implies to me you want an answer in that form, but that that isn’t the answer. 

?? Confused…   :unsure: ??

You need to calculate the area of the intersection of rectangle3 with the other 2 and then test for the bigger one.

Here’s a sample implementation

[lua]

local function intersectRect( x, y, w, h, otherX, otherY, otherW, otherH )

    if x < otherX then

        w = w - (otherX - x)

        x = otherX

    end

    if (x + w) > (otherX + otherW) then

        w = (otherX + otherW) - x

    end

    if y < otherY then

        h = h - (otherY - y)

        y = otherY

    end

    if (y + h) > (otherY + otherH) then

        h = (otherY + otherH) - y

    end

    return x, y, w, h

end

local function intersectArea( r1, r2 )

    local bounds1 = r1.contentBounds

    local bounds2 = r2.contentBounds

    local x, y, w, h = intersectRect(

        bounds1.xMin, bounds1.yMin, r1.contentWidth, r1.contentHeight,

        bounds2.xMin, bounds2.yMin, r2.contentWidth, r2.contentHeight )

    if w <= 0 or h <= 0 then

        return 0

    else

        return w*h

    end

end

local function biggerOverlap( r1, r2, r3 )

    local area1 = intersectArea( r1, r3 )

    local area2 = intersectArea( r2, r3 )

    if area1 > area2 then

        return “first”

    elseif area2 > area1 then

        return “second”

    elseif area1 == 0 then

        return “none”

    else

        return “same”

    end

end

[/lua]

Check out my math lib which includes an ability (with demo) to calculate overlapping areas:

https://gist.github.com/HoraceBury/9431861

Oh, sorry for not being clear enough. Yes, answered, calculating distance from center works fine enough for my need here. :slight_smile:

local function hasCollided(obj1, obj2) if obj1 == nil then return false end if obj2 == nil then return false end local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end -- circle based local function hasCollidedCircle(obj1, obj2) if obj1 == nil then return false end if obj2 == nil then return false end local sqrt = math.sqrt local dx = obj1.x - obj2.x; local dy = obj1.y - obj2.y; local distance = sqrt(dx\*dx + dy\*dy); local objectSize = (obj2.contentWidth/2) + (obj1.contentWidth/2) if distance \< objectSize then return true end return false end

something like this will help me :slight_smile:

Question - Is this question answered?

I ask, because you marked it ‘answered’, but then a the end of you last post you say

something like this will help me  :slight_smile:

This implies to me you want an answer in that form, but that that isn’t the answer. 

?? Confused…   :unsure: ??

You need to calculate the area of the intersection of rectangle3 with the other 2 and then test for the bigger one.

Here’s a sample implementation

[lua]

local function intersectRect( x, y, w, h, otherX, otherY, otherW, otherH )

    if x < otherX then

        w = w - (otherX - x)

        x = otherX

    end

    if (x + w) > (otherX + otherW) then

        w = (otherX + otherW) - x

    end

    if y < otherY then

        h = h - (otherY - y)

        y = otherY

    end

    if (y + h) > (otherY + otherH) then

        h = (otherY + otherH) - y

    end

    return x, y, w, h

end

local function intersectArea( r1, r2 )

    local bounds1 = r1.contentBounds

    local bounds2 = r2.contentBounds

    local x, y, w, h = intersectRect(

        bounds1.xMin, bounds1.yMin, r1.contentWidth, r1.contentHeight,

        bounds2.xMin, bounds2.yMin, r2.contentWidth, r2.contentHeight )

    if w <= 0 or h <= 0 then

        return 0

    else

        return w*h

    end

end

local function biggerOverlap( r1, r2, r3 )

    local area1 = intersectArea( r1, r3 )

    local area2 = intersectArea( r2, r3 )

    if area1 > area2 then

        return “first”

    elseif area2 > area1 then

        return “second”

    elseif area1 == 0 then

        return “none”

    else

        return “same”

    end

end

[/lua]

Check out my math lib which includes an ability (with demo) to calculate overlapping areas:

https://gist.github.com/HoraceBury/9431861

Oh, sorry for not being clear enough. Yes, answered, calculating distance from center works fine enough for my need here. :slight_smile: