Enargy, Posted April 15, 2016 Share Posted April 15, 2016 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
Bonsai Posted April 15, 2016 Share Posted April 15, 2016 Try https://wiki.multitheftauto.com/wiki/Ge ... enPoints2D. Link to comment
Enargy, Posted April 15, 2016 Author Share Posted April 15, 2016 Try https://wiki.multitheftauto.com/wiki/Ge ... enPoints2D. But that would be if a rectangle afaik. what I'm trying to make is that the cursor is not detected to get out of it for example: green = inside, red = outside. http://i.imgur.com/gDzE3S9.jpg Link to comment
ma2med Posted April 15, 2016 Share Posted April 15, 2016 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
Enargy, Posted April 16, 2016 Author Share Posted April 16, 2016 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
</Mr.Tn6eL> Posted April 16, 2016 Share Posted April 16, 2016 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
ma2med Posted April 16, 2016 Share Posted April 16, 2016 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
Enargy, Posted April 17, 2016 Author Share Posted April 17, 2016 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 . 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
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now