编译器:VS2013
描述:Bezier画线是利用导数相同拼接曲线,使曲线十分光滑,而不是随意拼接观赏性很差
主函数段
1 #include "stdafx.h" 2 #include<stdio.h> 3 #include"graphics.h" 4 #include<stdlib.h> 5 #include<math.h> 6 7 //函数声明 8 void Bezier4(int a[]);//四个控制点画出曲线 9 void Beziern(int a[], int N);//N个点画出曲线 10 int factorial(int n);//利用递归求出阶乘 11 12 int main() 13 { 14 int *p, N,gdriver = DETECT, gmove, i; 15 16 printf("please input the number of point "); 17 scanf_s("%d", &N); 18 19 p = (int *)malloc(2 * N*sizeof(int)); 20 21 printf("please input the coordinate: "); 22 for (i = 0; i < 2 * N; i += 2) 23 scanf_s("%d%d", &p[i], &p[i + 1]); 24 25 initgraph(&gdriver, &gmove, ""); 26 27 //画出多边形 28 for (i = 0; i < 2*N-2; i += 2) 29 line(p[i], p[i + 1], p[i + 2], p[i + 3]); 30 31 Beziern(p, N); 32 33 system("pause"); 34 35 closegraph(); 36 37 return 0; 38 }
Bezier画线函数
1 //N个点画出曲线 2 void Beziern(int a[], int N) 3 { 4 double t, X1 = a[0], Y1 = a[1]; 5 int k,i; 6 7 for (t = 0; t <= 1; t += 0.02) 8 { 9 putpixel(X1, Y1, YELLOW); 10 11 X1 = 0, Y1 = 0; 12 13 for (i=0,k = 0; k<N; i+=2,k ++) 14 { 15 X1 += 1.0*a[i] * factorial(N-1) / factorial(k) / factorial(N-1 - k)*pow(t, k)*pow(1 - t, N-1 - k);//pow函数为求指数的函数 16 Y1 += 1.0*a[i+1] * factorial(N-1) / factorial(k) / factorial(N-1 - k)*pow(t, k)*pow(1 - t, N -1- k); 17 } 18 19 } 20 } 21 22 //利用递归求出阶乘 23 inline int factorial(int n) 24 { 25 if (n > 1) 26 return n*factorial(n - 1); 27 else 28 return 1; 29 }
结果: