Skip to main content
corrected
Source Link
Tofu
  • 116
  • 2

Assuming your GUI elements are simple shapes like rectangles, detecting if your mouse is on the element becomes fairly trivial. Here's the pseudo code:

function IsCursorInRectangle[mouse,rectangle]
Is mouse.x > rectangle.x
     is mouse.x < rectangle.x + rectangle.width
          is mouse.y > rectangle.y
               is mouse.y >< rectangle.y + rectangle.height
                    return true;
 return false;

This is a simple example of how to check it in C.

#include <stdio.h>
struct Coordinate
{
     int m_nPositionX;
     int m_nPositionY;
};
struct GUIElement
{
     struct Coordinate m_csPosition;
     int m_nRecWidth;
     int m_nRecHeight;
};

int IsCursorOnElement(struct GUIElement* pgElement, struct Coordinate* pcCursor)
{
     if(pcCursor->m_nPositionX > pgElement->m_csPosition.m_nPositionX &&
        pcCursor->m_nPositionX < pgElement->m_csPosition.m_nPositionX + pgElement->m_nRecWidth &&
        pcCursor->m_nPositionY > pgElement->m_csPosition.m_nPositionY &&
        pcCursor->m_nPositionY < pgElement->m_csPosition.m_nPositionY + pgElement->m_nRecHeight)
     {
          return 1;
     }
     
     return 0;
}
int main(void)
{
     struct GUIElement gsButton = {{100,100},50,50};
     struct Coordinate csCursor = {105,120};
     
     if(IsCursorOnElement(&gsButton, &csCursor))
     {
          puts("We're inside the element");
     }
     else
     {
          puts("We're outside the element");
     }
     return 0;
}

If you want to do other shapes like circles, you would need the center x,y of the circle + it's radius.

Here's a resource you can check out . https://www.jeffreythompson.org/collision-detection/point-rect.php

Assuming your GUI elements are simple shapes like rectangles, detecting if your mouse is on the element becomes fairly trivial. Here's the pseudo code:

function IsCursorInRectangle[mouse,rectangle]
Is mouse.x > rectangle.x
     is mouse.x < rectangle.x + rectangle.width
          is mouse.y > rectangle.y
               is mouse.y > rectangle.y + rectangle.height
                    return true;
 return false;

This is a simple example of how to check it in C.

#include <stdio.h>
struct Coordinate
{
     int m_nPositionX;
     int m_nPositionY;
};
struct GUIElement
{
     struct Coordinate m_csPosition;
     int m_nRecWidth;
     int m_nRecHeight;
};

int IsCursorOnElement(struct GUIElement* pgElement, struct Coordinate* pcCursor)
{
     if(pcCursor->m_nPositionX > pgElement->m_csPosition.m_nPositionX &&
        pcCursor->m_nPositionX < pgElement->m_csPosition.m_nPositionX + pgElement->m_nRecWidth &&
        pcCursor->m_nPositionY > pgElement->m_csPosition.m_nPositionY &&
        pcCursor->m_nPositionY < pgElement->m_csPosition.m_nPositionY + pgElement->m_nRecHeight)
     {
          return 1;
     }
     
     return 0;
}
int main(void)
{
     struct GUIElement gsButton = {{100,100},50,50};
     struct Coordinate csCursor = {105,120};
     
     if(IsCursorOnElement(&gsButton, &csCursor))
     {
          puts("We're inside the element");
     }
     else
     {
          puts("We're outside the element");
     }
     return 0;
}

If you want to do other shapes like circles, you would need the center x,y of the circle + it's radius.

Here's a resource you can check out . https://www.jeffreythompson.org/collision-detection/point-rect.php

Assuming your GUI elements are simple shapes like rectangles, detecting if your mouse is on the element becomes fairly trivial. Here's the pseudo code:

function IsCursorInRectangle[mouse,rectangle]
Is mouse.x > rectangle.x
     is mouse.x < rectangle.x + rectangle.width
          is mouse.y > rectangle.y
               is mouse.y < rectangle.y + rectangle.height
                    return true;
 return false;

