使用C++语言
裁剪线段P1(3,3),P2(-2,-1),裁剪窗口为wxl=0,wxr=2,wyb=0,wyt=2。
// 梁友栋-barsky算法.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <GL/glut.h>
#include <iostream>
#include <cmath>
#include <algorithm>
double wxl = 0, wxr = 2, wyb = 0, wyt = 2;
void clipLine(double x1, double y1, double x2, double y2) {
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
// 定义线段P1(3,3)到P2(-2,-1)
double x1 = 3, y1 = 3;
double x2 = -2, y2 = -1;
// 裁剪线段
clipLine(x1, y1, x2, y2);
glFlush();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutCreateWindow("Liang-Barsky Line Clipping");
glClearColor(0.0, 0.0, 0.0, 1.0);
gluOrtho2D(-5, 5, -5, 5);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}