PDA

View Full Version : drag a rectangle using mouse event



syazwina
08-03-2006, 08:25 AM
hii :)
i have problem to drag/move a rectangle using mouse click..
heres my codes..my codes can draw a rectangle using mouse event,display the rectangle coordinates and when user click could determine whether the coordinates is inside or outside the rectangle border..
now im stuck at how to move/drag the rectangle when user click inside the border.
Thanks in advance.. :)

void myMotion(int x, int y)
{
corner[1].x = x;
corner[1].y = windowHeight - y;
corner[2].x = x;
corner[2].y = windowHeight - y;
corner[3].x = corner[0].x;
corner[3].y = corner[0].y;


glClear(GL_COLOR_BUFFER_BIT);
glRecti( corner[0].x, corner[0].y, corner[1].x, corner[1].y);
printf("%3d %3d , %3d %3d\n", corner[0].x, corner[0].y, corner[1].x, corner[1].y);

glFlush();

}

void myMouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
corner[0].x = x;
corner[0].y = windowHeight - y;
corner[1].x = corner[0].x;
corner[1].y = corner[0].y;

printf("%3d %3d\n", corner[0].x, corner[0].y);

if(corner[0].x >=corner[3].x && corner[0].x <= corner[2].x && corner[0].y <=corner[3].y && corner[0].y >=corner[2].y)
{
printf("Yup, the point is inside the boundary\n");
}
else
{
printf("Sorry..the point is outside the boundary\n");
}


}
}

08-05-2006, 05:27 PM
It's customary in Windows to consider the top-left rectangle edges as inclusive and the bottom-right edges exclusive, so there's no overlap in adjacent rects. So a mouse (x,y) is considered inside a rect if:

x >= left && x < right && y >= top && y < bottom,

where (left, top) and (right, bottom) are the (x, y) coordinates of the rectangle's min/max in window space.

Since window coordinates run in [0, Client Width) x [0, Client Height), you should subtract 1 when flipping the y,

y = clientHeight - 1 - y.

Otherwise for say a 800x600 client rect, a max mouse y of 599 will flip to 1 instead of 0.

syazwina
08-06-2006, 12:35 AM
Thanks jupitor..