Home:ALL Converter>Draw a polygon from mouse clicks

Draw a polygon from mouse clicks

Ask Time:2014-02-15T03:09:28         Author:spearman008

Json Formatter

I'm trying to draw a a polygon on the screen with vertices determined by mouse clicks. A left click adds a vertex to the polygon, and a right click will add the last vertex to the polygon connecting it to the first and creating the shape.

I currently have two vectors, one for x-coordinates and one for y-coordinates, and I am looping through the vectors creating a line loop. A -1 in the vector determines the end of a polygon and the start of a new one. This is a function that is then called in the display function.

Eventually I have to scan convert these polygons, and then clip them in a user defined window using the Sutherland Hodgman algorithm, but I'm having trouble even getting the polygons to show up.

glBegin(GL_LINE_LOOP);
for (int i = 0; i < xCo.size(); i++)
{
    if (xCo[i + 1] != -1)
    {
        glVertex2f(xCo[i], yCo[i]);
        glVertex2f(xCo[i + 1], yCo[i + 1]);
    }
    else
    {
        glVertex2f(xCo[i + 1], yCo[i + 1]);
        glVertex2f(xCo[0], yCo[0]);
    }
}
glEnd();
glFlush();
xCo.clear();
yCo.clear();

Author:spearman008,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/21787600/draw-a-polygon-from-mouse-clicks
yy