Sutherland-Hodgman裁剪算法(附c++源码)

2023-12-25 01:32

本文主要是介绍Sutherland-Hodgman裁剪算法(附c++源码),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

这是一个关于多边形的裁剪算法,剪裁的是多边形,关于直线的剪裁,博主在https://blog.csdn.net/sinat_33916407/article/details/88918553 里面给出了梁友栋-Barskey裁剪算法的实现,在这里,博主给出的是多边形剪裁方法中的Sutherland-Hodgman方法,亲测可用,按下c键进行剪裁,按下r键会复原原图,按下x键会退出。

开发环境:win10 + opengl + visual studio2017

关于visual studio2017配置opengl是项目---->管理NuGet程序包--->输入nupengl,可以看见两个相应的包,下载即可。

附上代码:

//本项目是计算机图形学中的Sutherland-Hodgman算法

//本算法的思想是从边框xl处开始,对所有线进行判断,更新新的顶点表,循环直到最后一个边界线判断结束,得到最后的顶点表

 

#include "pch.h"

#include <iostream>

#include<gl/glut.h>

#include<vector>

using namespace std;

 

//此处定义出了边框的边界

#define XL 100

#define XR 400

#define YL 100

#define YR 400

 

 

//定义点结构

struct Points

{

float x;

float y;

};

 

//定义边框

struct Border

{

float xl;

float xr;

float yl;

float yr;

};

 

//多边形的几个顶点

vector<Points> p = { {200,450},{450,300},{300,50},{50,200} };

Border rectangles = { XL,XR,YL,YR };

 

//画直线

void LineGL(Points &p1, Points &p2) {

glBegin(GL_LINES);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex2f(p1.x, p1.y);

glColor3f(0.0f, 1.0f, 0.0f);

glVertex2f(p2.x, p2.y);

glEnd();

}

//计算线与边界x的交点坐标

Points interactionx(float &borde,Points &x,Points &y) {

float k, b;

Points result;

k = (y.y - x.y) / (y.x - x.x);

b = (y.x*x.y - x.x*y.y) / (y.x - x.x);

float yy = k * borde + b;

result.x = borde;

result.y = yy;

 

return result;

}

//计算线与边界y的交点坐标

Points interactiony(float &borde, Points &x, Points &y) {

float k, b;

Points result;

k = (y.y - x.y) / (y.x - x.x);

b = (y.x*x.y - x.x*y.y) / (y.x - x.x);

float xx = (borde - b) / k;

result.y = borde;

result.x = xx;

 

return result;

}

 

//核心算法 每条边按照一定的顺序进行相应的判断,然后更新这个新的含points的表,将最后的表再进行画出来

