#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 ;
}

最新文章

  1. 一步步学习javascript基础篇(6):函数表达式之【闭包】
  2. Intellij IDEA 13.1.3 创建Java Web项目
  3. Emacs学习心得之 基础操作
  4. iOS开发——网络编程Swift篇&amp;Alamofire详解
  5. iOS App创建桌面快捷方式
  6. SCOM2007R2安装和报表服务器配置
  7. Ehcache(2.9.x) - API Developer Guide, Class Loading
  8. rsync使用说明
  9. null的小扩展
  10. MySQL推出Applier,可实时复制数据到Hadoop
  11. 脉冲神经网络Spiking neural network
  12. Ninject的项目情况
  13. Spring中一个类的注入和引用是不一样的
  14. 一篇文章让你搞懂 SSL 证书
  15. [BZOJ 4916]神犇和蒟蒻
  16. PLSQL创建Oracle定时任务
  17. Spring Boot学习记录02_构建SpringBoot工程_通过idea构建
  18. Thinking in Java from Chapter 11
  19. PL/SQL学习笔记之数据类型中的标量、LOB
  20. Intellij IDEA 热部署处理

热门文章

  1. Python之 time 模块
  2. 一分钟 - 创建python虚拟环境
  3. Pandas的基础操作(一)——矩阵表的创建及其属性
  4. LeetCode第152场周赛(Java)
  5. xorm -Get方法实例
  6. tidb测试环境搭建
  7. Consul 的安装与基本使用
  8. jdbcUrl is required with driverClassName
  9. C# vb .net实现羽化效果
  10. 【图解】cpu,内存,硬盘,指令的关系