This is a simple example of how to check it in C.

#include <stdio.h>
struct Coordinate
{
     int m_nPositionX;
     int m_nPositionY;
};
struct GUIElement
{
     struct Coordinate m_csPosition;
     int m_nRecWidth;
     int m_nRecHeight;
};

int IsCursorOnElement(struct GUIElement* pgElement, struct Coordinate* pcCursor)
{
     if(pcCursor->m_nPositionX > pgElement->m_csPosition.m_nPositionX &&
        pcCursor->m_nPositionX < pgElement->m_csPosition.m_nPositionX + pgElement->m_nRecWidth &&
        pcCursor->m_nPositionY > pgElement->m_csPosition.m_nPositionY &&
        pcCursor->m_nPositionY < pgElement->m_csPosition.m_nPositionY + pgElement->m_nRecHeight)
     {
          return 1;
     }
     
     return 0;
}
int main(void)
{
     struct GUIElement gsButton = {{100,100},50,50};
     struct Coordinate csCursor = {105,120};
     
     if(IsCursorOnElement(&gsButton, &csCursor))
     {
          puts("We're inside the element");
     }
     else
     {
          puts("We're outside the element");
     }
     return 0;
}

If you want to do other shapes like circles, you would need the center x,y of the circle + it's radius.

Here's a resource you can check out . https://www.jeffreythompson.org/collision-detection/point-rect.php

pseudo code and clarification
Source Link
Tofu
  • 116
  • 2

Assuming your GUI elements are simple shapes like rectangles, detecting if your mouse is on the element becomes fairly trivial. You would check if the cursor x position is greater than the element x position and less than the element's x position + the element's width. You would do the same for cursor Yy position is greater than the element y position and less than the element y position + Here's the element's height.pseudo code:

function IsCursorInRectangle[mouse,rectangle]
Is mouse.x > rectangle.x
     is mouse.x < rectangle.x + rectangle.width
          is mouse.y > rectangle.y
               is mouse.y > rectangle.y + rectangle.height
                    return true;
 return false;

Since you didn't specify which language, I just wrote outThis is a simple example for youof how to check it in C. But this will only really work with rectangles.

#include <stdio.h>
struct Coordinate
{
     int m_nPositionX;
     int m_nPositionY;
};
struct GUIElement
{
     struct Coordinate m_csPosition;
     int m_nRecWidth;
     int m_nRecHeight;
};

int IsCursorOnElement(struct GUIElement* pgElement, struct Coordinate* pcCursor)
{
     if(pcCursor->m_nPositionX > pgElement->m_csPosition.m_nPositionX &&
        pcCursor->m_nPositionX < pgElement->m_csPosition.m_nPositionX + pgElement->m_nRecWidth &&
        pcCursor->m_nPositionY > pgElement->m_csPosition.m_nPositionY &&
        pcCursor->m_nPositionY < pgElement->m_csPosition.m_nPositionY + pgElement->m_nRecHeight)
     {
          return 1;
     }
     
     return 0;
}
int main(void)
{
     struct GUIElement gsButton = {{100,100},50,50};
     struct Coordinate csCursor = {105,120};
     
     if(IsCursorOnElement(&gsButton, &csCursor))
     {
          puts("We're inside the element");
     }
     else
     {
          puts("We're outside the element");
     }
     return 0;
}

If you want to do other shapes like circles, you would need the center x,y of the circle + it's radius.

Here's a resource you can check out . https://www.jeffreythompson.org/collision-detection/point-rect.php

Assuming your GUI elements are simple shapes like rectangles, detecting if your mouse is on the element becomes fairly trivial. You would check if the cursor x position is greater than the element x position and less than the element's x position + the element's width. You would do the same for cursor Yy position is greater than the element y position and less than the element y position + the element's height.

Since you didn't specify which language, I just wrote out a simple example for you in C. But this will only really work with rectangles.

