OpenGL直线剪裁
来源:互联网 发布:windows live影音 编辑:程序博客网 时间:2024/05/22 04:28
这次是用MFC+OpenGL写的,代码没法全部贴上,上传到下载栏目里好了。点击打开下载链接(现在好像不能上传0积分下载的东西了QAQ)
运行效果:
核心代码(剪裁的核心代码):
int GraphyCut::getCode(const Point a) //得到编码{ int code = 0; if (a.y > max(st.y, endd.y)) code |= 8; if (a.y < min(st.y, endd.y)) code |= 4; if (a.x > max(st.x, endd.x)) code |= 2; if (a.x < min(st.x, endd.x)) code |= 1; return code;}bool GraphyCut::isLinesAcross(const Line a, const Line b) //跨立试验{ int num1 = (a.b.x - a.a.x)*(b.a.y - a.a.y) - (a.b.y - a.a.y)*(b.a.x - a.a.x); int num2 = (a.b.x - a.a.x)*(b.b.y-a.a.y) - (a.b.y - a.a.y)*(b.b.x - a.a.x); if (num1/abs(num1)*num2 <= 0) //这里不能用乘法计算,因为可能出现溢出(难查的bug) return true; return false;}bool GraphyCut::isRemain(Line &line){ int code1 = getCode(line.a); int code2 = getCode(line.b); if (code1 == 0 && code2 == 0) //在中间,保留 return true; else if (code1&code2) //边界外,舍去 return false; else if (!(isLinesAcross(line, Line(st, endd)) || isLinesAcross(line, Line(Point(st.x, endd.y), Point(endd.x, st.y))))) { //进行跨立试验,如果不通过则表示在框外 return false; } else { if (fabs(line.a.x - line.b.x) <= 2) { if (line.a.y > max(st.y, endd.y)) line.a.y = max(st.y, endd.y); else if (line.a.y < min(st.y, endd.y)) line.a.y = min(st.y, endd.y); if (line.b.y > max(st.y, endd.y)) line.b.y = max(st.y, endd.y); else if (line.b.y < min(st.y, endd.y)) line.b.y = min(st.y, endd.y); return true; } do { if (code1 == 0 && code2 == 0) break; int code = code1 == 0 ? code2 : code1; double x, y; if (code&1) { y = line.a.y + (line.b.y - line.a.y)*(min(st.x,endd.x) - line.a.x) / (line.b.x - line.a.x); x = (double)min(st.x,endd.x); } else if (code&2) { y = line.a.y + (line.b.y - line.a.y)*(max(st.x,endd.x) - line.a.x) / (line.b.x - line.a.x); x = (double)max(st.x,endd.x); } else if (code&4) { x = line.a.x + (line.b.x - line.a.x)*(min(st.y,endd.y) - line.a.y) / (line.b.y - line.a.y); y = (double)min(st.y,endd.y); } else if (code&8){ x = line.a.x + (line.b.x - line.a.x)*(max(st.y,endd.y) - line.a.y) / (line.b.y - line.a.y); y = (double)max(st.y,endd.y); } if (code == code1) { line.a.x = x; line.a.y = y; code1 = getCode(line.a); } else { line.b.x = x; line.b.y = y; code2 = getCode(line.b); } } while (true); return true; }}void GraphyCut::CutLines(){ for (int i = 0; i < lines.size(); i++) { if (!isRemain(lines[i])) { lines.erase(lines.begin() + i); i--; } }}
阅读全文
0 0
- OpenGL直线剪裁
- 直线剪裁:
- openGL剪裁的使用
- OpenGL ES 剪裁
- OpenGL直线拾取
- opengl Bresenham直线算法
- OpenGL绘制直线
- OpenGL编码剪裁算法Cohen-Surtherland算法
- c++实现Cohen-Sutherlan算法(直线段剪裁)
- OpenGL 画直线 WIN7 VS2012
- OPENGL—DDA画直线
- OpenGL中剪裁平面与模型视图变换的关系
- OpenGL学习三十四:剪裁区域和TGA图像文件的加载
- 基于Weiler-Atherton剪裁布尔运算openGL实现-2D
- OpenGL ES 2.0 - 几种剪裁与测试
- 用opengl实现直线的裁剪
- OpenGL写的直线裁剪法
- 第一课、OpenGL绘制直线等等
- 平安一账通面试
- Vs2015 tesseract 3.05 编译x86 dll 调用 身份证数字识别
- Fragment切换 hide/show 生命周期的运用
- StackNavigator的简单使用
- JavaMail发送文本邮件换行
- OpenGL直线剪裁
- BING代码:BingObjectnessCVPR14在VS2013+win64+opencv2.4.12位下的运行方法
- Java提高篇——对象克隆(复制)
- C# ASP.NET 控制Windows服务的开启、关闭、重启相关参考资料
- 区块链架构设计和知识图谱
- ceshifu
- 数据库字段重复(Duplicate column name 'ORGID')
- Android屏幕适配全攻略(2)
- 备份数据库中表的方法