当前位置:网站首页>實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
實驗10 Bezier曲線生成-實驗提高-控制點生成B樣條曲線
2022-06-11 21:26:00 【圖形空間】
本代碼根據已知控制點( 10, 5, 0 ),( 5, 10, 0 ),( -5, 15, 0 ),( -10, -5, 0 ),( 4, -4, 0 ),( 10, 5, 0 ), ( 5, 10, 0 ), ( -5, 15, 0 ), ( -10, -5, 0 ),( 10, 5, 0 )來生成三次B樣條曲線。
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct Point
{
float c[2];
float& x = c[0];
float& y = c[1];
Point() {
x = 0, y = 0; }
Point(float x0, float y0) {
x = x0, y = y0; }
Point(const Point& pt) {
x = pt.x, y = pt.y; }
float& operator[](const int i) {
return c[i]; }
};
vector<Point> GctrlPt, GbsCurvePt;
int GnumSegment = 10;
void CalcBSPoints()
{
int numCtrPt = GctrlPt.size();
float F0[2], F1[2], F2[2], F3[2];
float t = 0;
float dt = 1 / (float)GnumSegment;
//第一段NumSubSegment + 1個點,第二段之後每段為NumSubSegment個點,因為第二段之後的起點為前一段的終點
int numCurvePoint = (GnumSegment + 1) + GnumSegment * (numCtrPt - 4);
GbsCurvePt.resize(numCurvePoint);
//第一段起點編號為0,第二段之後的為1,原因見上
for (int i = 0; i < (numCtrPt - 3); i++)
{
for (int j = 0; j < 2; j++)
{
F0[j] = (GctrlPt[i][j] + 4 * GctrlPt[1 + i][j] + GctrlPt[2 + i][j]) / 6.0f;
F1[j] = (-3 * GctrlPt[i][j] + 3 * GctrlPt[2 + i][j]) / 6.0f;
F2[j] = (3 * GctrlPt[i][j] - 6 * GctrlPt[1 + i][j] + 3 * GctrlPt[2 + i][j]) / 6.0f;
F3[j] = (-GctrlPt[i][j] + 3 * GctrlPt[1 + i][j] - 3 * GctrlPt[2 + i][j] + GctrlPt[3 + i][j]) / 6.0f;
}
t = (i == 0) ? 0 : dt;//第一段起點參數為0,第二段之後的為dt,原因同上
for (int j = (i == 0) ? 0 : 1; j < GnumSegment + 1; j++)
{
for (int k = 0; k < 2; k++)
{
GbsCurvePt[j + GnumSegment * i][k] = F0[k] + F1[k] * t + F2[k] * t*t + F3[k] * t*t*t;
}
t += dt;
}
}
}
void ControlPoint(const vector<Point>& vpt)
{
glPointSize(3);
glBegin(GL_POINTS);
for (unsigned int i = 0; i < vpt.size(); i++)
{
glVertex2f(vpt[i].x, vpt[i].y);
}
glEnd();
}
void PolylineGL(const vector<Point>& vpt)
{
glBegin(GL_LINE_STRIP);
for (unsigned int i = 0; i < vpt.size(); i++)
{
glVertex2f(vpt[i].x, vpt[i].y);
}
glEnd();
}
void myDisplay()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
ControlPoint(GctrlPt);
glColor3f(0.0f, 1.0f, 0.0f);
PolylineGL(GctrlPt);
if (GctrlPt.size() >= 4)
{
glColor3f(1.0f, 1.0f, 1.0f);
CalcBSPoints();
PolylineGL(GbsCurvePt);
}
glFlush();
}
void Init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
float points[9][3] = {
{
10, 5, 0 }, {
5, 10, 0 }, {
-5, 15, 0 }, {
-10, -5, 0 }, {
4, -4, 0 },
{
10, 5, 0 }, {
5, 10, 0 }, {
-5, 15, 0 }, {
-10, -5, 0 } };
Point pt;
for (int i = 0; i < 9; i++)
{
Point pt = Point(points[i][0], points[i][1]);
GctrlPt.push_back(pt);
}
}
void myReshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-30.0, 30.0, -30.0*(GLfloat)h / (GLfloat)w, 30.0*(GLfloat)h / (GLfloat)w, 0.0, 100.0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("B-Spline Curve");
Init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}
边栏推荐
- JVM|前言介绍
- Field queryIndexFieldnameService in xxxImpl required a single bean, but 19 were found:
- 【生活思考】文字与语音
- flutter系列之:flutter中常用的container layout详解
- Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
- Codeforces Round #744 (Div. 3) 解题报告
- LeetCode-43-字符串相乘
- 12 golden rules of growth
- Three waves of changes in cloud computing
- AC自动机
猜你喜欢

JVM object allocation policy TLAB

Only 38 years old! Zhou Chuan, a researcher of the Chinese Academy of Sciences, died unfortunately. Rao Yi sent a document to mourn: he guided me when he was still my student
![Analysis on the development history and market development status of China's system integration industry in 2020 [figure]](/img/3c/b53c2a3f59ff6784f128cb98a5a7a2.jpg)
Analysis on the development history and market development status of China's system integration industry in 2020 [figure]
![[game theory complete information static game] strategic game](/img/d2/743e8d14e4fb27cbe883d1df1bca27.jpg)
[game theory complete information static game] strategic game

Syntax of SQL

Goland中在文件模板中为go文件添加个人声明

Database daily question --- day 9: salesperson
![BZOJ3189 : [Coci2011] Slika](/img/46/c3aa54b7b3e7dfba75a7413dfd5b68.png)
BZOJ3189 : [Coci2011] Slika

LeetCode-322-零钱兑换

2021牛客多校5 Double Strings
随机推荐
LeetCode-98-验证二叉搜索树
Obsidian关系图谱如何让节点可以手动拖动
Cs144 lab0 lab1 record
AC automata
RANSAC提取平面(MATLAB内置函数)
Codeforces Round #740 Div. 2 解题报告
Go语言条件语句
JVM|类加载器;双亲委派机制
LabVIEW控制Arduino实现超声波测距(进阶篇—5)
Iros 2021 | new idea of laser vision fusion? Lidar intensity diagram +vpr
一步步把 SAP UI5 应用部署到 SAP BTP Kyma 运行环境中去
One article to show you how to understand the harmonyos application on the shelves
Common file functions
LeetCode-110-平衡二叉树
One article to show you how to understand the harmonyos application on the shelves
LeetCode-43-字符串相乘
Live broadcast with practice | 30 minutes to build WordPress website with Alibaba cloud container service and container network file system
Release of version 5.6 of rainbow, add multiple installation methods, and optimize the topology operation experience
如何将SAP API Hub 上提供的工作流导入到 SAP BTP 上
Tensorflow 2. X Getting Started tutorial