Jump to content

[HELP] if the cursor is inside a circle


Enargy,

Recommended Posts

Hey, I wanna do some detecting whether the cursor is within a circle. The circle is a dx image displayed on the screen and has size 50x50 then the circle occupies the same diameter as the picture, so my question is how could calculate the position of the cursor staying inside.

I tried with this but is not correctly in position.

function isCursorHoverRadius(x, y, r) 
    if not isCursorShowing() then return end 
    local cx, cy = getCursorPosition() 
    local fx, fy = cx*sx, cy*sy 
    if ( (x - fx) * (x - fx) + (y - fy) * (y - fy) <= r * r ) then 
        return true 
    end 
    return false 
end 

Link to comment
  
function isCursorHoverRadius(x, y, r) 
     if not isCursorShowing() then return end 
     local cx, cy = getCursorPosition() 
     local distance = getDistanceBetweenPoints2D(cx, cy, x, y) 
     if distance <= r then 
          return true 
     end 
     return false 
end 
  

Link to comment

use math.sqrt

Well I found an example here but it doesn't work very well.

Got this

addEventHandler("onClientRender", root,
function()
    local r, g, b = 255, 0, 0
   
    if isCursorShowing() and circularHover(500, 500, 50) then
        r, g, b = 0, 255, 0
        else
        r, g, b = 255, 0, 0
    end
   
    dxDrawImage(500, 500, 50, 50, "images/circle.png", 0, 0, 0, tocolor(r, g, b, 255)) -- the circle has the width and height of the image.
end)
 
function circularHover (x, y, radius)
    local screenX, screenY = guiGetScreenSize() --
    local mx, my = getCursorPosition()
    local mx, my = (mx or 0) * screenX, (my or 0) * screenY
    return math.sqrt((mx-x)^2 + (my-y)^2) <= radius^2
end

Link to comment
function isMouseInPosition(x, y, w, h) 
    if isCursorShowing( ) then 
        local cx, cy = getCursorPosition( ) 
        return cx >= x and cx <= x+w and cy >= y and cy <= y+h 
    end 
    return false 
end 
function isPositionInCircle(x, y, r, cx, cy) 
    local i = 0 
    for k=(-r), r do 
        local q = math.sqrt((r/2)*(r/2)-k*k) 
        if isMouseInPosition(x-q+(r/2), y+k+(r/2), 2*q, 1) then 
            i = i+1 
        end 
    end 
    return i ~= (-r)+r 
end 

Link to comment

You draw circle with rectangle? If yes, try it:

  
function isCursorHoverRadius(x, y, w, h, r) -- x, y, width, height, radius 
     if not isCursorShowing() then return end 
     local cx, cy = getCursorPosition() 
     local distance = getDistanceBetweenPoints2D(cx, cy, x+(w/2), y+(h/2)) 
     if distance <= r then 
          return true 
     end 
     return false 
end 
  

Link to comment
function isMouseInPosition(x, y, w, h)
    if isCursorShowing( ) then
        local cx, cy = getCursorPosition( )
        return cx >= x and cx <= x+w and cy >= y and cy <= y+h
    end
    return false
end
function isPositionInCircle(x, y, r, cx, cy)
    local i = 0
    for k=(-r), r do
        local q = math.sqrt((r/2)*(r/2)-k*k)
        if isMouseInPosition(x-q+(r/2), y+k+(r/2), 2*q, 1) then
            i = i+1
        end
    end
    return i ~= (-r)+r
end

Thank you but you were missing something :D . You didn't calculate the cursor position with the screen size.

local cx, cy = getCursorPosition( )
local sx, sy = guiGetScreenSize()
cx, cy = cx*sx, cy*sy

Now it works very well, thank you so much.

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...