计算机图形学-基于OpenGL的直线段的裁剪算法

计算机图形学-基于OpenGL的直线段的裁剪算法

在Opengl应用框架下实现C-S裁剪算法或L-B裁剪算法。完成一个四边形对两条线段的裁剪:四边形的左上角和右下角顶点分别为(100,100),(300,200),线段1的两个端点为(150,50),(250,150)。集成开发环境为vs2013。

实验代码-Cohen-Sutherland裁剪算法

#include <GL/glut.h>
#include <cstdio>

#define LEFT 1
#define RIGHT 2
#define BOTTOM 4
#define TOP 8

int x1=150,y1=50, x2=50, y2=250, XL=100, XR=300, YB=100, YT=200;  //(x1,y1)、(x2,y2)为直线段的端点,XL为左边界,XR为右边界,YB为下边界,YT为上边界
int x1_init = 150, y1_init = 50, x2_init = 50, y2_init = 250;  //将直线段端点备份,以便画出裁剪前的直线段

int encode(int x,int y)
{
    int c = 0;
    if (x < XL) c |= LEFT;
    if (x > XR) c |= RIGHT;
    if (y < YB) c |= BOTTOM;
    if (y > YT) c |= TOP;
    return c;
}

void CS_LineClip()  //Cohen-Sutherland裁剪算法
{
    int x, y;
    int code1, code2, code;
    code1 = encode(x1, y1);
    code2 = encode(x2, y2);

    while (code1 != 0 || code2 != 0)
    {
        if (code1 & code2)
            return;
        if (code1 != 0)
            code = code1;
        else
            code = code2;

        if (LEFT & code)
        {
            x = XL;
            y = y1 + (y2 - y1)*(XL - x1) / (x2 - x1);
        }
        else if (RIGHT & code)
        {
            x = XR;
            y = y1 + (y2 - y1)*(XR - x1) / (x2 - x1);
        }
        else if (BOTTOM & code)
        {
            y = YB;
            x = x1 + (x2 - x1)*(YB - y1) / (y2 - y1);
        }
        else if (TOP & code)
        {
            y = YT;
            x = x1 + (x2 - x1)*(YT - y1) / (y2 - y1);
        }
        if (code == code1)
        {
            x1 = x; y1 = y; code1 = encode(x1, y1);
        }
        else
        {
            x2 = x; y2 = y; code2 = encode(x2, y2);
        }
    }

}


void init()  //初始化函数
{
    glClearColor(1.0, 1.0, 1.0, 0.0);  //设置背景颜色
    glMatrixMode(GL_PROJECTION);       // 设置投影参数
    gluOrtho2D(0.0, 600.0, 0.0, 400.0); // 设置场景的大小
    CS_LineClip();  //执行一次裁剪算法
}

void mydisplay()  //显示函数
{
    //绘制方形边界
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);   
    glPointSize(2);
    glBegin(GL_LINE_LOOP);
        glVertex2i(XL, YT);
        glVertex2i(XL, YB);
        glVertex2i(XR, YB);
        glVertex2i(XR, YT);
    glEnd();
    glFlush();
    //绘制未裁剪前的线段
    glBegin(GL_LINES);
        glVertex2i(x1_init, y1_init);
        glVertex2i(x2_init, y2_init);
    glEnd();
    glFlush();
    //绘制裁剪后的线段
    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_LINES);
        glVertex2i(x1, y1);
        glVertex2i(x2, y2);
    glEnd();
    glFlush();
}

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Cohen-Sutherland裁剪算法");
    init();
    glutDisplayFunc(&mydisplay);
    glutMainLoop();
    return 0;
}

效果预览

CS

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值