struct Coordinate
{
     int m_nPositionX;
     int m_nPositionY;
};
struct GUIElement
{
     struct Coordinate m_csPosition;
     int m_nRecWidth;
     int m_nRecHeight;
};

int IsCursorOnElement(struct GUIElement* pgElement, struct Coordinate* pcCursor)
{
     if(pcCursor->m_nPositionX > pgElement->m_csPosition.m_nPositionX &&
        pcCursor->m_nPositionX < pgElement->m_csPosition.m_nPositionX + pgElement->m_nRecWidth &&
        pcCursor->m_nPositionY > pgElement->m_csPosition.m_nPositionY &&
        pcCursor->m_nPositionY < pgElement->m_csPosition.m_nPositionY + pgElement->m_nRecHeight)
     {
          return 1;
     }
     
     return 0;
}

If you want to do other shapes like circles, you would need the center x,y of the circle + it's radius.

Assuming your GUI elements are simple shapes like rectangles, detecting if your mouse is on the element becomes fairly trivial. Here's the pseudo code:

function IsCursorInRectangle[mouse,rectangle]
Is mouse.x > rectangle.x
     is mouse.x < rectangle.x + rectangle.width
          is mouse.y > rectangle.y
               is mouse.y > rectangle.y + rectangle.height
                    return true;
 return false;

This is a simple example of how to check it in C.

#include <stdio.h>
struct Coordinate
{
     int m_nPositionX;
     int m_nPositionY;
};
struct GUIElement
{
     struct Coordinate m_csPosition;
     int m_nRecWidth;
     int m_nRecHeight;
};

int IsCursorOnElement(struct GUIElement* pgElement, struct Coordinate* pcCursor)
{
     if(pcCursor->m_nPositionX > pgElement->m_csPosition.m_nPositionX &&
        pcCursor->m_nPositionX < pgElement->m_csPosition.m_nPositionX + pgElement->m_nRecWidth &&
        pcCursor->m_nPositionY > pgElement->m_csPosition.m_nPositionY &&
        pcCursor->m_nPositionY < pgElement->m_csPosition.m_nPositionY + pgElement->m_nRecHeight)
     {
          return 1;
     }
     
     return 0;
}
int main(void)
{
     struct GUIElement gsButton = {{100,100},50,50};
     struct Coordinate csCursor = {105,120};
     
     if(IsCursorOnElement(&gsButton, &csCursor))
     {
          puts("We're inside the element");
     }
     else
     {
          puts("We're outside the element");
     }
     return 0;
}

If you want to do other shapes like circles, you would need the center x,y of the circle + it's radius.

Here's a resource you can check out . https://www.jeffreythompson.org/collision-detection/point-rect.php

Source Link
Tofu
  • 116
  • 2

Assuming your GUI elements are simple shapes like rectangles, detecting if your mouse is on the element becomes fairly trivial. You would check if the cursor x position is greater than the element x position and less than the element's x position + the element's width. You would do the same for cursor Yy position is greater than the element y position and less than the element y position + the element's height.

Since you didn't specify which language, I just wrote out a simple example for you in C. But this will only really work with rectangles.

struct Coordinate
{
     int m_nPositionX;
     int m_nPositionY;
};
struct GUIElement
{
     struct Coordinate m_csPosition;
     int m_nRecWidth;
     int m_nRecHeight;
};

int IsCursorOnElement(struct GUIElement* pgElement, struct Coordinate* pcCursor)
{
     if(pcCursor->m_nPositionX > pgElement->m_csPosition.m_nPositionX &&
        pcCursor->m_nPositionX < pgElement->m_csPosition.m_nPositionX + pgElement->m_nRecWidth &&
        pcCursor->m_nPositionY > pgElement->m_csPosition.m_nPositionY &&
        pcCursor->m_nPositionY < pgElement->m_csPosition.m_nPositionY + pgElement->m_nRecHeight)
     {
          return 1;
     }
     
     return 0;
}

If you want to do other shapes like circles, you would need the center x,y of the circle + it's radius.