📄 02824523_wanglili.cpp
字号:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glaux.h>
#include <gl/glu.h>
#include <iostream.h>
#include "math.h"
//.......
int j=0;
double dep[500][500]={0};
double A[10],B[10],C[10],D[10]; //平面方程Ax+By+Cz+D=0; z=temp!
//........
struct poly
{
int x[10];
int y[10];
int count;
};
struct poly p[5];
int a,b;
int flag;
int pnum; //记录欲处理的多变形的个数;%%%%%%%%%
#define SCREEN_WIDTH 500
//-----------------------------------
//Here, please define your own data structure for edge Table, Active Edge Table, Edge
//-----------------------------------
int inside(int x0,int y0,int t)
{
int m=t;
int num=0;
for(int i=0;i<p[m].count;i++)
{
if((y0>=p[m].y[i]&&y0<p[m].y[i+1])||(y0>=p[m].y[i+1]&&y0<p[m].y[i]))
{
int dx=p[m].x[i]+(p[m].x[i+1]-p[m].x[i])*(y0-p[m].y[i])/(p[m].y[i+1]-p[m].y[i]);//能处理垂直
if(dx>x0)
num++;
}
}
if(num%2!=0)
return 1;
else
return 0;
}
void DrawPoint(int x, int y)
{
//.............
// glColor3f (1.0, 1.0, 1.0);
//..............
::glBegin(GL_POINTS);
::glVertex2d(x, y);
::glEnd();
}
//.............
//...........
void CALLBACK Reshape(GLsizei w, GLsizei h)
{
::glViewport(0, 0, w, h);
::glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, SCREEN_WIDTH, 0.0, SCREEN_WIDTH*(GLfloat)h/(GLfloat)w);
else
gluOrtho2D (0.0, (GLfloat)SCREEN_WIDTH*(GLfloat)w/(GLfloat)h, 0.0, SCREEN_WIDTH);
glMatrixMode(GL_MODELVIEW);
return;
}
void CALLBACK Display()
{
//int j=0;
int color=1;
::glColor3f(1.0, 0.0, 0.0);
//-----------------------------------
//
// your scan conversion result here
do
{
for(b=1;b<500;b++)
for(a=1;a<500;a++)
{
if(inside(a,b,j))
{
if(flag==2)
{
if(color%2==0)
::glColor3f(1.0, 0.0, 0.0);//不是空心改颜色!!!!!!
else
::glColor3f(0.0, 1.0, 0.0);
}
else if(flag==1)
::glColor3f(0.0, 1.0, 0.0);
double temp=fabs(-(A[j]*a+B[j]*b+D[j])/C[j]); //保存当前点的最大深度
if(temp>dep[a][b]) //画出深度最大的点的颜色
{
dep[a][b]=temp;
DrawPoint(a,b);
}
}
}
color++;
}while(j++<pnum);
//-----------------------------------
::glFlush();
return;
}
void Init()
{
::auxInitDisplayMode(AUX_RGBA|AUX_SINGLE);
::auxInitPosition(0, 0, 500,500);//????????????????????????????
::auxInitWindow("Scan algithmic");
::glShadeModel(GL_FLAT);
::glClearColor(0.0, 0.0, 0.0,0.0);//////////
}
void main(int argc, char **argv)
{
// Init();
//-----------------------------------
// implement your scan conversion algorithm here!
//cout<<"是否处理多个多边形?\n1---是\t2---否\n"<<endl;
//cin>>flag;
flag=2;
cout<<"输入欲处理的多变形的个数:"<<endl;
cin>>pnum;
/* for(int k=0;k<pnum;k++)
{
cout<<"输入边数:"<<endl;
cin>>p[k].count;
cout<<"输入各个顶点的x坐标:"<<endl;
for(int i=0;i<p[k].count;i++)
{
cin>>p[k].x[i];
}
p[k].x[p[k].count]=p[k].x[0];
cout<<"输入各个顶点的y坐标:"<<endl;
for(i=0;i<p[k].count;i++)
{
cin>>p[k].y[i];
}
p[k].y[p[k].count]=p[k].y[0];
}*/
for(int k=0;k<pnum;k++)
{
cout<<"输入边数:"<<endl;
cin>>p[k].count;
cout<<"输入所要绘制的图形所在平面方程Ax+By+Cz+D=0的系数:"<<endl;
cout<<"A=";
cin>>A[k];
cout<<"\nB=";
cin>>B[k];
cout<<"\nC=";
cin>>C[k];
cout<<"\nD=";
cin>>D[k];
cout<<endl;
for(int i=0;i<p[k].count;i++)
{
cout<<"输入第"<<i+1<<"个顶点的x坐标:"<<endl;
cin>>p[k].x[i];
cout<<"输入第"<<i+1<<"个顶点的y坐标:"<<endl;
cin>>p[k].y[i];
}
p[k].x[p[k].count]=p[k].x[0];
p[k].y[p[k].count]=p[k].y[0];
}
Init();
//-----------------------------------
::auxReshapeFunc(Reshape);
::auxMainLoop(Display);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -