VC++中OPENGL种子填充算法

shamfox 2005-12-05 05:36:01
/*边界表示的种子填充算法*/

#include <windows.h>
//#include <graphics.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glaux.h>
#include <math.h>
#include <iostream.h>
#include <stdio.h>
#include <malloc.h>
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"glaux.lib")
#define NULL 0
#define MAXSIZE 10000

int fill_color=RGB(1.0,1.0,0.0);
int boundary_color=RGB(1.0,0.0,0.0);

struct node{
float x,y;
int color;
}a[MAXSIZE];
int top=0; //栈顶指针

float m=300.0f; //圆的半径及圆心
float n=300.0f;
float radiu=200.0f;

void draw(float x,float y) //画点
{
glColor3f(1.0,1.0,0.0); //与boundary_color一致
glBegin(GL_POINTS);
glVertex2f(x,y);
glEnd();
glFlush();
}

void pushpixel(float x,float y)
{
int i;
for (i=0;i<top;i++)
if (x==a[top].x && y==a[top].y) return; //已在栈中
a[top].x=x;
a[top].y=y;
if (x*x+y*y==radiu*radiu) a[top].color=boundary_color; //边界点
else a[top].color=-1; //未被画过
top++;
}

struct node poppixel()
{
struct node p;
p.x=a[top].x;
p.y=a[top].y;
p.color=a[top].color;
top--;
return(p);

}

void seedfilling(float x,float y)
{
struct node p;
int c;
if ((x-m)*(x-m)+(y-n)*(y-n)<=radiu*radiu)
pushpixel(x,y);/*当前点压栈*/

while (top!=-1)
{
p=poppixel();
c=p.color;
if ((c!=boundary_color) && (c!=fill_color)) /*如果颜色为边界色则不填充*/

{
draw(p.x,p.y);/*当前点出栈,画点*/

if ((x+1-m)*(x-m)+(y-n)*(y-n)<=radiu*radiu) seedfilling(x+1,y);

if ((x-1-m)*(x-m)+(y-n)*(y-n)<=radiu*radiu) seedfilling(x-1,y);

if ((x-m)*(x-m)+(y+1-n)*(y+1-n)<=radiu*radiu) seedfilling(x,y+1);

if ((x-m)*(x-m)+(y-1-n)*(y-1-n)<=radiu*radiu) seedfilling(x,y-1);

}
}

}

void CALLBACK fillseed(void)
{
glClear(GL_COLOR_BUFFER_BIT);
seedfilling(m,n);
}

void main()
{
auxInitDisplayMode(AUX_SINGLE | AUX_RGBA);
auxInitPosition(50,50,700,600);
auxInitWindow("An Example of OpenGL");
auxMainLoop(fillseed);
}



...全文
682 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
寻开心 2005-12-05
  • 打赏
  • 举报
回复
可以参考这个例子
http://www.codeproject.com/gdi/QuickFill.asp

19,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