计算机图形学程序

计算机图形学程序,第1张

#include <GL/glut.h>

#include <stdio.h>

#include <stdlib.h>

void LineGL(int x0,int y0,int x1,int y1)

{

glBegin (GL_LINES)

glColor3f (1.0f, 0.0f, 0.0f) glVertex2f (x0,y0)

glColor3f (0.0f, 1.0f, 0.0f) glVertex2f (x1,y1)

glEnd ()

}

struct outcode

{

unsigned all

unsigned left,right,top,bottom

}

struct Rectangle

{

float xmin,xmax,ymin,ymax

}

Rectangle rect

int x0,y0,x1,y1

GLboolean bClip = false

void compoutcode(float x,float y,Rectangle rect,outcode *outcode)

{

outcode->all=0

outcode->top=outcode->bottom=0

if(y>(float)rect.ymax)

{

outcode->top=1

outcode->all+=1

}

else if(y<(float)rect.ymin)

{

outcode->bottom=1

outcode->all+=1

}

outcode->right=outcode->left=0

if(x>(float)rect.xmax)

{

outcode->right=1

outcode->all+=1

}

else if(x<(float)rect.xmin)

{

outcode->left=1

outcode->all+=1

}

}

int cohensutherlandlineclip(Rectangle rect, int &x0,int &y0,int &x1,int &y1)

{

int accept,done

outcode outcode0,outcode1

outcode * outcodeout

float x,y

accept=0

done=0

compoutcode(x0,y0,rect,&outcode0)

compoutcode(x1,y1,rect,&outcode1)

do{

if(outcode0.all==0&&outcode1.all==0)

{

accept=1

done=1

}

else if(outcode0.all&outcode1.all!=0)

done=1

else

{

if(outcode0.all!=0)

outcodeout=&outcode0

else

outcodeout=&outcode1

if(outcodeout->left)

{

y=y0+(y1-y0)*(rect.xmin-x0)/(x1-x0)

x=(float)rect.xmin

}

else if(outcodeout->top)

{

x=x0+(x1-x0)*(rect.ymax-y0)/(y1-y0)

y=(float)rect.ymax

}

else if(outcodeout->right)

{

y=y0+(y1-y0)*(rect.xmax-x0)/(x1-x0)

x=(float)rect.xmax

}

else if(outcodeout->bottom)

{

x=x0+(x1-x0)*(rect.ymin-y0)/(y1-y0)

y=(float)rect.ymin

}

if(outcodeout->all==outcode0.all)

{

x0=xy0=ycompoutcode(x0,y0,rect,&outcode0)

}

else

{

x1=xy1=y

compoutcode(x1,y1,rect,&outcode1)

}

}

}while(!done)

if(accept)

LineGL(x0,y0,x1,y1)

return accept

}

void myDisplay()

{

glClear(GL_COLOR_BUFFER_BIT)

glColor3f (1.0f, 0.0f, 0.0f)

glRectf(rect.xmin,rect.ymin,rect.xmax,rect.ymax)

if (!bClip)

LineGL(x0,y0,x1,y1)

else

cohensutherlandlineclip(rect, x0,y0,x1,y1)

glFlush()

}

void Init()

{

glClearColor(0.0, 0.0, 0.0, 0.0)

glShadeModel(GL_FLAT)

rect.xmin=100//窗口的大小

rect.xmax=300

rect.ymin=100

rect.ymax=300

x0 = 450,y0 = 0, x1 = 0, y1 = 450//裁剪前的直线端点

printf("Press key 'c' to Clip!\nPress key 'r' to Restore!\n")

}

void Reshape(int w, int h)

{

glViewport(0, 0, (GLsizei) w, (GLsizei) h)

glMatrixMode(GL_PROJECTION)

glLoadIdentity()

gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h)

}

void keyboard(unsigned char key, int x, int y)

{

switch (key)

{

case 'c':

bClip = true

glutPostRedisplay()//重画

break

case 'r':

bClip = false

Init()

glutPostRedisplay()//

break

case 'x':

exit(0)

break

default:

break

}

}

int main(int argc, char *argv[])

{

glutInit(&argc, argv)

glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE)

glutInitWindowPosition(100, 100)

glutInitWindowSize(640, 480)

glutCreateWindow("Hello World!")

Init()

glutDisplayFunc(myDisplay)

glutReshapeFunc(Reshape)

glutKeyboardFunc(keyboard)

glutMainLoop()

return 0

}

正好也在做试验, 这个是裁剪算法,我在VC下运行无错误

还有torbc?是turbo c吧?因为编译环境有些区别,而网上找的又都是VC的,所以都会遇到错误。

建议去装个VC的 ,毕竟VC比turbo c主流

这个要你自己给出起点和终点:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <graphics.h>

void swap_start_end(int &x1,int &y1,int &x2,int &y2)

void s_line(int x1,int y1,int x2,int y2)

main()

{

int x1,y1,x2,y2

printf("Please input the start dot:\n")

scanf("%d%d",&x1,&y1)

printf("Please input the end dot:\n")

scanf("%d%d",&x2,&y2)

swap(x1,y1,x2,y2)

s_line(x1,y1,x2,y2)

}

void swap_start_end(int &x1,int &y1,int &x2,int &y2)

{

int mid

mid=x1

x1=y1

y1=mid

mid=x2

x2=y2

y2=mid

}

void s_line(int x1,int y1,int x2,int y2)

{

int driver=DETECT,mode

int curx,cury,dx,dy,tx,ty,mid,d,flag,i

initgraph(&driver,&mode,"")

curx=x1

cury=y1

dx=abs(x2-x1)

dy=abs(y2-y1)

if(x2-x1>=0)

tx=1

else

tx=-1

if(y2-y1>=0)

ty=1

else

ty=-1

if(dx>dy)

flag=0

else

{

flag=1

mid=dxdx=dydy=mid

mid=txtx=tyty=mid

mid=x1x1=y1y1=mid

mid=x2x2=y2y2=mid

}

if(flag)

putpixel(cury,curx,2)

else

putpixel(curx,cury,2)

d=2*dy-dx

for(i=0i<dxi++)

{

if(d>=0)

{

d=d+2*(dy-dx)

cury+=ty

}

else

d=d+2*dy

curx+=tx

if(flag)

putpixel(cury,curx,2)

else

putpixel(curx,cury,2)

}

getch()

closegraph()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7804795.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-10
下一篇 2023-04-10

发表评论

登录后才能评论

评论列表(0条)

保存