/***********************************************************************
REVISION LOG ENTRY
Revision By: http://blog.csdn.net/hongweijin
Revised on : 2004-10-22 5:34:30
Comments : bezier二次曲线的插值模拟算法***********************************************************************/
#include "stdio.h"
#include "stdlib.h"
void main( void )
{
inti;
doublet;
doublex;
/*P0是一个顶点,P1是第三点确定的,P2是第二点确定的*/
float P0[2], P1[2], P2[2];
printf("输入第一点的坐标X:");
scanf("%f", &P0[0]);
printf("输入第一点的坐标Y:");
scanf("%f", &P0[1]);
printf("\n输入第二点的坐标X:");
scanf("%f", &P1[0]);
printf("输入第二点的坐标Y:");
scanf("%f", &P1[1]);
printf("\n输入第三点的坐标X:");
scanf("%f", &P2[0]);
printf("输入第三点的坐标Y:");
scanf("%f", &P2[1]);
for(i = 0; i < 10000; i++)
{
t = i / 10000.0;
x = (1-t) * (1-t) * P0[0] + (2*t) * (1-t) * P1[0] + t*t*P2[0];
printf("%f,", x);
x = (1-t) * (1-t) * P0[1] + (2*t) * (1-t) * P1[1] + t*t*P2[1];
printf("%f\n", x);
}
}
/***********************************************************************
REVISION LOG ENTRY
Revision By: http://blog.csdn.net/hongweijin
Revised on : 2004-10-22 5:34:30
Comments : bezier三次曲线的插值模拟算法
***********************************************************************/
#include "stdio.h"
#include "stdlib.h"
void main( void )
{
inti;
doublet;
doublex;
/*P0是一个顶点,P1是第三点确定的,P2是第二点确定的*/
float P0[2], P1[2], P2[2], P3[2];
printf("输入第一点的坐标X:");
scanf("%f", &P0[0]);
printf("输入第一点的坐标Y:");
scanf("%f", &P0[1]);
printf("\n输入第二点的坐标X:");
scanf("%f", &P1[0]);
printf("输入第二点的坐标Y:");
scanf("%f", &P1[1]);
printf("\n输入第三点的坐标X:");
scanf("%f", &P2[0]);
printf("输入第三点的坐标Y:");
scanf("%f", &P2[1]);
printf("\n输入第四点的坐标X:");
scanf("%f", &P3[0]);
printf("输入第四点的坐标Y:");
scanf("%f", &P3[1]);
for(i = 0; i < 100; i++)
{
t = i / 100.0;
x = (1-t)*(1-t)*(1-t)*P0[0]+(3*t)*(1-t)*(1-t)*P1[0]+3*t*t*(1-t)*P2[0]+t*t*t*P3[0];
printf("%f,", x);
x = (1-t)*(1-t)*(1-t)*P0[1]+(3*t)*(1-t)*(1-t)*P1[1]+3*t*t*(1-t)*P2[1]+t*t*t*P3[1];
printf("%f\n", x);
}
}
在TC3.0环境下用二次插值画的曲线:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
inti;
doublet;
int x;
int y;
int P0[2] = {1, 1}, P1[2] = {50, 50}, P2[2]={70, 1};
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "c:/tc");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* return with error code */
}
for(i = 0; i < 1000; i++)
{
t = i / 1000.0;
x =125 + 5* ((1-t) * (1-t) * P0[0] + (2*t) * (1-t) * P1[0] + t*t*P2[0]);
y =120 + 5*((1-t) * (1-t) * P0[1] + (2*t) * (1-t) * P1[1] + t*t*P2[1]);
/* if ((x == (102 + 5) && y == (120 + 5)) ||(x == (102 + 5*15) && y == (120 + 5))||(x == (102 + 5) && y == (120 + 5)) )
*/
putpixel( x, y, 4);
putpixel(125 + 5*P0[1], 120 + 5 * P0[1] ,2);
putpixel(125 + 5*P1[0], 120 + 5 * P1[1], 2);
putpixel(125 + 5*P2[0], 120 + 5 * P2[1] , 2);
}
/* clean up */
getch();
closegraph();
return 0;
}