计算机图形学:线段剪裁中点分割算法,要求用C++做,急求源代码啊,谢谢! 752512212 5
展开全部
#include <GL/glut.h>#include <stdlib.h>#include "iostream.h"int x0,y0,x1,y1;int Max(int a,int b,int c){ if(a>b) { if(a>c) return a; else return c; } else { if(b>c) return b; else return c; }}int Min(int a,int b,int c){ if(a<b) { if(a<c) return a; else return c; } else { if(b<c) return b; else return c; }}void DrawLine1(int x0,int y0,int x1,int y1){ int d,temp; temp=y0; d=2*(y1-y0)-(x1-x0); glBegin(GL_POINTS); glVertex2d(x0,y0); glEnd(); for(int k=x0+1;k<x1;k++) { if(d>=0) { glBegin(GL_POINTS); glVertex2d(k,temp+1); glEnd(); d=d+2*(y1-y0)-2*(x1-x0); temp=temp+1; } else { glBegin(GL_POINTS); glVertex2d(k,temp); glEnd(); d=d+2*(y1-y0); temp=temp; } } glBegin(GL_POINTS); glVertex2d(x1,y1); glEnd();}void DrawLine2(int x0,int y0,int x1,int y1){ int d,temp; temp=x0; d=2*(x1-x0)-(y1-y0); glBegin(GL_POINTS); glVertex2d(x0,y0); glEnd(); for(int k=y0+1;k<y1;k++) { if(d>=0) { glBegin(GL_POINTS); glVertex2d(temp+1,k); glEnd(); d=d+2*(x1-x0)-2*(y1-y0); temp=temp+1; } else { glBegin(GL_POINTS); glVertex2d(temp,k); glEnd(); d=d+2*(x1-x0); temp=temp; } } glBegin(GL_POINTS); glVertex2d(x1,y1); glEnd();}void DrawTriangle(int x0,int y0,int x1,int y1,int x2,int y2){ int xmin,xmax,ymin,ymax; float a,b,c; xmin=Min(x0,x1,x2); xmax=Max(x0,x1,x2); ymin=Min(y0,y1,y2); ymax=Max(y0,y1,y2); glColor3f(1.0f,0.0f,0.0f); glBegin(GL_POINTS); glVertex2d(x0,y0); glEnd(); glColor3f(0.0f,1.0f,0.0f); glBegin(GL_POINTS); glVertex2d(x1,y1); glEnd(); glColor3f(0.0f,0.0f,1.0f); glBegin(GL_POINTS); glVertex2d(x2,y2); glEnd(); for(float n=ymin;n<=ymax;n++) for(float m=xmin;m<xmax;m++) { a=((y1-y2)*m+(x2-x1)*n+x1*y2-x2*y1)/((y1-y2)*x0+(x2-x1)*y0+x1*y2-x2*y1); b=((y2-y0)*m+(x0-x2)*n+x2*y0-x0*y2)/((y2-y0)*x1+(x0-x2)*y1+x2*y0-x0*y2); c=((y0-y1)*m+(x1-x0)*n+x0*y1-x1*y0)/((y0-y1)*x2+(x1-x0)*y2+x0*y1-x1*y0); if(a>0 && b>0 && c>0) { float color0=a*1.0; float color1=b*1.0; float color2=c*1.0; glColor3f(color0,color1,color2); glBegin(GL_POINTS); glVertex2d(m,n); glEnd(); } } }void display(){/* clear all pixels */ glClear (GL_COLOR_BUFFER_BIT); glColor3f (1.0, 1.0, 1.0); glBegin(GL_POINTS); glVertex2d(x,y); 中间是点的坐标 glEnd(); */ /*下面的语句是画一个白色的正方形*/ if((y1-y0)/(x1-x0)<=1) { DrawLine1(x0,y0,x1,y1); } else { DrawLine2(x0,y0,x1,y1); } DrawTriangle(35,35,135,185,235,35);/* don't wait! * start processing buffered OpenGL routines */ glFlush ();}void init (void) {/* select clearing color */ glClearColor (0.0, 0.0, 0.0, 0.0);/* initialize viewing values */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, 600, 0, 600);}/* * Declare initial window size, position, and display mode * (single buffer and RGBA). Open window with "hello" * in its title bar. Call initialization routines. * Register callback function to display graphics. * Enter main loop and process events. */int main(int argc, char** argv){ cout<<"input x0,y0,x1,y1 :"<<endl; cin>>x0>>y0>>x1>>y1; glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); //设置窗口大小,以像素为单位 glutInitWindowSize (600, 600); glutInitWindowPosition (100, 100); glutCreateWindow ("hello");
希望能够帮助到你,望采纳,谢谢!
希望能够帮助到你,望采纳,谢谢!
来自:求助得到的回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询