【计算机图形学】c++ OpenGL Sutherlang-Hodgman 多边形裁剪
源代码
// 多边形裁剪.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"
#include<GL/glut.h>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>//Sutherlang-Hodgman多边形裁剪
typedef struct
{float x, y;
}wcPt2D;typedef enum
{Left, Right, Bottom, Top
}Boundary;int Inside(wcPt2D p, Boundary b, wcPt2D wMin, wcPt2D wMax)
{switch (b){case Left:if (p.x<wMin.x) return (false);break;case Right:if (p.x>wMax.x) return (false);break;case Bottom:if (p.y<wMin.y) return (false);break;case Top:if (p.y>wMax.y) return (false);break;}return true;
}int Cross(wcPt2D p1, wcPt2D p2, Boundary b, wcPt2D wMin, wcPt2D wMax)
{if (Inside(p1, b, wMin, wMax) == Inside(p2, b, wMin, wMax))return (false);elsereturn true;
}wcPt2D Intersect(wcPt2D p1, wcPt2D p2, Boundary b, wcPt2D wMin, wcPt2D wMax)
{wcPt2D iPt;float m;if (p1.x != p2.x) m = (p2.y - p1.y) / (p2.x - p1.x);switch (b){case Left:iPt.x = wMin.x;iPt.y = p2.y + (wMin.x - p2.x)*m;break;case Right:iPt.x = wMax.x;iPt.y = p2.y + (wMax.x - p2.x)*m;break;case Bottom:iPt.y = wMin.y;if (p1.x != p2.x)iPt.x = p2.x + (wMin.y - p2.y) / m;else iPt.x = p2.x;break;case Top:iPt.y = wMax.y;if (p1.x != p2.x) iPt.x = p2.x + (wMax.y - p2.y) / m;else iPt.x = p2.x;break;}return iPt;
}
int edgeCliper(Boundary b, wcPt2D wMin, wcPt2D wMax, wcPt2D *pIn, int cnt, wcPt2D *pOut)
{wcPt2D s;int i, Outcnt = 0;s = pIn[0];for (i = 1; i <= cnt; i++){if (!Inside(s, b, wMin, wMax) && Inside(pIn[i], b, wMin, wMax)){pOut[Outcnt] = Intersect(s, pIn[i], b, wMin, wMax);Outcnt++;pOut[Outcnt] = pIn[i];Outcnt++;}else if (Inside(s, b, wMin, wMax) && Inside(pIn[i], b, wMin, wMax)){pOut[Outcnt] = pIn[i];Outcnt++;}else if (Inside(s, b, wMin, wMax) && (!Inside(pIn[i], b, wMin, wMax))){pOut[Outcnt] = Intersect(s, pIn[i], b, wMin, wMax);Outcnt++;}s = pIn[i];}return (Outcnt);
}void init(void)
{glClearColor(1.0, 1.0, 1.0, 0.0);glMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(-400.0, 400.0, -300.0, 300.0);
}void ClipPolygonSuthHodg(void)
{glClear(GL_COLOR_BUFFER_BIT);glMatrixMode(GL_MODELVIEW);int i, cnt, Outcnt, b;wcPt2D points[7] = { { -300, -100 },{ -300, 150 }, { -100, 150 }, { 100, 100 }, { 100, 0 }, { 0, -100 },{ -300, -100 }};cnt = 7;wcPt2D pOut[28], pIn[28];wcPt2D wMin = { -200, 0 }, wMax = { 50, 200 };for (i = 0; i<4 * cnt; i++){pIn[i].x = 0.0;pIn[i].y = 0.0;pOut[i].x = 0.0;pOut[i].y = 0.0;}for (i = 0; i <= cnt; i++) pIn[i] = points[i];glColor3f(0.0, 0.0, 0.0);glBegin(GL_LINE_LOOP);glVertex2f(wMin.x, wMin.y);glVertex2f(wMax.x, wMin.y);glVertex2f(wMax.x, wMax.y);glVertex2f(wMin.x, wMax.y);glEnd();glLineWidth(2.0);glBegin(GL_LINE_LOOP);for (i = 0; i<cnt; i++)glVertex2f(pIn[i].x, pIn[i].y);glEnd();for (b = 0; b<4; b++){Outcnt = edgeCliper(Boundary(b), wMin, wMax, pIn, cnt, pOut);for (i = 0; i<Outcnt; i++)pIn[i] = pOut[i];pIn[Outcnt] = pOut[0];cnt = Outcnt;}glColor3f(1.0, 0.0, 0.0);glBegin(GL_LINE_LOOP);for (i = 0; i<cnt; i++)glVertex2f(pOut[i].x, pOut[i].y);glEnd();glFlush();
}
void main(int argc, char **argv)
{glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowPosition(50, 100);glutInitWindowSize(800, 600);glutCreateWindow("An Example OpenGL Program");init();glutDisplayFunc(ClipPolygonSuthHodg);glutMainLoop();
}
运行结果
【计算机图形学】c++ OpenGL Sutherlang-Hodgman 多边形裁剪相关推荐
- 平移计算机图形学代码注释,求代码注释:计算机图形学的OpenGL画四面体。高手来吧。争取每句都注释下。谢谢...
求代码注释:计算机图形学的OpenGL画四面体.高手来吧.争取每句都注释下.谢谢 答案:3 信息版本:手机版 解决时间 2018-12-20 05:00 已解决 2018-12-20 01:58 求 ...
- 计算机图形学及OpenGL简介摘记
计算机图形学(Computer Graphics)是研究怎样用计算机表示.生成.处理和显示图形的一门学科.图形的具体应用范围很广,但是按基本的处理技术划分只有两类.一类是线条,如工程图.地图.曲线图表 ...
- 计算机图形学E1——OpenGL 方中有圆,圆中有方,无穷尽焉
其他计算机图形学实验见 链接 使用OpenGL绘制如图所示图形 有穷的: #include <GL/glut.h> #include <math.h> #include< ...
- 【计算机图形学】OpenGL递归实现光线追踪
计算机图形学课程设计:基于面向对象的光线跟踪算法设计与实现 目录 一.前言 二.项目实现与说明 1. 数据结构设计 1.1 光线 Ray 1.2 材质 Material 1.3 光照 Light 1. ...
- 计算机图形学入门OpenGL
计算机图形学openGL 计算机图形学:(Computer Graphics, 简称CG) 简单得说:计算机图形学是一种使用数学算法将二维或者三维图形转化为计算机显示器所能显示的二维栅格形式的科学. ...
- 【学习日志】2022.08.19 计算机图形学、OpenGL、疼迅云游戏引擎面试、GLAD、GLFW、virtual
计算机图形学知识图谱 .学习路线 OpenGL 概述 简介 - LearnOpenGL CN (learnopengl-cn.github.io) 当前比较流行的搭配是 freeglut + glew ...
- 计算机图形学中OpenGL和DirectX
引言 接触计算机图形学,一定听说过两大编程接口:OpenGL和DirectX,两者之间不断竞争,也不断促进着各自的发展.API大战真的很有意思,一起来看看这两大BOSS究竟是什么,实力如何? Open ...
- 计算机图形学E3——OpenGL 中点画圆
其他计算机图形学实验见 链接 #include<iostream> #include<GL/glut.h> using namespace std;float bx, by, ...
- 计算机图形学E7——OpenGL 交互式三角形裁剪直线
其他计算机图形学实验见 链接 #include<gl/glut.h> #include<iostream> #include<algorithm> #include ...
- 计算机图形学E5——OpenGL 扫描线填充
其他计算机图形学实验见 链接 #include<iostream> #include<gl/glut.h> #include<algorithm> #include ...
最新文章
- 那些对程序员来说伤害性不高但是侮辱性极强的瞬间…
- 【以太坊】javascript控制台完整交易流程
- JS、CSS中的相对路径
- C++ 简单实现基数排序(list容器)
- 华为荣耀v20是android10,荣耀V20和荣耀V10买哪个好
- linux之expect
- ES6新特性_ES6的数值扩展---JavaScript_ECMAScript_ES6-ES11新特性工作笔记039
- android Fragment 笔记
- Web设计里的软件工程思想
- java归并算法实例_Java编程中实现归并排序算法的实例教程
- wxwindows编译
- 使用express 代理图片下载
- 计算机游戏程序启动错误及其解决方法汇总
- 虚拟搭建局域网模拟器_如何建立虚拟局域网
- lda指令是什么意思_lda指令什么意思
- Maven实战读书笔记1
- vue实现鼠标拖拽div滚动效果-vue-dragscroll(整理)
- 【云速建站】微信公众平台中维护IP白名单
- PaddleHub创意之艺术风格迁移
- 湖州云计算机,湖州云存储服务_菲尔云计算