要求:利用边缘填充算法和边界填充算法的原理,对《计算机图形学基础》(第二版)(p150 图5-67)进行填充
边缘填充算法:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
#define N 100000
#define Min -99999999
int n;
int right;
struct point
{
int x;
int y;
} point[N];
void init()
{
int i;
printf("请输入点的个数: ");
scanf("%d",&n);
right=Min;
for(i=0; i<n; i++)
{
scanf("%d %d",&point[i].x,&point[i].y);
if(point[i].x>right)
right=point[i].x;
}
point[n].x=point[0].x;
point[n].y=point[0].y;
}
void DDA(int x0,int y0,int x1,int y1)
{
int dx,dy,eps1,k;
float x,y,xIncre,yIncre;
int p,q;
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
if(abs(dx)>abs(dy))
eps1=abs(dx);
else
eps1=abs(dy);
xIncre=(float)dx/(float)eps1;
yIncre=(float)dy/(float)eps1;
for(k=0; k<=eps1; k++)
{
p=(int)(x+0.5);
q=(int)(y+0.5);
if(getpixel(p,q)==BLUE)
putpixel(p,q,RED);
else
putpixel(p,q,BLUE);
x+=xIncre;
y+=yIncre;
}
}
void sol1()
{
int i;
int x0,y0,x1,y1;
setbkcolor(BLUE);
cleardevice();
setcolor(RED);
for(i=0; i<n; i++)
{
x0=point[i].x;
y0=point[i].y;
x1=point[i+1].x;
y1=point[i+1].y;
line(x0,y0,x1,y1);
}
}
void sol2()
{
int x0,y0,x1,y1,i;
int dx,dy,eps1,k;
int p,q;
int flag;
int temp;
float x,y,xIncre,yIncre;
for(i=0; i<n; i++)
{
x0=point[i].x;
y0=point[i].y;
x1=point[i+1].x;
y1=point[i+1].y;
if(y0>y1)
{
temp=y0;
y0=y1;
y1=temp;
temp=x0;
x0=x1;
x1=temp;
}
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
eps1=abs(dy);
xIncre=(float)dx/(float)eps1;
yIncre=(float)dy/(float)eps1;
flag=1;
for(k=0; k<=eps1; k++)
{
p=(int)(x+0.5);
q=(int)(y+0.5);
if(k!=0)
DDA(p,q,right,q);
x+=xIncre;
y+=yIncre;
}
}
}
int main()
{
init();
initgraph(500,500);
sol1();
sol2();
getch();
closegraph();
return 0;
}
边界填充算法:
#include <iostream>
#include "stdio.h"
#include <stdlib.h>
#include <cmath>
#include <gl/glut.h>
#include <windows.h>
#include <gl/gl.h>
using namespace std;
typedef float Color[3];
void init ()
{
glClearColor (1.0, 1.0, 1.0,0);
glMatrixMode (GL_PROJECTION);
gluOrtho2D (0.0, 10.0, 0.0, 10.0);
}
void setPixel(GLint x,GLint y)
{
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glFlush();
}
void getPixel(GLint x, GLint y, Color c)
{
glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,c);
}
bool ColorEqual(Color c1,Color c2)
{
return (fabs(c1[0]-c2[0])<0.001 && fabs(c1[1]-c2[1])<0.001 && fabs(c1[2]-c2[2])<0.001) ;
}
void boundaryFill4 (int x, int y, Color fillColor, Color borderColor)
{
Color Color;
getPixel (x, y, Color);
if (!(ColorEqual(Color,borderColor)) && !(ColorEqual(Color,fillColor)))
{
setPixel(x, y);
boundaryFill4 (x + 1, y , fillColor, borderColor);
boundaryFill4 (x - 1, y , fillColor, borderColor);
boundaryFill4 (x , y + 1, fillColor, borderColor);
boundaryFill4 (x , y - 1, fillColor, borderColor);
}
}
void drawBoundaryPolygon()
{
Color a= {0.0,0.0,1.0},b= {0.0,1.0,1.0};
glColor3fv(b);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(4.0);
glBegin(GL_LINE_LOOP);
glVertex2i(1,1);
glVertex2i(7,1);
glVertex2i(7,5);
glVertex2i(6,4);
glVertex2i(4,6);
glVertex2i(3,2);
glVertex2i(2,3);
glEnd();
glColor3fv(a);
boundaryFill4(70,60,a,b);
glFlush();
}
void myDisplay ()
{
glClear(GL_COLOR_BUFFER_BIT);
Color color1= {0.0,0.0,1.0};
Color color2= {1.0,0.0,0.0};
glColor3fv(color1);
glLineWidth(2.0);
drawBoundaryPolygon();
glColor3fv(color2);
boundaryFill4(100,100,color1,color2);
glFlush(); //清空OpenGL命令缓冲区,执行OpenGL程序
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowPosition(400,200);
glutInitWindowSize(400,400);
glutCreateWindow("边界填充算法");
init(); //初始化函数
glutDisplayFunc(myDisplay);
glutMainLoop();
return 0;
}