资源简介

基于openGL的四种直线裁减算法,cohen-sutherland算法,中点分割裁剪算法,梁友栋算法,beck算法。

资源截图

代码片段和文件信息

// Cys-Beck算法.cpp: 定义控制台应用程序的入口点。
//



#include    // use as needed for your system
#include 
#include    
#include 

//********* Data structure 
struct GLintPoint  //描述点的结构体 整型
{
GLint x y;
};

struct GLfloatPoint //描述点的结构体 浮点型
{
GLfloat x y;
};

const int MAX = 100;
class GLintPointArray  //类型为GLintPoint的数组类
{
public:
int num;  //计数
GLintPoint point[MAX]; //数组
};

class GLfloatPointArray  //类型为GLfloatPoint的数组类
{
public:
int num;
GLfloatPoint point[MAX];
};

//********** Support subprograms
typedef GLfloat colorType[3];  //点的颜色值

void drawDot(GLint x GLint y GLfloat r GLfloat g GLfloat b)
{
glColor3f(r g b);
glBegin(GL_POINTS);
glVertex2i(x y);
glEnd();
}

void drawIntPolygon(GLintPointArray P colorType c)
{
glColor3fv(c);
glBegin(GL_LINE_LOOP);
for (int i = 0; i glVertex2i(P.point[i].x P.point[i].y);
glEnd();
}

void drawFloatPolygon(GLfloatPointArray P colorType c)
{
glColor3fv(c);
glBegin(GL_LINE_LOOP);
for (int i = 0; i < P.num; i++)
glVertex2f(P.point[i].x P.point[i].y);
glEnd();
}

//**************** myInit 
void myInit(void)
{
glClearColor(1.0 1.0 1.0 0.0);  // set white background color
glColor3f(0.0f 0.0f 0.0f);    //default color
//glPointSize(2.0);        // a ‘dot‘ is 4 by 4 pixels
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0 640.0 0.0 480.0);
}

//**************  drawing subprograms go here

const int MaxN = 100;
typedef GLfloatPoint Normals[MaxN]; //数组

void buildfloatPolygon(GLfloatPointArray &P)
{
P.num = 5;


P.point[0].x = 100; P.point[0].y = 100;
P.point[1].x = 400; P.point[1].y = 100;
P.point[2].x = 400; P.point[2].y = 300;
P.point[3].x = 250; P.point[3].y = 400;
P.point[4].x = 100; P.point[4].y = 300;
}

float DotProduct(GLfloatPoint v1 GLfloatPoint v2)//两向量的乘积
{
return v1.x*v2.x + v1.y*v2.y;
}


void CalcNormals(GLfloatPointArray p Normals & n)//求每个顶点的内法矢
{
int i j k;
GLfloatPoint v;

for (i = 0; i < p.num; i++)
{
j = (i + 1) % p.num;
k = (i + (int)p.num - 1) % p.num;
if ((p.point[j].x - p.point[i].x) == 0) { //特殊情况 边框平行与y
n[i].x = 1; n[i].y = 0;
}
else if ((p.point[j].y - p.point[i].y) == 0) {//特殊情况  边框平行于x
n[i].x = 0; n[i].y = 1;
}
else {//一般情况 边框有斜率
float q = -((p.point[j].y - p.point[i].y) / (p.point[j].x - p.point[i].x));
n[i].x = 1; n[i].y = 1 / q;
}

v.x = p.point[k].x - p.point[i].x;
v.y = p.point[k].y - p.point[i].y;

if (DotProduct(n[i] v) < 0)  //通过求向量乘积来判断求的是内法矢量还是外法矢量
{
n[i].x *= -1;
n[i].y *= -1;
}
}
}


void CBClip(GLfloatPoint p1 GLfloatPoint p2 Normals &n GLfloatPointArray p
bool & visible GLfloatPoint & ps GLfloatPoint & pe)   //Beck裁减算法
{
float t1 t2 t;  //t1是下限最小值 t2是上限最大值
GLfloatPoint D w;//D=P2-P1 W=P1-fi
int i;
t1 = 0.0;
t2 = 1.0;
D.x = p2.x - p1.x;
D.y = p2.y - p1.y;

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-05-24 19:11  ComputerGraphicsexci5\
     目录           0  2018-05-24 19:11  ComputerGraphicsexci5\Beck算法\
     文件        5545  2018-05-22 17:30  ComputerGraphicsexci5\Beck算法\Beck.cpp
     文件        4021  2018-05-21 21:33  ComputerGraphicsexci5\Beck算法\Beck算法.vcxproj
     文件         945  2018-05-21 21:33  ComputerGraphicsexci5\Beck算法\Beck算法.vcxproj.filters
     目录           0  2018-05-24 19:11  ComputerGraphicsexci5\Beck算法\Debug\
     文件      175581  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck.obj
     文件        1357  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.Build.CppClean.log
     文件        1656  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.log
     目录           0  2018-05-24 19:11  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.tlog\
     文件         208  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.tlog\Beck算法.lastbuildstate
     文件       29412  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.tlog\CL.read.1.tlog
     文件         780  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.tlog\CL.write.1.tlog
     文件         790  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.tlog\cl.command.1.tlog
     文件        1450  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.tlog\link.command.1.tlog
     文件        3538  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.tlog\link.read.1.tlog
     文件         758  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\Beck算法.tlog\link.write.1.tlog
     文件      723968  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\vc120.idb
     文件      405504  2018-05-22 20:57  ComputerGraphicsexci5\Beck算法\Debug\vc120.pdb
     目录           0  2018-05-24 19:11  ComputerGraphicsexci5\CohenSotherland\
     文件        4085  2018-05-05 18:16  ComputerGraphicsexci5\CohenSotherland\CohenSotherland.vcxproj
     文件         956  2018-05-05 18:16  ComputerGraphicsexci5\CohenSotherland\CohenSotherland.vcxproj.filters
     文件        2615  2018-05-08 17:26  ComputerGraphicsexci5\CohenSotherland\CohenSutherLAND.cpp
     目录           0  2018-05-24 19:11  ComputerGraphicsexci5\CohenSotherland\Debug\
     文件        1733  2018-05-08 21:33  ComputerGraphicsexci5\CohenSotherland\Debug\CohenSotherland.log
     文件       15491  2018-05-08 21:33  ComputerGraphicsexci5\CohenSotherland\Debug\CohenSutherLAND.obj
     文件        1458  2018-05-08 21:33  ComputerGraphicsexci5\CohenSotherland\Debug\CohenSutherland.Build.CppClean.log
     目录           0  2018-05-24 19:11  ComputerGraphicsexci5\CohenSotherland\Debug\CohenSutherland.tlog\
     文件        2844  2018-05-08 21:33  ComputerGraphicsexci5\CohenSotherland\Debug\CohenSutherland.tlog\CL.read.1.tlog
     文件         896  2018-05-08 21:33  ComputerGraphicsexci5\CohenSotherland\Debug\CohenSutherland.tlog\CL.write.1.tlog
     文件         208  2018-05-08 21:33  ComputerGraphicsexci5\CohenSotherland\Debug\CohenSutherland.tlog\CohenSutherland.lastbuildstate
............此处省略90个文件信息

评论

共有 条评论