vector<Points> SutherlandHodgman(Border &border, vector<Points> &pp) {//输入边界和要剪切的图形

vector<Points> r1, r;

//判断 从最左边的XL开始

for (int i = 0; i < pp.size(); i++) {

//判断 由于一条线段是由两个点组成,所以相邻的两个点自发形成一条线段,最后的一个点与第一个点形成线段

if (i == pp.size() - 1) {//最后一个点与第一个点形成一条线段,单独先处理出来

int x1 = pp[i].x - border.xl;

int x2 = pp[0].x - border.xl;

if (x1 < 0 && x2 < 0) {

 

}

else if (x1 < 0 && x2 >= 0) {

Points poin;

if (x2 == 0) {

r1.push_back(pp[0]);

}

else {

Points px = interactionx(border.xl, pp[i], pp[0]);

r1.push_back(px);

r1.push_back(pp[0]);

}

}

else if (x1 >= 0 && x2 < 0) {

if (x1 == 0) {

r1.push_back(pp[i]);

}

else

{

Points px = interactionx(border.xl, pp[i], pp[0]);

r1.push_back(px);

}

}

else if (x1 >= 0 && x2 >= 0)

{

r1.push_back(pp[0]);

}

}

else {//其余的线段正常处理

int x1 = pp[i].x - border.xl;

int x2 = pp[i+1].x - border.xl;

//处理四种情况

//第一种情况:两个点都在边界之外

if (x1 < 0 && x2 < 0) {

 

}

//第二种情况:第一个点在边界外,第二个点在边界内或边界上

else if (x1 < 0 && x2 >= 0) {

if (x2 == 0) {

r1.push_back(pp[i + 1]);

}

else {

Points px = interactionx(border.xl, pp[i], pp[i + 1]);

r1.push_back(px);

r1.push_back(pp[i + 1]);

}

}

//第三种情况:第一个点在边界上或边界内,第二个点在边界外

else if (x1 >= 0 && x2 < 0) {

if (x1 == 0) {

r1.push_back(pp[i]);

}

else

{

Points px = interactionx(border.xl, pp[i], pp[i + 1]);

r1.push_back(px);

}

}

//第四种情况:两个点都在边界内或者边界上

else if(x1>=0 && x2>=0)

{

r1.push_back(pp[i + 1]);

}

}

}

r.assign(r1.begin(), r1.end());

r1.clear();

 

//最上边yr

for (int i = 0; i < r.size(); i++) {

if (i == r.size() - 1) {

int x1 = border.yr - r[i].y;

int x2 = border.yr - r[0].y;

if (x1 < 0 && x2 < 0) {

 

}

else if (x1 < 0 && x2 >= 0) {

if (x2 == 0) {

r1.push_back(r[0]);

}

else {

Points px = interactiony(border.yr, r[i], r[0]);

r1.push_back(px);

r1.push_back(r[0]);

}

}

else if (x1 >= 0 && x2 < 0) {

if (x1 == 0) {

r1.push_back(r[i]);

}

else

{

Points px = interactiony(border.yr, r[i], r[0]);

r1.push_back(px);

}

}

else if (x1 >= 0 && x2 >= 0)

{

r1.push_back(r[0]);

}

}

else {

int x1 = border.yr - r[i].y;

int x2 = border.yr - r[i+1].y;

if (x1 < 0 && x2 < 0) {

 

}

else if (x1 < 0 && x2 >= 0) {

Points poin;

if (x2 == 0) {

r1.push_back(r[i + 1]);

}

else {

Points px = interactiony(border.yr, r[i], r[i + 1]);

r1.push_back(px);

r1.push_back(r[i + 1]);

}

}

else if (x1 >= 0 && x2 < 0) {

if (x1 == 0) {

r1.push_back(r[i]);

}

else

{

Points px = interactiony(border.yr, r[i], r[i + 1]);

r1.push_back(px);

}

}

else if (x1 >= 0 && x2 >= 0)

{

r1.push_back(r[i + 1]);

}

}

}

r.clear();

r.assign(r1.begin(), r1.end());

r1.clear();

 

 

//最右边xr

for (int i = 0; i < r.size(); i++) {

if (i == r.size() - 1) {

int x1 = border.xr - r[i].x;

int x2 = border.xr - r[0].x;

if (x1 < 0 && x2 < 0) {

 

}

else if (x1 < 0 && x2 >= 0) {

Points poin;

if (x2 == 0) {

r1.push_back(r[0]);

}

else {

Points px = interactionx(border.xr, r[i], r[0]);

r1.push_back(px);

r1.push_back(r[0]);

}

}

else if (x1 >= 0 && x2 < 0) {

if (x1 == 0) {

r1.push_back(r[i]);

}

else

{

Points px = interactionx(border.xr, r[i], r[0]);

r1.push_back(px);

}

}

else if (x1 >= 0 && x2 >= 0)

{

r1.push_back(r[0]);

}

}

else {

int x1 = border.xr - r[i].x;

int x2 = border.xr - r[i + 1].x;

if (x1 < 0 && x2 < 0) {

 

}

else if (x1 < 0 && x2 >= 0) {

if (x2 == 0) {

r1.push_back(r[i + 1]);

}

else {

Points px = interactionx(border.xr, r[i], r[i + 1]);

r1.push_back(px);

r1.push_back(r[i + 1]);

}

}

else if (x1 >= 0 && x2 < 0) {

if (x1 == 0) {

r1.push_back(r[i]);

}

else

{

Points px = interactionx(border.xr, r[i], r[i + 1]);

r1.push_back(px);

}

}

else if (x1 >= 0 && x2 >= 0)

{

r1.push_back(r[i + 1]);

}

}

}

r.clear();

r.assign(r1.begin(), r1.end());

r1.clear();

 

//最下边yl

for (int i = 0; i < r.size(); i++) {

if (i == r.size() - 1) {

int x1 = r[i].y - border.yl;

int x2 = r[0].y - border.yl;

if (x1 < 0 && x2 < 0) {

 

}

else if (x1 < 0 && x2 >= 0) {

Points poin;

if (x2 == 0) {

r1.push_back(r[0]);

}

else {

Points px = interactiony(border.yl, r[i], r[0]);

r1.push_back(px);

r1.push_back(r[0]);

}

}

else if (x1 >= 0 && x2 < 0) {

if (x1 == 0) {

r1.push_back(r[i]);

}

else

{

Points px = interactiony(border.yl, r[i], r[0]);

r1.push_back(px);

}

}

else if (x1 >= 0 && x2 >= 0)

{

r1.push_back(r[0]);

}

}

else {

int x1 = r[i].y - border.yl;

int x2 = r[i+1].y - border.yl;

if (x1 < 0 && x2 < 0) {

 

}

else if (x1 < 0 && x2 >= 0) {

Points poin;

if (x2 == 0) {

r1.push_back(r[i + 1]);

}

else {

Points px = interactiony(border.yl, r[i], r[i + 1]);

r1.push_back(px);

r1.push_back(r[i + 1]);

}

}

else if (x1 >= 0 && x2 < 0) {

if (x1 == 0) {

r1.push_back(r[i]);

}

else

{

Points px = interactiony(border.yl, r[i], r[i + 1]);

r1.push_back(px);

}

}

else if (x1 >= 0 && x2 >= 0)

{

r1.push_back(r[i + 1]);

}

}

}

r.clear();

r.assign(r1.begin(), r1.end());

return r;

}

 

 

 

 

