-
大小: 3KB文件類型: .rar金幣: 2下載: 0 次發(fā)布日期: 2021-06-15
- 語言: 其他
- 標簽:
資源簡介
實現(xiàn)中點分割剪裁算法,用不同顏色畫出固定的剪裁矩形窗口和線段,然后運行剪裁命令,做剪裁,再顯示剪裁結(jié)果。線段包括所有類型的5種線段。

代碼片段和文件信息
//?OGL1.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
#include?“windows.h“
#include? //?Header?File?For?The?OpenGL32?Library
#include? //?Header?File?For?The?GLu32?Library
#include? //?Header?File?For?The?Glaux?Library
#include? //?Header?File?For?The?OpenGL32?tools?Library
#include
#include
#define?LEFT?1
#define?RIGHT?2
#define?BOTTOM?4
#define?TOP?8
#define?XL?100
#define?XR?300
#define?YB?100
#define?YT?200
//int?x1x2y1y2;
//坐標點編碼
typedef?struct?point
{
int?x;
int?y;
int?code;
}Point;
//給坐標點編碼
int?encode(int?xint?y)
{
?
int?c;
c=0;
?
if(x c=c|LEFT;
else?if?(x>XR)?
c=c|RIGHT;
if(y else?if(y>YT)
c=c|TOP;
return?c;
}
//判斷線段兩端點與窗口的位置關(guān)系
int?judge(int?code1int?code2)
{
if(?(code1?==?code2)?&&?code1==0)
{
//printf(“線段在窗口內(nèi)。\n“);
return?1;
}
else?if(?(code1?&?code2)?!=?0)
{
//printf(“線段在窗口外。\n“);
return?-1;
}
else?
{
//printf(“需要求交點。\n“);
return?0;
}
}
//不斷中點求交點
Point?point_of_intersection(Point?P1?Point?P2)
{
printf(“in?point?intersection\n“?);
// printf(“P1.x?is?%d?P1.y=?%dP1.code?is?%d\n“P1.x??P1.y??P1.code?);
Point?P;
// printf(“P1.x?-?P2.x=?%d\n“P1.x?-?P2.x?);
while(?abs(P2.x?-?P1.x)?>?1)
{
P1.code?=?encode(P1.x?P1.y);
//求中點坐標并編碼
P.x?=?(P1.x?+?P2.x)?/?2;
P.y?=?(P1.y?+?P2.y)?/?2;
P.code?=?encode(P.x?P.y);
// printf(“p.code?is?%d?\n“P.code?);
// printf(“p.x?is?%d?p.y?is?%d\n“P.xP.y?);
if(?(P1.code?&?P.code)?!=?0)
P1?=?P;
else
P2?=?P;
}
return?P2;
}
void?init?(void)
{
????glClearColor?(1.0?1.0?1.0?0.0);??//?Set?display-window?color?to?white.
????glMatrixMode?(GL_PROJECTION);???????//?Set?projection?parameters.
gluOrtho2D?(0.0?400.0?0.0?300.0);
}
void?initPoints(Point?*P)
{
//定義第一組線段:一半在窗口內(nèi)一半在窗口外
P[0].x=60;P[0].y=140;
P[0].code=encode(P[0].xP[0].y);
P[1].x=140;P[1].y=160;
P[1].code=encode(P[1].xP[1].y);
//定義第二組線段:全在窗口內(nèi)
P[2].x?=?120;P[2].y=120;
P[2].code=encode(P[2].xP[2].y);
P[3].x=200;P[3].y=150;
P[3].code=encode(P[3].xP[3].y);
//定義第三組線段:全在左上角窗口外
P[4].x?=?20;P[4].y=180;
P[4].code=encode(P[4].xP[4].y);
P[5].x?=?80;P[5].y=220;
P[5].code=encode(P[5].xP[5].y);
//定義第四組線段:全在右下角窗口外
P[6].x?=?200;P[6].y=50;
P[6].code=encode(P[6].xP[6].y);
P[7].x?=?350;P[7].y=120;
P[7].code=encode(P[7].xP[7].y);
//定義第五組線段:橫穿窗口
P[8].x?=?120;P[8].y=250;
P[8].code=encode(P[8].xP[8].y);
P[9].x?=?320;P[9].y=150;
P[9].code=encode(P[9].xP[9].y);
}
void?DrawLines()
{
// 定義10個坐標點,表示五組線段
Point?P[10];
initPoints(P);
for(int?i?=?0;?i?10;?)
{
Point?temp;
Point?jd1?jd2;
int?ifneed; //是否需要求交點
ifneed?=?judge(P[i].code?P[i?+?1].code);
/* glVertex2i?(P[i].x?P[i].y);???????//?Specify?line-segment?geometry.
glVertex2i?(P[i+1].x?P[i+1].
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????6798??2014-05-16?19:02??中點分割裁剪算法\MidpointLine.cpp
?????文件????????291??2013-02-26?00:00??中點分割裁剪算法\StdAfx.cpp
?????文件????????769??2013-02-26?00:00??中點分割裁剪算法\StdAfx.h
?????目錄??????????0??2014-05-28?13:28??中點分割裁剪算法
-----------?---------??----------?-----??----
?????????????????7858????????????????????4
評論
共有 條評論