两点三次Hermiter插值C++代码
2024-08-26 09:48:24
#include <math.h>
#include <gl/glut.h>
#include <iostream>
using namespace std; struct Point2
{
double x;
double y; Point2(int px, int py) { x = px; y = py; }
}; Point2 P0(, );
Point2 P1(, );
Point2 derP0(, );
Point2 derP1(, ); bool mouseLeftDown = false;
bool mouseRightDown = false; /*计算Hermite曲线*/
void Hermit(int n)
{
float f1, f2, f3, f4; double deltaT = 1.0 / n; glBegin(GL_LINE_STRIP);
for (int i = ; i <= n; i++) { double T = i * deltaT; f1 = 2.0*pow(T, ) - 3.0*pow(T, ) + 1.0;
f2 = -2.0*pow(T, ) + 3.0*pow(T, );
f3 = pow(T, ) - 2.0*pow(T, ) + T;
f4 = pow(T, ) - pow(T, ); glVertex2f(f1*P0.x + f2*P1.x + f3*derP0.x + f4*derP1.x,
f1*P0.y + f2*P1.y + f3*derP0.y + f4*derP1.y);
}
glEnd();
} /*用鼠标进行绘制,完成后可改变控制点,拖动即可*/
void display() {
glClear(GL_COLOR_BUFFER_BIT); glLineWidth(1.5);
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_LINES);
glVertex2f(P0.x, P0.y);
glVertex2f(P0.x + derP0.x / , P0.y + derP0.y / );
glVertex2f(P1.x, P1.y);
glVertex2f(P1.x - derP1.x / , P1.y - derP1.y / );
glEnd(); glColor3f(0.0, 0.0, 1.0);
glPointSize(10.0f); glBegin(GL_POINTS);
glVertex2f(P0.x, P0.y);
glVertex2f(P0.x + derP0.x / , P0.y + derP0.y / );
glVertex2f(P1.x, P1.y);
glVertex2f(P1.x - derP1.x / , P1.y - derP1.y / );
glEnd(); Hermit(); glFlush();
glutSwapBuffers();
} void init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
} void myReshape(int w, int h)
{
glViewport(, , (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLsizei)w, (GLsizei)h, 0.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
} void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
mouseLeftDown = true;
} if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
mouseLeftDown = false;
} if (button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
{
mouseRightDown = true;
} if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP)
{
mouseRightDown = false;
}
} double distance(int x1, int y1, int x2, int y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
} void motion(int x, int y)
{
if (mouseLeftDown)
{
if (distance(P0.x + derP0.x / , P0.y + derP0.y / , x, y) < )
{
derP0.x = (x - P0.x) * ;
derP0.y = (y - P0.y) * ;
} if (distance(P1.x - derP1.x / , P1.y - derP1.y / , x, y) < )
{
derP1.x = (P1.x - x) * ;
derP1.y = (P1.y - y) * ;
}
} glutPostRedisplay();
} int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(, );
glutInitWindowPosition(, );
glutCreateWindow("hello");
init(); glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutMouseFunc(mouse);
glutMotionFunc(motion); glutMainLoop();
return ;
}
最新文章
- 一步步学习javascript基础篇(6):函数表达式之【闭包】
- Intellij IDEA 13.1.3 创建Java Web项目
- Emacs学习心得之 基础操作
- iOS开发——网络编程Swift篇&;Alamofire详解
- iOS App创建桌面快捷方式
- SCOM2007R2安装和报表服务器配置
- Ehcache(2.9.x) - API Developer Guide, Class Loading
- rsync使用说明
- null的小扩展
- MySQL推出Applier,可实时复制数据到Hadoop
- 脉冲神经网络Spiking neural network
- Ninject的项目情况
- Spring中一个类的注入和引用是不一样的
- 一篇文章让你搞懂 SSL 证书
- [BZOJ 4916]神犇和蒟蒻
- PLSQL创建Oracle定时任务
- Spring Boot学习记录02_构建SpringBoot工程_通过idea构建
- Thinking in Java from Chapter 11
- PL/SQL学习笔记之数据类型中的标量、LOB
- Intellij IDEA 热部署处理