抛物样条曲线

最近写了很多的图形学方面的程序,给大家分享下。

源代码如下,需要opengl库函数的支持。

#include <GL/glut.h>
#include<Windows.h>

GLfloat bc=0.0001f;
int sign=0;
void drawparabola(GLfloat point[10][2],GLint num)
{
     GLfloat px,py;
     GLint i=0;
     for(i=0;i<num-2;i++)
     {   GLfloat t,tempt;
         if(i==0||i==num-3)
         {
             if(sign==0)
             {   tempt=0.0f;sign=1;}
             else
             {   tempt=0.5f;sign=0;}
            
             for(t=tempt;t<(tempt+0.5f);)
             {
                  px=(2*t*t-3*t+1)*point[i][0]+(4*t-4*t*t)*point[i+1][0]+(2*t*t-t)*point[i+2][0];
                  py=(2*t*t-3*t+1)*point[i][1]+(4*t-4*t*t)*point[i+1][1]+(2*t*t-t)*point[i+2][1];
                  t=t+bc;
                  glColor3f(1.0,1.0,0.0);
                  glBegin(GL_POINTS);
                     glVertex2f(px,py);
                  glEnd();
             }
         }
     }
     for(i=0;i<num-3;i++)
     {   GLfloat t;

         for(t=0.0f;t<=0.5f;)
         {
              px=(-4*t*t*t+4*t*t-t)*point[i][0]+(12*t*t*t-10*t*t+1)*point[i+1][0]+(-12*t*t*t+8*t*t+t)*point[i+2][0]+(4*t*t*t-2*t*t)*point[i+3][0];
              py=(-4*t*t*t+4*t*t-t)*point[i][1]+(12*t*t*t-10*t*t+1)*point[i+1][1]+(-12*t*t*t+8*t*t+t)*point[i+2][1]+(4*t*t*t-2*t*t)*point[i+3][1];
              t=t+bc;
              glColor3f(1.0,0.0,0.0);
              glBegin(GL_POINTS);
                 glVertex2f(px,py);
              glEnd();
         }
     }
}


void display()
{
     GLfloat point[7][2]={{-0.7f,-0.2f},{-0.5f,0.6f},{-0.2f,-0.2f},{0.4f,0.6f},{0.6f,-0.2f},{0.8f,0.6f},{1.0,-0.2f}};
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0,0.0,0.0);//设置当前颜色状态为红色
     drawparabola(point,7);
     glFlush();//发送缓冲区
    
}
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("抛物样条曲线");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,作为AI语言模型,我无法生成图像。但是,我可以提供以下C语言代码,其中使用了线样条曲线法来绘制一个猫头: ``` #include <graphics.h> #include <math.h> void drawCatHead() { int gd = DETECT, gm; initgraph(&gd, &gm, ""); float x[10] = {0, 50, 100, 150, 200, 250, 300, 350, 400, 450}; float y[10] = {100, 50, 25, 10, 0, 10, 25, 50, 100, 150}; float h[10], b[10], u[10], v[10], z[10]; float a[10], c[10], d[10], l[10], m[10], p[10], q[10], r[10], s[10], t[10], f[10], g[10], j[10], k[10]; // initialize values of h, b, and u for (int i = 1; i < 9; i++) { h[i] = x[i] - x[i - 1]; b[i] = (y[i] - y[i - 1]) / h[i]; u[i] = 2 * (h[i - 1] + h[i]); } // initialize values of v, z, and a v[1] = h[1] / u[1]; z[1] = 6 * (b[1] - b[0]) / u[1]; for (int i = 2; i < 9; i++) { v[i] = h[i] / (u[i] - h[i - 1] * v[i - 1]); z[i] = (6 * (b[i] - b[i - 1]) - h[i - 1] * z[i - 1]) / (u[i] - h[i - 1] * v[i - 1]); } a[9] = 0; for (int i = 8; i >= 1; i--) { a[i] = z[i] - v[i] * a[i + 1]; } // initialize values of c, d, l, m, p, q, r, s, t, f, g, j, and k for (int i = 1; i < 9; i++) { c[i] = y[i - 1]; d[i] = (y[i] - y[i - 1]) / h[i] - h[i] * (a[i + 1] + 2 * a[i]) / 6; l[i] = a[i] / (6 * h[i]); m[i] = a[i + 1] / (6 * h[i]); p[i] = (y[i] - y[i - 1]) / h[i] - h[i] * (2 * a[i] + a[i + 1]) / 6; q[i] = (y[i] - y[i - 1]) / h[i] - h[i] * (a[i] + a[i + 1]) / 3; r[i] = y[i]; s[i] = (y[i] - y[i - 1]) / h[i] - h[i] * (a[i] + 2 * a[i + 1]) / 6; t[i] = -a[i + 1] / (6 * h[i]); f[i] = x[i - 1]; g[i] = y[i]; j[i] = x[i]; k[i] = y[i]; } // draw the cat head using parabolic spline interpolation for (float i = 1; i < 9; i += 0.01) { float xCoord = i; float interval = floor(i); float yCoord = c[interval] + d[interval] * (xCoord - x[interval]) + l[interval] * pow(xCoord - x[interval], 3) + m[interval] * pow(xCoord - x[interval], 4); if (i < 2) { putpixel(xCoord, yCoord, WHITE); } else if (i < 3) { putpixel(xCoord, yCoord, YELLOW); } else if (i < 4) { putpixel(xCoord, yCoord, RED); } else if (i < 5) { putpixel(xCoord, yCoord, GREEN); } else if (i < 6) { putpixel(xCoord, yCoord, CYAN); } else if (i < 7) { putpixel(xCoord, yCoord, MAGENTA); } else if (i < 8) { putpixel(xCoord, yCoord, LIGHTGRAY); } else { putpixel(xCoord, yCoord, BLUE); } } getch(); closegraph(); } int main() { drawCatHead(); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值