1. Attached is the code to draw dots with mouse. Reproduce the code to make is f
ID: 3580223 • Letter: 1
Question
1. Attached is the code to draw dots with mouse. Reproduce the code to make is functional
2. Modify the code so that is can draw rectangles entered with mouse (Function definition attached)
3. Modify the code to create a polyline using the mouse (Function definition attached)
4. Modify the code so you can draw freely with your mouse (you need myMoveMouse function)
Functions:
1. Draw dots with mouse:
void myMouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
drawDot(x, screenHeight -y);
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
exit(-1);
}
2. Draw Rectangles with mouse:
void myMouse(int button, int state, int x, int y)
{
static GLintPoint corner[2];
static int numCorners = 0; // initial value is 0
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y; // flip y coordinate
numCorners++; // have another point
if(numCorners == 2)
{
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0; // back to 0 corners
}
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
glClear(GL_COLOR_BUFFER_BIT); // clear the window
glFlush();
}
3. Draw polylines with mouse:
void myMouse(int button, int state, int x, int y)
{
#define NUM 20
static GLintPoint List[NUM];
static int last = -1; // last index used so far
// test for mouse button as well as for a full array
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && last < NUM -1)
{
List[++last].x = x; // add new point to list
List[ last].y = screenHeight - y; // window height is 480
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glBegin(GL_LINE_STRIP); // redraw the polyline
for(int i = 0; i <= last; i++)
glVertex2i(List[i].x, List[i].y);
glEnd();
glFlush();
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
last = -1; // reset the list to empty
}
4. Free hand drawing with mouse:
void myMovedMouse(int mouseX, int mouseY)
{
GLint x = mouseX; //grab the mouse position
GLint y = screenHeight – mouseY; // flip it as usual
GLint brushSize = 20;
glRecti(x,y, x + brushSize, y + brushSize);
glFlush();
}
Explanation / Answer
void myMouse(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
drawDot(x, screenHeight -y);
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
exit(-1);
}
void myMouse(int button, int state, int x, int y)
{
static GLintPoint corner[2];
static int numCorners = 0; // initial value is 0
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y; // flip y coordinate
numCorners++; // have another point
if(numCorners == 2)
{
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0; // back to 0 corners
}
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
glClear(GL_COLOR_BUFFER_BIT); // clear the window
glFlush();
}
}
void myMovedMouse(int mouseX, int mouseY)
{
GLint x = mouseX; //grab the mouse position
GLint y = screenHeight – mouseY; // flip it as usual
GLint brushSize = 20;
glRecti(x,y, x + brushSize, y + brushSize);
glFlush();
}
void myMouse(int button, int state, int x, int y)
{
#define NUM 20
static GLintPoint List[NUM];
static int last = -1; // last index used so far
// test for mouse button as well as for a full array
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN && last < NUM -1)
{
List[++last].x = x; // add new point to list
List[ last].y = screenHeight - y; // window height is 480
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
glBegin(GL_LINE_STRIP); // redraw the polyline
for(int i = 0; i <= last; i++)
glVertex2i(List[i].x, List[i].y);
glEnd();
glFlush();
}
else if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
last = -1; // reset the list to empty
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.