void LineBres(int x0,int y0,int xEnd,int yEnd){
double m=double (yEnd-y0) / (xEnd - x0);
int dx=fabs(xEnd - x0);
int dy=fabs(yEnd - y0);
int p = 2 * dy -dx;
int p1 = 2 * dx -dy;
int twoDy = 2 * dy;
int twoDx = 2 * dx;
int twoDyMinusDx = 2 * (dy-dx);
int twoDxMinusDy = 2 * (dx - dy);
int x,y;
glBegin(GL_POINTS);
if(x0 == xEnd){
x=x0;
if(y0>yEnd){
y=yEnd;
yEnd = y0;
}
else
y=y0;
glVertex2i(x,y);
while(y<yEnd)
{
y++;
glVertex2i(x,y);
}
}
else if(y0 == yEnd){
y=y0;
if(x0>xEnd){
x=xEnd;
xEnd =x0;
}
else
x=x0;
glVertex2i(x,y);
while(x<xEnd)
{
x++;
glVertex2i(x,y);
}
}
else if(fabs(m)<1){
if(x0>xEnd){
x=xEnd;
y=yEnd;
xEnd = x0;
}
else
{
x=x0;
y=y0;
}
glVertex2i(x,y);
while(x<xEnd){
x++;
if(p<0)
p=p+twoDy;
else{
if(m>=0)
y++;
else
y--;
p=p+twoDyMinusDx;
}
glVertex2i(x,y);
}
}
else{
if(y0>yEnd){
x=xEnd;
y=yEnd;
yEnd = y0;
}
else
{
x=x0;
y=y0;
}
glVertex2i(x,y);
while(y<yEnd){
y++;
if(p1<0)
p1 = p1 + twoDx;
else
{
if(m>=0)
x++;
else
x--;
p1=p1+twoDxMinusDy;
}
glVertex2i(x,y); }
}
glEnd();
}
void bers(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
glPointSize(2);
LineBres(50,0,50,90);
LineBres(10,10,90,60);
LineBres(10,60,90,10);
LineBres(10,50,90,50);
glFlush();
}
void initbers(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,100,0,100);
}
//区域填充
void BorderBres(int x[],int y[],int length){
int i;
glBegin(GL_LINE_LOOP);
for(i=0;i<length;i++)
glVertex2i(x[i],y[i]);
glEnd();
glColor3f(1.0,0.0,0.0);
}
int isEqual(float a[],float b[]){
int i;
for(i=0;i<3;i++)
{
if(a[i] != b[i])
return 0;
}
return 1;
}
int fillapoint(int x0,int y0){
float getColor[3];
glReadPixels(x0,y0,1,1,GL_RGB,GL_FLOAT,getColor);
if((!isEqual(getColor,borderColor)) && (!isEqual(getColor,fillColor)))
{
glColor3f(fillColor[0],fillColor[1],fillColor[2]);
glBegin(GL_POINTS);
glVertex2i(x0,y0);
glEnd();
return 1;
}
return 0;
}
void fill(int x0,int y0){
if(fillapoint(x0,y0))
{
fill(x0+1,y0);
fill(x0-1,y0);
fill(x0,y0-1);
fill(x0,y0+1);
}
}
void area(void)
{
glPointSize(1);
int x[100],y[100];
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0,0.0,0.0);
x[0] = 250; y[0] = 250;
x[1] = 250; y[1] = 350;
x[2] = 350;y[2] = 350;
x[3] = 350;y[3] = 250;
BorderBres(x,y,4);
glFlush();
}
void initarea(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,600,0,600);
}
void mouse2(GLint button,GLint action,GLint xMouse,GLint yMouse){
if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN)
{fill(260,260);
glFlush();
}
}
//几何变换
void inittrans(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,600.0,0.0,600.0);
}
void polygon(wcPt2D verts[],int n) //初始多边形
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
{
glVertex2f(verts[i].x,verts[i].y);
}
glEnd();
glFlush();
}
void rotatePolygon(wcPt2D verts[],int n,wcPt2D pivPt,GLdouble theta) //旋转函数
{
glClear(GL_COLOR_BUFFER_BIT);
wcPt2D vertsRot;
int i;
for(i=0;i<n;i++){
vertsRot.x = pivPt.x + (verts[i].x - pivPt.x) * cos(theta) - (verts[i].y - pivPt.y) * sin(theta);
vertsRot.y = pivPt.y + (verts[i].x - pivPt.x) * sin(theta) + (verts[i].y - pivPt.y) * cos(theta);
verts[i].x = vertsRot.x;
verts[i].y = vertsRot.y;
}
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
glVertex2f(verts[i].x,verts[i].y);
glEnd();
glFlush();
}
void translatePolygon(wcPt2D verts[],int n,int tx,int ty) //平移函数
{
glClear(GL_COLOR_BUFFER_BIT);
int i;
for(i=0;i<n;i++)
{
verts[i].x = verts[i].x + tx;
verts[i].y = verts[i].y + ty;
}
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
glVertex2f(verts[i].x,verts[i].y);
glEnd();
glFlush();
}
void scalePolygon(wcPt2D verts[],int n,wcPt2D fixedPt,GLfloat sx,GLfloat sy) //缩放函数
{
glClear(GL_COLOR_BUFFER_BIT);
int i;
for(i=0;i<n;i++){
verts[i].x = verts[i].x * sx + fixedPt.x * (1-sx);
verts[i].y = verts[i].y * sy + fixedPt.y * (1-sy);
}
glBegin(GL_POLYGON);
for(i=0;i<n;i++)
glVertex2f(verts[i].x,verts[i].y);
glEnd();
glFlush();
}
void trans(void)
{
polygon(verts,n);
}
void key(GLubyte key,GLint xMouse,GLint yMouse){ //鼠标函数
switch(key){
case 'a':
translatePolygon(verts,n,25,25);
break;
case 'q':
translatePolygon(verts,n,-25,-25);
break;
case 's':
scalePolygon(verts,n,verts[0],2,2);
break;
case 'w':
scalePolygon(verts,n,verts[0],0.5,0.5);
break;
case 'd':
rotatePolygon(verts,n,verts[0],3.14/6);
break;
case 'e':
rotatePolygon(verts,n,verts[0],-3.14/6);
break;
}
}
//裁剪
typedef enum { Left,Right,Bottom,Top } Boundary;
wcPt2D vertss[] = {{200,150},{200,450},{600,450},{600,150}};
wcPt2D originalverts[15] = {{400,500},{100,300},{400,50},{700,300}};
int num = 4;
wcPt2D changeverts[15];
wcPt2D winMin = {200,150};
wcPt2D winMax = {600,450};
int nu=4;
GLint inside(wcPt2D p,Boundary b,wcPt2D wMin,wcPt2D wMax)
{
switch(b){
case Left:if(p.x < wMin.x) return false;break;
case Right:if(p.x > wMax.x) return false;break;
case Bottom:if(p.y < wMin.y) return false;break;
case Top:if(p.y > wMax.y) return false;break;
}
return true;
}
GLint kind(wcPt2D p1,wcPt2D p2,Boundary winEdge,wcPt2D wMin,wcPt2D wMax)
{
if(!inside(p1,winEdge,wMin,wMax) && inside(p2,winEdge,wMin,wMax))
return 0;
if(inside(p1,winEdge,wMin,wMax) && inside(p2,winEdge,wMin,wMax))
return 1;
if(inside(p1,winEdge,wMin,wMax) && !inside(p2,winEdge,wMin,wMax))
return 2;
else
return 3;
}
wcPt2D intersect(wcPt2D p1,wcPt2D p2,Boundary winEdge,wcPt2D wMin,wcPt2D wMax)
{
wcPt2D iPt;
GLfloat m;
if(p1.x != p2.x) m = (p1.y-p2.y) / (p1.x - p2.x);
switch(winEdge){
case Left:
iPt.x = wMin.x;
iPt.y = p2.y + (wMin.x - p2.x) * m;
break;
case Right:
iPt.x = wMax.x;
iPt.y = p2.y + (wMax.x - p2.x ) * m;
break;
case Bottom:
iPt.y = wMin.y;
if(p2.x != p1.x)
iPt.x = p2.x + (wMin.y - p2.y) / m;
else
iPt.x = p2.x;
break;
case Top:
iPt.y = wMax.y;
if(p2.x != p1.x)
iPt.x = p2.x + (wMax.y - p2.y) / m;
else
iPt.x = p2.x;
break;
}
return iPt;
}
void Clip(int n1,wcPt2D verts1[],wcPt2D verts2[],Boundary winEdge,wcPt2D wMin,wcPt2D wMax)
{
int k = 0,i;
for(i=0;i<n1;i++)
{
switch(kind(verts1[i],verts1[(i+1)%n1],winEdge,wMin,wMax)){
case 0:
verts2[k++]=intersect(verts1[i],verts1[(i+1)%n1],winEdge,wMin,wMax);
verts2[k++]=verts1[(i+1)%n1];
break;
case 1:
verts2[k++]=verts1[(i+1)%n1];
break;
case 2:
verts2[k++]=intersect(verts1[i],verts1[(i+1)%n1],winEdge,wMin,wMax);
break;
case 3:
break;
}
}
num=k;
}
void polygonClipSuthHodg(wcPt2D verts1[],wcPt2D verts2[],wcPt2D wMin,wcPt2D wMax)
{
Clip(num,verts1,verts2,Left,wMin,wMax);
Clip(num,verts2,verts1,Right,wMin,wMax);
Clip(num,verts1,verts2,Bottom,wMin,wMax);
Clip(num,verts2,verts1,Top,wMin,wMax);
}
void initcut(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,800.0,0.0,600.0);
}
void lineloop(wcPt2D vertss[],int nu) //初始多边形
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINE_LOOP);
for(i=0;i<nu;i++)
{
glVertex2f(vertss[i].x,vertss[i].y);
}
glEnd();
glFlush();
polygonClipSuthHodg(originalverts,changeverts,winMin,winMax);
glBegin(GL_POLYGON);
for(i=0;i<num;i++)
glVertex2f(originalverts[i].x,originalverts[i].y);
glEnd();
glFlush();
}
void cut( void)
{
lineloop(vertss,nu);
}
//曲线
double a[][2]={{100,120},{150,270},{250,270},{300,120}};
void bezier()
{
glColor3f(1.0,0.0,0.0);
double x1=a[0][0],y1=a[0][1],x2,y2;
glLineWidth(2.0);
for(double t=0;t<=1;t+=0.1)
{
x2=pow((1-t),3)*a[0][0]+3*pow((1-t),2)*t*a[1][0]+3*t*t*(1-t)*a[2][0]+t*t*t*a[3][0];
y2=pow((1-t),3)*a[0][1]+3*pow((1-t),2)*t*a[1][1]+3*t*t*(1-t)*a[2][1]+t*t*t*a[3][1];
glBegin(GL_LINES);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
x1=x2;
y1=y2;
}
glFlush();
}
void bez()
{
glColor3f(0.0,0.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(2.0);
glBegin(GL_LINES);
glVertex2f(a[0][0],a[0][1]);
glVertex2f(a[1][0],a[1][1]);
glEnd();
glBegin(GL_LINES);
glVertex2f(a[1][0],a[1][1]);
glVertex2f(a[2][0],a[2][1]);
glEnd();
glBegin(GL_LINES);
glVertex2f(a[2][0],a[2][1]);
glVertex2f(a[3][0],a[3][1]);
glEnd();
glFlush();
bezier();
}
void initbez(void)
{
glClearColor(1.0,1.0,1.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,600.0,0.0,600.0);
}
void mouse(GLint button,GLint action,GLint xMouse,GLint yMouse){
}
void key1(GLubyte key,GLint xMouse,GLint yMouse){
}
void renderScene(void) {
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glVertex2f(0.5,0.5);
glVertex2f(-0.5,0.5);
glVertex2f(0,-0.5);
glEnd();
glFlush();
}
void processMenu(int option)
{
switch (option) {
case 1:
initbers();
glutDisplayFunc(bers);
glutMouseFunc(mouse);
glutKeyboardFunc(key1);
glutPostRedisplay();
break;
case 2:
initarea();
glutDisplayFunc(area);
glutMouseFunc(mouse2);
glutKeyboardFunc(key1);
glutPostRedisplay();
break;
case 3:
inittrans();
glutDisplayFunc(trans);
glutMouseFunc(mouse);
glutKeyboardFunc(key);
glutPostRedisplay();
break;
case 4:
initcut();
glutDisplayFunc(cut);
glutMouseFunc(mouse);
glutKeyboardFunc(key1);
glutPostRedisplay();
break;
case 5:
initbez();
glutDisplayFunc(bez);
glutMouseFunc(mouse);
glutKeyboardFunc(key1);
glutPostRedisplay();
break;
default:
break;
}
}
简单图形变换
最新推荐文章于 2022-06-16 10:54:32 发布