实验6 Bezier曲线生成
1.实验目的:
了解曲线的生成原理,掌握几种常见的曲线生成算法,利用VC+OpenGL实现Bezier曲线生成算法。
2.实验内容:
(1) 结合示范代码了解曲线生成原理与算法实现,尤其是Bezier曲线;
(2) 调试、编译、修改示范程序。
3.实验原理:
Bezier曲线是通过一组多边形折线的顶点来定义的。如果折线的顶点固定不变,则由其定义的Bezier曲线是唯一的。在折线的各顶点中,只有第一点和最后一点在曲线上且作为曲线的起始处和终止处,其他的点用于控制曲线的形状及阶次。曲线的形状趋向于多边形折线的形状,要修改曲线,只要修改折线的各顶点就可以了。因此,多边形折线又称Bezier曲线的控制多边形,其顶点称为控制点。
三次Bezier曲线,有四个控制点,其数学表示如下:
,
4.实验代码:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct Point {
int x, y;
};
Point pt[], bz[];
vector<Point> vpt;
bool bDraw;
int nInput;
void CalcBZPoints()
{
float a0,a1,a2,a3,b0,b1,b2,b3;
a0=pt[].x;
a1=-*pt[].x+*pt[].x;
a2=*pt[].x-*pt[].x+*pt[].x;
a3=-pt[].x+*pt[].x-*pt[].x+pt[].x;
b0=pt[].y;
b1=-*pt[].y+*pt[].y;
b2=*pt[].y-*pt[].y+*pt[].y;
b3=-pt[].y+*pt[].y-*pt[].y+pt[].y;
float t = ;
float dt = 0.01;
for(int i = ; t<1.1; t+=0.1, i++)
{
bz[i].x = a0+a1*t+a2*t*t+a3*t*t*t;
bz[i].y = b0+b1*t+b2*t*t+b3*t*t*t;
}
}
void ControlPoint(vector<Point> vpt)
{
glPointSize();
for(int i=; i<vpt.size(); i++)
{
glBegin (GL_POINTS);
glColor3f (1.0f, 0.0f, 0.0f); glVertex2i (vpt[i].x,vpt[i].y);
glEnd ();
}
}
void PolylineGL(Point *pt, int num)
{
glBegin (GL_LINE_STRIP);
for(int i=;i<num;i++)
{
glColor3f (1.0f, 1.0f, 1.0f);
glVertex2i (pt[i].x,pt[i].y);
}
glEnd ();
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f (1.0f, 1.0f, 1.0f);
if (vpt.size() > ) {
ControlPoint(vpt);
}
if(bDraw)
{
PolylineGL(pt, );
CalcBZPoints();
PolylineGL(bz, );
}
glFlush();
}
void Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
printf("Please Click left button of mouse to input control point of Bezier Curve!\n");
}
void Reshape(int w, int h)
{
glViewport(, , (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
}
void mouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
if (nInput == )
{
pt[].x = x;
pt[].y = - y;
nInput = ;
vpt.clear();
vpt.push_back(pt[]);
bDraw = false;
glutPostRedisplay();//
}
else if (nInput == )
{
pt[].x = x;
pt[].y = - y;
vpt.push_back(pt[]);
nInput = ;
glutPostRedisplay();//
}
else if (nInput == )
{
pt[].x = x;
pt[].y = - y;
vpt.push_back(pt[]);
nInput = ;
glutPostRedisplay();//
}
else if (nInput == )
{
pt[].x = x;
pt[].y = - y;
bDraw = true;
vpt.push_back(pt[]);
nInput = ;
glutPostRedisplay();//
}
}
break;
default:
break;
}
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(, );
glutInitWindowSize(, );
glutCreateWindow("Hello World!");
Init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(Reshape);
glutMouseFunc(mouse);
glutMainLoop();
return ;
}
附上本实验的VC++工程代码(VC++2008)
5.实验提高
尝试实现B样条曲线算法。
实验6 Bezier曲线生成的更多相关文章
- 曲线生成与求交—Bezier曲线
Bezier曲线生成 法国工程师Pierre Bezier在雷诺公司使用该方法来设计汽车.一条Bezier曲线可以拟合任何数目的控制点. 公式 设\(n+1\)个控制点\(P_0,P_1--P_n\) ...
- [摘抄] Bezier曲线、B样条和NURBS
Bezier曲线.B样条和NURBS,NURBS是Non-Uniform Rational B-Splines的缩写,都是根据控制点来生成曲线的,那么他们有什么区别了?简单来说,就是: Bezier曲 ...
- C# 实现Bezier曲线(vs2008)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 7.5.5编程实例-Bezier曲线曲面绘制
(a)Bezier曲线 (b) Bezier曲面 1. 绘制Bezier曲线 #include <GL/glut.h> GLfloat ct ...
- python bezier 曲线
1.手写bezier公式,生成bezier代码, 如果给的点数过多,则会生成一半bezier曲线,剩下的一半就需要进行拼接: import numpy as np import matplotlib. ...
- 曲线生成与求交—B样条曲线
B样条曲线生成 Bezier曲线缺点:改变任一控制点的位置,将影响整条曲线的形状. B样条曲线是对Bezier曲线的改进,可进行局部控制,生成的曲线与控制多边形的外形更接近,将Bezier曲线作为一特 ...
- Bezier曲线的原理 及 二次Bezier曲线的实现
原文地址:http://blog.csdn.net/jimi36/article/details/7792103 Bezier曲线的原理 Bezier曲线是应用于二维图形的曲线.曲线由顶点和控制点组成 ...
- 连续bezier曲线的实现
需求场景 一系列的坐标点,划出一条平滑的曲线 3次Bezier曲线 基本上大部分绘图工具都实现了3次Bezier曲线,4个点确定一条3次Bezier曲线.以html5中的canvas为例 let ct ...
- 简单而粗暴的方法画任意阶数Bezier曲线
简单而粗暴的方法画任意阶数Bezier曲线 虽然说是任意阶数,但是嘞,算法原理是可以到任意阶数,计算机大概到100多阶就会溢出了 Bezier曲线介绍] [本文代码] 背景 在windows的Open ...
随机推荐
- 天翼云 RDS数据库操作
1.RDS数据库创建好之后点击RDS实例管理找到已下信息 官方文档 -1:http://www.ctyun.cn/help/qslist/567 官方文档 -2:http://www.ctyun.cn ...
- PHP统计当前在线用户数实例
HTML 我们在页面上放置一个显示当前在线人数的div#total以及一个用于展示访客地区分布的列表#onlinelist,默认我们在列表中放置一张与加载动画图片,后面我们用jQuery控制当鼠标滑向 ...
- What identity values you get with the @@IDENTITY and SCOPE_IDENTITY functions
--测试表及数据 CREATE TABLE TZ ( Z_id int IDENTITY(1,1)PRIMARY KEY, Z_name varchar(20) NOT NULL) INSE ...
- raize5的修改。
( ( ; Col1: $; Col2: $; Col3: $; Col4: $ ), ( ; Col1: $; Col2: $FA; Col3: $; Col4: $ ), ( ; Col1: $C ...
- uva 11624
#include<stdio.h> #include<string.h> #include<queue> using namespace std; #define ...
- asp.net--webconfg指南
原文链接 花了点时间整理了一下ASP.NET Web.config配置文件的基本使用方法.很适合新手参看,由于Web.config在使用很灵活,可以自定义一些节点.所以这里只介绍一些比较常用的节点. ...
- [bzoj3389][Usaco2004Dec]Cleaning Shifts安排值班_最短路
Cleaning Shifts bzoj-3389 Usaco-2004Dec 题目大意:每天有n个时间段,每个时间段都必须安排一个奶牛值班.有m个奶牛,每个奶牛只有一个空闲时间s[i]~e[i],求 ...
- Spring MVC-表单(Form)标签-文本框(Text Box)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_textbox.htm 说明:示例基于Spring MVC 4.1.6. 以下示例 ...
- C++设计模式之状态模式(二)
2.智能空调的设计与实现 某软件公司将开发一套智能空调系统: 系统检測到温度处于20---30度之间,则切换到常温状态:温度处于30---45度,则切换到制冷状态: 温度小于20度,则切换到制热状态. ...
- #leetcode#Anagrames
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...