图形学上机实验四-曲线的生成算法实现(二次、三次Bezier曲线)

实验四 曲线的生成算法实现

一、实验目的

1. 掌握B样条曲线、Bezier曲线的定义;

2. 能编程实现N 次B样条曲线、Bezier曲线的绘制与显示。

二、实验内容

1. 编程实现二次Bezier曲线的绘制

三、算法描述

1. 贝塞尔曲线

贝塞尔曲线的参数向量表达式

通常,n+1个顶点定义一个n次多项式。                                     

其中

称为伯恩斯坦(Bernstain)基函数。

四、实验步骤

1. 编程实现二次Bezier曲线的绘制;

2. 上机调试程序,显示最终绘图结果。

五、实验要求

1. 上交源程序;

2. 上交实验报告,实验报告内容如下:

(1)实验名称

(2)实验目的

(3)说明图形设计思想或程序流程图

(4)程序结果分析

 

二次Bezier曲线:

#include<stdio.h>
#include<graphics.h>
float decas(int degree,float coeff[],float t){
int r,i;
float t1;
float coeffa[10];
t1=1.0-t;
for(i=0;i<=degree;i++){
coeffa[i]=coeff[i];
}
for(r=1;r<=degree;r++){
for(i=0;i<=degree-r;i++){
coeffa[i]=t1*coeffa[i]+t*coeffa[i+1];
}
}
return (coeffa[0]);
}
void main(){
int i,n,k;
int gd = DETECT, gm = 0; 
float t,x,y;
static float px[4];
static float py[4];
n=2;
k=3000;
px[0]=50; px[1]=140;px[2]=400;
py[0]=400; py[1]=20;py[2]=40;
initgraph(&gd, &gm, "e:\\TC\\BGI");
cleardevice();
setcolor(BLUE);
for (i=0;i<n;i++)
line(px[i],py[i],px[i+1],py[i+1]);
setcolor(YELLOW);
for(i=0;i<=k;i++){
t=(float)i/k;
x=decas(n,px,t);
y=decas(n,py,t);
if(i==0)
moveto(x,y);
else
lineto(x,y);
}
getch();           
}

三次Bezier曲线:

#include <graphics.h>
void bezier_3(int color, double p[4][2])
{
    double t,t1,t2,xt,yt;
    int rate=200,x,y;
    setcolor(color);
    moveto(p[0][0],p[0][1]);
    for (t=0;t<=1;t+=1.0/rate)
    {
        yt=1-t;
 t1=yt*yt;
 t2=3*yt*t;
        xt=p[0][0]*t1*yt+p[1][0]*t2*yt+p[2][0]*t2*t+p[3][0]*t*t*t;
        yt=p[0][1]*yt*t1+p[1][1]*t2*yt+p[2][1]*t2*t+p[3][1]*t*t*t;
        x=(int)(xt);
        y=(int)(yt);
        lineto(x,y);
    }
}
void main()
{
    static double p[4][2]={50,400,140,20,400,40,635,420};
    const NO=3;           /*特征顶点数*/
    int i;
    int driver=DETECT,mode;
    initgraph(&driver,&mode,"e:\\TC\\BGI");
    cleardevice();
    setcolor(BLUE);
    moveto(p[0][0],p[0][1]);
    for (i=1;i<NO;i++)
    lineto(p[i][0],p[i][1]);
    bezier_3(LIGHTRED,p);
    getch();           /*按ESC键退出*/
    closegraph();
} 
 

 

  • 3
    点赞
  • 72
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值