Hi, I'm triying to draw in the window a poligon using the mouse but when I do that, there's an extra line that I don't need and I don't know what's wrong, please help me


Here's my code:


#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

int buffer[3*400*300];
int xa,ya,xb,yb;
int xc, yc, radio, lados=7;

void inicializa();
void Mouse(int button, int state, int x, int y);
void Motion(int x, int y);
void putPixel(int x, int y, int xb, int yb);
void lineaBres(int xa, int ya, int xb, int yb);
void poligono(int xc, int yc, int x, int y, int radio, int lados);

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(50, 100);
glutInitWindowSize(400, 300);
glutCreateWindow("Trazo de polígonos usando el Mouse");
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
glutDisplayFunc(inicializa);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);
glutMainLoop();

return (0);
}

void inicializa()
{
glColor3f(0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
}

void Mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_DOWN)
{
glReadPixels(0, 0, 400, 300, GL_RGB, GL_UNSIGNED_BYTE,buffer);
xc= x;
yc= 300-y;
}
else if(button == GLUT_LEFT_BUTTON &amp;&amp; state == GLUT_UP)
{
glDrawPixels(0, 0, GL_RGB, GL_UNSIGNED_BYTE, buffer);
}
}

void Motion(int x, int y)
{
glDrawPixels(400, 300, GL_RGB, GL_UNSIGNED_BYTE, buffer);
radio= sqrt((x-xc)*(x-xc)+(y-yc)*(y-yc));
poligono(xc, yc, x, y, radio, lados);
}

void poligono(int xc, int yc, int x, int y, int radio, int lados)
{
int i;
float angulo= atan2(y-yc, x-xc);

for(i= 0; i <= lados; i++)
{
xa= x;
ya= y;
angulo= angulo+M_PI*2/lados;
x= ceil(xc + radio * cos(angulo));
y= ceil(yc + radio * sin(angulo));
x= x/2;
y= y/2;
lineaBres(xa, ya, x, y);
}
}

void putPixel(int xa,int ya, int xb, int yb)
{
glEnable(GL_LINE_SMOOTH);
glLineWidth(1.0);
glBegin(GL_LINE_STRIP);
glVertex2f(xa,ya);
glVertex2f(xb,yb);
glEnd();
}

void lineaBres(int xa, int ya, int xb, int yb)
{
int x, y, dx, dy, dezplazamientoX, dezplazamientoY, p;

dx= abs(xb-xa);
dy= abs(yb-ya);
x= xa;
y= ya;

if(xa>xb)
{
dezplazamientoX= -1;
}
else
{
dezplazamientoX= 1;
}

if(ya>yb)
{
dezplazamientoY= -1;
}
else
{
dezplazamientoY= 1;
}
putPixel(xa,ya,xb,yb);

if(dx>=dy)
{
p= dx/2;
while(x!=xb)
{
p= p-dy;
if(p<0)
{
y= y+dezplazamientoY;
p= p+dx;
}
x= x+dezplazamientoX;
putPixel(xa,ya,xb,yb);
}
}
else if(dy>dx)
{
p= dy/2;
while (y!=yb)
{
p= p-dx;
if(p<0)
{
x= x+dezplazamientoX;
p= p+dy;
}
y= y+dezplazamientoY;
putPixel(xa,ya,xb,yb);
}
}
glFlush();
}