//初始化整个界面

void Init() {

glClearColor(0.0, 0.0, 0.0, 0.0);

glShadeModel(GL_FLAT);

//按下c键会进行剪裁,按下r键会复原,按下x键会退出窗口

printf("Press key 'c' to Clip!\nPress key 'r' to Recover\nPress key 'x' to Exit\n");

}

 

//窗口大小的改变函数

void Reshape(int w, int h) {

glViewport(0, 0, (GLsizei)w, (GLsizei)h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);

}

 

//未剪裁前的显示

void display() {

glClear(GL_COLOR_BUFFER_BIT);

glColor3f(1.0f, 0.0f, 0.0f);

glPointSize(2);

//画矩形 使用的是红色

glBegin(GL_LINE_LOOP);

glVertex2f(XL, YL);

glVertex2f(XR, YL);

glVertex2f(XR, YR);

glVertex2f(XL, YR);

glEnd();

 

//画出最先开始的直线,没有经过任何修改

for (int i = 0; i < p.size(); i++) {

if (i == p.size() - 1) {

LineGL(p[p.size() - 1], p[0]);

}

else {

LineGL(p[i], p[i + 1]);

}

}

glFlush();

}

 

//键盘监听事件

void keyboard(unsigned char key, int x, int y) {

switch (key) {

case 'c':

//按下c键以后,会进行裁剪工作

p = SutherlandHodgman(rectangles, p);//调用函数得到最新的点

glutPostRedisplay();//当前窗口重新绘制

break;

case 'r':

//按下r键以后,原来的直线恢复,出现原本的线

p = { {200,450},{450,300},{300,50},{50,200} };

Init();

glutPostRedisplay();

break;

case 'x'://退出

exit(0);

break;

default:

break;

}

}

 

 

int main(int argc, char* argv[])

{

glutInit(&argc, argv);//从main函数传递参数

//设置显示模式,单缓冲,RGB

glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);

glutInitWindowPosition(400, 200);//窗口位置

glutInitWindowSize(500, 500);//窗口大小

glutCreateWindow("Sutherland_Hodgman");//窗口标题

 

Init();//初始化

 

glutDisplayFunc(&display);//渲染函数

glutReshapeFunc(Reshape); //窗口函数发生改变时调用函数

glutKeyboardFunc(&keyboard);//调用键盘监听事件

glutMainLoop();//循环消息机制

 

return 0;

}

 

 

 

 

关于算法的原理,一部分在博主给出的代码可看懂,另一部分给出一个博主当时看的博客https://blog.csdn.net/damotiansheng/article/details/43274183 其余的也可以自己寻找。如果有什么问题可以给博主留言,博主会尽快回复!

 

这篇关于Sutherland-Hodgman裁剪算法(附c++源码)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/533835

相关文章

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

C++ scoped_ptr 和 unique_ptr对比分析

《C++scoped_ptr和unique_ptr对比分析》本文介绍了C++中的`scoped_ptr`和`unique_ptr`,详细比较了它们的特性、使用场景以及现代C++推荐的使用`uni... 目录1. scoped_ptr基本特性主要特点2. unique_ptr基本用法3. 主要区别对比4. u

C++11中的包装器实战案例

《C++11中的包装器实战案例》本文给大家介绍C++11中的包装器实战案例,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录引言1.std::function1.1.什么是std::function1.2.核心用法1.2.1.包装普通函数1.2.

C++多线程开发环境配置方法

《C++多线程开发环境配置方法》文章详细介绍了如何在Windows上安装MinGW-w64和VSCode,并配置环境变量和编译任务,使用VSCode创建一个C++多线程测试项目,并通过配置tasks.... 目录下载安装 MinGW-w64下载安装VS code创建测试项目配置编译任务创建 tasks.js

C++ 多态性实战之何时使用 virtual 和 override的问题解析

《C++多态性实战之何时使用virtual和override的问题解析》在面向对象编程中,多态是一个核心概念,很多开发者在遇到override编译错误时,不清楚是否需要将基类函数声明为virt... 目录C++ 多态性实战:何时使用 virtual 和 override?引言问题场景判断是否需要多态的三个关

C++简单日志系统实现代码示例

《C++简单日志系统实现代码示例》日志系统是成熟软件中的一个重要组成部分,其记录软件的使用和运行行为,方便事后进行故障分析、数据统计等,:本文主要介绍C++简单日志系统实现的相关资料,文中通过代码... 目录前言Util.hppLevel.hppLogMsg.hppFormat.hppSink.hppBuf