Jump to content

Cursor position


Recommended Posts

Hi there,

I was playing with DX functions and I make something that I really don't like. I'll try to explain you with images. That would be more easier to understand.

There is my screen (not my real screen, just to explain ^^):

3Ui5A.png

As you can see, there is a cursor and a rectangle. To detect if the cursor is IN the rectangle I can make a basic thing like this:

if x >= rectangle.x and y >= rectangle.y and x <= (rectangle.x+rectangle.w) and y <= (rectangle.y+rectangle.h) then 

Now, let's try to do with a Trapeze.

3Uib6.png

I've tryed but I can't find it.

3Uiqb.png

if (x >= (trapeze.x+ triangle.width)) and (y >= (trapeze.y ... Can’t find the next 

And now, more harder (I think). For this one, I've no idea how do that.

3UitU.png

So... If someone have the answer for theses problems that would be very usefull for me (and other peoples maybe).

Thanks alot for your help.

Regards,

FatalTerror.

Link to comment

this is not a script problem. Is a MATH problem.

for the circurference ...

you need to check if the distance from the center of the circurference to the position of the mouse is less or equal than the radius of such circurference. in that case, the cursor is inside...

  
local x,y = getCursorPosition (); 
if  ( (circurference.x-x)^2 + (circurference.y-y)^2) <= circurference.radius^2 then  
        --- inside 
else 
       --- outside. 
end; 
  

for any irregular polygon, like your trapeze, subdivide it in triangles and check if the point is inside of any of those triangles...

if it´s insides, then, it´s inside of the polygon.

i wil not tell you how to check if a point is inside of a triangle, just i´ll show you the code. (need basic vector algebra knowledge)

trapeze.width and trapeze.height will be the dimension of the bounding rectangle.

trapeze.x, trapeze.y will be the position of that rectangle that surrounds your trapeze.

trapeze.wt will be the width of the triangle.

  
local x,y = getCursorPosition (); 
if isPointInTriangle (x, y,  trapeze.x, trapeze.y+trapeze.height,  trapeze.x+trapeze.wt, trapeze.y, 
        trapeze.x+trapeze.width-trapeze.wt, trapeze.y+trapeze.height) or  
   isPointInTriangle (x,y, trapeze.x+trapeze.width-trapeze.wt, trapeze.y+trapeze.height, 
         trapeze.x+trapeze.wt, trapeze.y, trapeze.x+trapeze.width, trapeze.y) then  
   --- inside  
else 
   --- outside.  
end;  
  
-- and this is the function to check whatever a point is inside of a triangle. 
function isPointInTriangle (x,y,  x0,y0, x1,y1,x2,y2)  
     -- implementation of the barycentric technique...  
     local v0x,v0y = x2 - x0, y2 - y0; 
     local v1x, v1y = x1 - x0, y1 - y0; 
     local v2x, v2y = x - x0, y - y0;  
      
     local dot00 = v0x^2 + v0y^2;  
     local dot01 = v0x*v1x + v0y*v1y; 
     local dot02 = v0x*v2x + v0y*v2y;  
     local dot11 = v1x^2 + v1y^2;  
     local dot12 = v1x*v2x + v1y*v2y; 
  
     local i = 1 / (dot00 * dot11 - dot01 * dot01); 
     local u,v = (dot11 * dot02 - dot01 * dot12) * i, (dot00 * dot12 - dot01 * dot02) * i; 
     return ((u+v)<1) and (u>=0) and (v>=0);  
  
     -- more info here: [url=http://www.blackpawn.com/texts/pointinpoly/]http://www.blackpawn.com/texts/pointinpoly/[/url] 
end;  
  

Link to comment
this is not a script problem. Is a MATH problem.

for the circurference ...

you need to check if the distance from the center of the circurference to the position of the mouse is less or equal than the radius of such circurference. in that case, the cursor is inside...

  
local x,y = getCursorPosition (); 
if  ( (circurference.x-x)^2 + (circurference.y-y)^2) <= circurference.radius^2 then  
        --- inside 
else 
       --- outside. 
end; 
  

for any irregular polygon, like your trapeze, subdivide it in triangles and check if the point is inside of any of those triangles...

if it´s insides, then, it´s inside of the polygon.

i wil not tell you how to check if a point is inside of a triangle, just i´ll show you the code. (need basic vector algebra knowledge)

trapeze.width and trapeze.height will be the dimension of the bounding rectangle.

trapeze.x, trapeze.y will be the position of that rectangle that surrounds your trapeze.

trapeze.wt will be the width of the triangle.

  
local x,y = getCursorPosition (); 
if isPointInTriangle (x, y,  trapeze.x, trapeze.y+trapeze.height,  trapeze.x+trapeze.wt, trapeze.y, 
        trapeze.x+trapeze.width-trapeze.wt, trapeze.y+trapeze.height) or  
   isPointInTriangle (x,y, trapeze.x+trapeze.width-trapeze.wt, trapeze.y+trapeze.height, 
         trapeze.x+trapeze.wt, trapeze.y, trapeze.x+trapeze.width, trapeze.y) then  
   --- inside  
else 
   --- outside.  
end;  
  
-- and this is the function to check whatever a point is inside of a triangle. 
function isPointInTriangle (x,y,  x0,y0, x1,y1,x2,y2)  
     -- implementation of the barycentric technique...  
     local v0x,v0y = x2 - x0, y2 - y0; 
     local v1x, v1y = x1 - x0, y1 - y0; 
     local v2x, v2y = x - x0, y - y0;  
      
     local dot00 = v0x^2 + v0y^2;  
     local dot01 = v0x*v1x + v0y*v1y; 
     local dot02 = v0x*v2x + v0y*v2y;  
     local dot11 = v1x^2 + v1y^2;  
     local dot12 = v1x*v2x + v1y*v2y; 
  
     local i = 1 / (dot00 * dot11 - dot01 * dot01); 
     local u,v = (dot11 * dot02 - dot01 * dot12) * i, (dot00 * dot12 - dot01 * dot02) * i; 
     return ((u+v)<1) and (u>=0) and (v>=0);  
  
     -- more info here: [url=http://www.blackpawn.com/texts/pointinpoly/]http://www.blackpawn.com/texts/pointinpoly/[/url] 
end;  
  

Thanks alot for this quickly answer.

If I understand right, we subdivide the polygon in multiples triangles and check if the point is one of theses triangles like:

3UmC2.png

If the point isn't in the first triangle, check the second, the third etc. Am I right?

I didn't saw vector or barycentric things at school yet :?

Regards,

FatalTerror.

Link to comment

for that polygon, you don´t have to subdivide the rectangle in the middle in 2 triangles, because is easy to check if the mouse is inside of a rectangle.

Like you say in the post:

  
if x >= rectangle.x and y >= rectangle.y and x <= (rectangle.x+rectangle.w) and y <= (rectangle.y+rectangle.h) then 
  

so, the mouse is inside in that case, if it´s in one triangle or in the rectangle.

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