当前位置:网站首页>Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
Experiment 10 Bezier curve generation - experiment improvement - interactive generation of B-spline curve
2022-06-12 18:39:00 【Walker 08】
This code generates three times through interaction B Splines . The main function :
- The control point is generated according to the left mouse button click , The control points are generated three times B Splines ;
- Right click to pop up the menu “New B-Spline Curve” Clear current curve , And start a new curve .
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct Point
{
int c[2];
int& x = c[0];
int& y = c[1];
Point() { x = 0, y = 0; }
Point(int x0, int y0) { x = x0, y = y0; }
Point(const Point& pt) { x = pt.x, y = pt.y; }
int& 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];
for (int i = 0; i < 2; i++)
{
F0[i] = (GctrlPt[numCtrPt - 4][i] + 4 * GctrlPt[numCtrPt - 3][i] + GctrlPt[numCtrPt - 2][i]) / 6;
F1[i] = (-3 * GctrlPt[numCtrPt - 4][i] + 3 * GctrlPt[numCtrPt - 2][i]) / 6;
F2[i] = (3 * GctrlPt[numCtrPt - 4][i] - 6 * GctrlPt[numCtrPt - 3][i] + 3 * GctrlPt[numCtrPt - 2][i]) / 6;
F3[i] = (-GctrlPt[numCtrPt-4][i] + 3 * GctrlPt[numCtrPt-3][i] - 3 * GctrlPt[numCtrPt-2][i] + GctrlPt[numCtrPt-1][i]) / 6;
}
float t = 0;
float dt = 1 / (float)GnumSegment;
// The first paragraph NumSubSegment + 1 A little bit , After the second paragraph, each paragraph is NumSubSegment A little bit , Because the starting point after the second paragraph is the end point of the previous paragraph
int numCurvePoint = (GnumSegment + 1) + GnumSegment * (numCtrPt - 4);
GbsCurvePt.resize(numCurvePoint);
int startIdx = GnumSegment * (numCtrPt - 4);
// The starting point number of the first paragraph is 0, After the second paragraph 1, The reasons are as follows
for (int i = (numCtrPt == 4)? 0:1; i < GnumSegment + 1; i++)
{
for (int j = 0; j < 2; j++)
{
GbsCurvePt[i + startIdx][j] = F0[j] + F1[j] * t + F2[j] * t*t + F3[j] * 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++)
{
glVertex2i(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);
printf("Please Click left button of mouse to input control point of B-Spline Curve.\n");
printf("Please Click right button of mouse to new a B-Spline Curve.\n");
}
void myReshape(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 myMouse(int button, int state, int x, int y)
{
Point pt;
int numCtrPt = GctrlPt.size();
switch (button)
{
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN)
{
pt.x = x;
pt.y = 480 - y;
GctrlPt.push_back(pt);
glutPostRedisplay();
}
break;
default:
break;
}
}
void processMenuEvents(int option)
{
switch (option) {
case 1:
GctrlPt.clear();
glutPostRedisplay();
break;
}
}
void createGLUTMenus(){
int menu;
// Create a menu and tell GLUT,processMenuEvents Handle menu events .
menu = glutCreateMenu(processMenuEvents);
// Add items to the menu
glutAddMenuEntry("New B-Spline Curve", 1);
// Associate the menu with the right mouse button .
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
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);
glutMouseFunc(myMouse);
createGLUTMenus();
glutMainLoop();
return 0;
}边栏推荐
- 【sql语句基础】——查(select)(单表查询)
- The difference between user status and system status in CRM
- Go init initialization function
- A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
- Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers
- 2022.6.12-----leetcode.890
- Leetcode topic [string]-541- reverse string II
- How to modify the authorization of sina Weibo for other applications
- 静态内存分配和动态内存分配小结
- C language learning -- data storage in memory
猜你喜欢

Title 54: take 4 ~ 7 bits of an integer a from the right end.

VirtualLab基础实验教程-6.闪耀光栅

Review of MySQL (VI): usage of Union and limit

Machine learning series (3): logistic regression

VirtualLab基础实验教程-4.单缝衍射

Redis(三十二)-用Redis做分布式锁

基于halcon—缺陷检测常用方法与示例总结

Review of MySQL (V): Joint table query and sub query
![Two months later, my second listing anniversary [June 2, 2022]](/img/55/6678659a552ba7dbace330d8b9c3ae.png)
Two months later, my second listing anniversary [June 2, 2022]

"Big fat • small lesson" - talk about big file segmentation and breakpoint sequel
随机推荐
Review of MySQL (V): Joint table query and sub query
JS quick sort
232-CH579M学习开发-以太网例程-TCP服务器(项目应用封装,局域网或广域网测试)
论大型政策性银行贷后,如何数字化转型 ?-亿信华辰
网盘和对象云存储管理之磁盘映射工具比较
Lenovo responded that there are too many and too messy notebooks: it is now the product adjustment period and will be divided into three series of digital /air/ pro in the future
What is SAP support package stack
This shift, I still have to go
yoloe 目标检测使用笔记
Title 68: there are n integers, so that the previous numbers are moved backward m positions, and the last m numbers become the first m numbers
Title 66: input 3 numbers a, B, C, and output them in order of size.
2022.6.12-----leetcode. eight hundred and ninety
Arrays in JS (including leetcode examples) < continuous update ~>
笔记本电脑清灰打硅脂后,开机一直黑屏,如何破?
01-复杂度
To understand Devops, you must read these ten books!
Quickly copy the request in browser F12 to postman/ or generate the corresponding code of the relevant language
从应无所住说起
Gd32f4xx controls dgus touch keys
快速复制浏览器F12中的请求到Postman/或者生成相关语言的对应代码