Jump to content

Rectangle clip


Recommended Posts

Hello.

I have a problem with custom f11 map radar areas.

The rectangle perfectly working, but going out of the map dx frame.

How to clip the rectangles, or what?

Currently (bad):

bad_www.kepfeltoltes.hu_.png

And the perfect illustration (how to?):

good_www.kepfeltoltes.hu_.png

Code:

  
for _, area in ipairs(getElementsByType('radararea')) do 
    local areaX, areaY = getElementPosition(area); 
    local areaWidth, areaHeight = getRadarAreaSize(area); 
    local areaR, areaG, areaB, areaA = getRadarAreaColor(area); 
         
    if (isRadarAreaFlashing(area)) then 
        local flashTick = getTickCount() % 1500; 
        areaR, areaG, areaB = flashTick <= 750 and 255 or areaR, flashTick <= 750 and 0 or areaG, flashTick <= 750 and 0 or areaB; 
    end 
     
    local areaX, areaY = getMapFromWorldPosition(areaX, areaY + areaHeight); 
    local areaWidth, areaHeight = areaWidth / Bigmap.CurrentZoom * Minimap.MapUnit, areaHeight / Bigmap.CurrentZoom * Minimap.MapUnit; 
  
    -- how to clip the rectangles? 
     
    dxDrawRectangle(areaX, areaY, areaWidth, areaHeight, tocolor(areaR, areaG, areaB, areaA), false); 
end 
  
function getMapFromWorldPosition(worldX, worldY) 
    local playerX, playerY, playerZ = getElementPosition(localPlayer); 
     
    local centerX, centerY = (Bigmap.PosX + (Bigmap.Width / 2)), (Bigmap.PosY + (Bigmap.Height / 2)); 
     
    local areaLeftFrame = centerX - ((playerX - worldX) / Bigmap.CurrentZoom * Minimap.MapUnit); 
    local areaRightFrame = centerX + ((worldX - playerX) / Bigmap.CurrentZoom * Minimap.MapUnit); 
    local areaTopFrame = centerY - ((worldY - playerY) / Bigmap.CurrentZoom * Minimap.MapUnit); 
    local areaBottomFrame = centerY + ((playerY - worldY) / Bigmap.CurrentZoom * Minimap.MapUnit); 
     
    centerX = math.max(areaLeftFrame, math.min(areaRightFrame, centerX)); 
    centerY = math.max(areaTopFrame, math.min(areaBottomFrame, centerY)); 
     
    return centerX, centerY; 
end 
  

Link to comment
  
for _, area in ipairs(getElementsByType('radararea')) do 
    local x, y = getElementPosition(area); 
    local areaWidth, areaHeight = getRadarAreaSize(area); 
    local areaR, areaG, areaB, areaA = getRadarAreaColor(area); 
        
    if (isRadarAreaFlashing(area)) then 
        local flashTick = getTickCount() % 1500; 
        areaR, areaG, areaB = flashTick <= 750 and 255 or areaR, flashTick <= 750 and 0 or areaG, flashTick <= 750 and 0 or areaB; 
    end 
    
    local areaX, areaY = getMapFromWorldPosition(x, y + areaHeight); 
    local areaWidth, areaHeight = areaWidth / Bigmap.CurrentZoom * Minimap.MapUnit, areaHeight / Bigmap.CurrentZoom * Minimap.MapUnit; 
  
    -- how to clip the rectangles? 
    
    dxDrawRectangle(areaX, areaY, areaWidth, areaHeight, tocolor(areaR, areaG, areaB, areaA), false); 
end 
  
function getMapFromWorldPosition(worldX, worldY) 
    local playerX, playerY, playerZ = getElementPosition(localPlayer); 
    
    local centerX, centerY = (Bigmap.PosX + (Bigmap.Width / 2)), (Bigmap.PosY + (Bigmap.Height / 2)); 
    
    local areaLeftFrame = centerX - ((playerX - worldX) / Bigmap.CurrentZoom * Minimap.MapUnit); 
    local areaRightFrame = centerX + ((worldX - playerX) / Bigmap.CurrentZoom * Minimap.MapUnit); 
    local areaTopFrame = centerY - ((worldY - playerY) / Bigmap.CurrentZoom * Minimap.MapUnit); 
    local areaBottomFrame = centerY + ((playerY - worldY) / Bigmap.CurrentZoom * Minimap.MapUnit); 
    
    centerX = math.max(areaLeftFrame, math.min(areaRightFrame, centerX)); 
    centerY = math.max(areaTopFrame, math.min(areaBottomFrame, centerY)); 
    
    return centerX, centerY; 
end 

Link to comment
  • Moderators

Before you are going to clip it, it might be handy to check if the rectangle is actually inside the map:

local areaXRightSide = areaX+areaWidth 
local areaYBottom = areaY+areaHeight 
  
local BigmapRightSide = Bigmap.PosX+Bigmap.Width 
local BigmapBottom = Bigmap.PosY+Bigmap.Height 
  
if ((areaXRightSide > Bigmap.PosX) and (areaX < BigmapRightSide)) and ((areaYBottom > Bigmap.PosY) and (areaY < BigmapBottom)) then 
  

Untested.

  
local areaX2, areaY2 = math.min(areaX, Bigmap.PosX), math.min(areaY, Bigmap.PosY) 
local areaWidth2, areaHeight2 = areaWidth, areaHeight 
if areaX2 ~= areaX then  
    areaWidth2 = areaWidth-(areaX-areaX2) 
end 
  
if areaY2 ~= areaY then  
    areaHeight2 = areaHeight-(areaY-areaY2) 
end 
  
-- You can script the bottom clip!!!! 
  
dxDrawRectangle(areaX2, areaY2, areaWidth2, areaHeight2, tocolor(areaR, areaG, areaB, areaA), false); 

Untested

And you can script the bottom clip!

end 

Link to comment

Thanks, but I have already been solved.

  
if (doesCollide(Bigmap.PosX, Bigmap.PosY, Bigmap.Width, Bigmap.Height, areaX, areaY, areaWidth, areaHeight)) then 
    if (areaX < Bigmap.PosX) then 
        areaWidth = areaWidth - math.abs((Bigmap.PosX) - (areaX)); 
        areaX = areaX + math.abs((Bigmap.PosX) - (areaX)); 
    elseif (areaX + areaWidth > Bigmap.PosX + Bigmap.Width) then 
        areaWidth = areaWidth - math.abs((Bigmap.PosX + Bigmap.Width) - (areaX + areaWidth)); 
    end 
     
    if (areaY < Bigmap.PosY) then 
        areaHeight = areaHeight - math.abs((Bigmap.PosY) - (areaY)); 
        areaY = areaY + math.abs((Bigmap.PosY) - (areaY)); 
    elseif (areaY + areaHeight > Bigmap.PosY + Bigmap.Height) then 
        areaHeight = areaHeight - math.abs((Bigmap.PosY + Bigmap.Height) - (areaY + areaHeight)); 
    end 
     
    dxDrawRectangle(areaX, areaY, areaWidth, areaHeight, tocolor(areaR, areaG, areaB, areaA), false); 
end 
  

E: math.abs fixed

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...