计算机图形学实验之直线段的编码裁剪算法(Cohen-SutherLand算法

实验目的: 掌握相关算法的原理及实现

实验要求: 实现基础代码; 图形化交互;

其他(可选): 实现NLN直线段裁剪算法

以下代码仅供参考

#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

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

int compute_outcode(int x, int y, int xmin, int xmax, int ymin, int ymax) {
    int code = INSIDE;
    if (x < xmin) {
        code |= LEFT;
    }
    else if (x > xmax) {
        code |= RIGHT;
    }
    if (y < ymin) {
        code |= BOTTOM;
    }
    else if (y > ymax) {
        code |= TOP;
    }
    return code;
}

void cohen_sutherland(int x1, int y1, int x2, int y2, int xmin, int xmax, int ymin, int ymax) {
    int outcode1 = compute_outcode(x1, y1, xmin, xmax, ymin, ymax);
    int outcode2 = compute_outcode(x2, y2, xmin, xmax, ymin, ymax);

    while (1) {
        if (outcode1 == 0 && outcode2 == 0) {
            glBegin(GL_LINES);
            glVertex2i(x1, y1);
            glVertex2i(x2, y2);
            glEnd();
            break;
        }

        if (outcode1 & outcode2) {
            break;
        }

        int x, y;
        int outcode_out = outcode1 ? outcode1 : outcode2;

        if (outcode_out & LEFT) {
            x = xmin;
            y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
        }
        else if (outcode_out & RIGHT) {
            x = xmax;
            y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
        }
        else if (outcode_out & BOTTOM) {
            y = ymin;
            x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
        }
        else if (outcode_out & TOP) {
            y = ymax;
            x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
        }

        if (outcode_out == outcode1) {
            x1 = x;
            y1 = y;
            outcode1 = compute_outcode(x1, y1, xmin, xmax, ymin, ymax);
        }
        else {
            x2 = x;
            y2 = y;
            outcode2 = compute_outcode(x2, y2, xmin, xmax, ymin, ymax);
        }
    }
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);

    int x1 = 100, y1 = 100, x2 = 500, y2 = 400;
    int xmin = 200, xmax = 600, ymin = 200, ymax = 500;

    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_LINE_LOOP);
    glVertex2i(xmin, ymin);
    glVertex2i(xmax, ymin);
    glVertex2i(xmax, ymax);
    glVertex2i(xmin, ymax);
    glEnd();

    glColor3f(1.0, 0.0, 0.0);
    cohen_sutherland(x1, y1, x2, y2, xmin, xmax, ymin, ymax);

    glFlush();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Cohen-Sutherland Algorithm");
    glClearColor(1.0, 1.0, 1.0, 0.0);
    gluOrtho2D(0.0, 800.0, 0.0, 600.0);
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;

使用了OpenGL库来创建窗口,并绘制了一个矩形框和一个经过裁剪的直线段,可以修改坐标来进行其他测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值