计算机图形学问题:编写中点分割的线段裁剪算法?

敬请回复,O(∩_∩)O谢谢!... 敬请回复,O(∩_∩)O谢谢! 展开
 我来答
百度网友e4639e7
2010-05-13 · TA获得超过111个赞
知道答主
回答量:94
采纳率:0%
帮助的人:99.3万
展开全部
#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");
init ();
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}

////以前写的 不知道有没有用
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式