Edge filling algorithm implemented by OpenGL

Time：2021-11-25

The idea of edge filling: enumerate each edge, and perform the filling operation on the pixels on the right of this edge, that is, remove it if there is filling, and fill it if there is no filling

#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
using namespace std;
int x[]= {10,70,70,60,40,30,20};
int y[]= {10,10,50,40,60,20,30};
int vis; // Judge whether the coordinate point exists
void init()
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 200.0, 0.0, 150.0);
}
void MidBresenhamCircle()
{
memset(vis,0,sizeof(vis));
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(2);
glBegin(GL_LINES); // Draw polygon shape
for(int i=0; i<7; ++i)
{
glVertex2i(x[i],y[i]);
glVertex2i(x[(i+1)%7],y[(i+1)%7]);
}
glEnd();
glFlush();
glPointSize(2);
glBegin(GL_POINTS);
For (int, I = x , j = y ; I < 300; + + I) // special treatment for edges with k = 0
{
if(!vis[i][j])
{
glColor3f(0.0f,1.0f,0.0f);
glVertex2i(i,j);
vis[i][j]=1;
}
else
{
glColor3f(0.0f,0.0f,0.0f);
glVertex2i(i,j);
vis[i][j]=1;
}
}
For (int j = y ; J < = y ; + + J) // special treatment for edges where K does not exist
{
for(int i=x; i<300; ++i)
{
if(!vis[i][j])
{
glColor3f(0.0f,1.0f,0.0f);
glVertex2i(i,j);
vis[i][j]=1;
}
else
{
glColor3f(0.0f,0.0f,0.0f);
glVertex2i(i,j);
vis[i][j]=1;
}
}
}
for(int p=2; p<7; ++p)
{
int k=(y[(p+1)%7]-y[p])/(x[(p+1)%7]-x[p]); // Find the slope
int x1=x[(p+1)%7],x0=x[p],y1=y[(p+1)%7],y0=y[p]; // The points of the line are derived from the slope
if(y1<y0)
{
swap(x1,x0);
swap(y1,y0);
}
for(int j=y0; j<=y1; ++j)
{
int i=(j-y0+k*x0)/k;
for(int px=i; px<300; ++px)
{
if(!vis[px][j])
{
glColor3f(0.0f,1.0f,0.0f);
glVertex2i(px,j);
vis[px][j]=1;
}
else
{
glColor3f(0.0f,0.0f,0.0f);
glVertex2i(px,j);
vis[px][j]=0;
}
}
}
}
glEnd();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
Glutcreatewindow ("the first OpenGL program");
init();
glutDisplayFunc(MidBresenhamCircle);
glutMainLoop();
return 0;
}

effect The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Introduction Guide to test development, specializing in treating discomfort

The term “contact test development” has been used for a long time. Here are some opinions of Xiaobian. Go to the directory first 1、 What is test development? 2、 What capabilities should test development have? 3、 How do I get started with test development? What is test development? Looking ahead, test development is not a […]