1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
int ncr(int n,int r);
int fact(int n);
int round(int z);
void bcurve(int a, int b, int c, int d, float l, float m);
static int counter = 1;
static float m[6];
static float l[6];
//this array 'cpts ' is my problem
float cpts[5] = { 0, 0.25f, 0.5f, 0.75f, 1 };
int main()
{
int gd = 0, gm;
int n,r;
clrscr();
initgraph(&gd,&gm,"C:\\TC\\BGI");
//cout << "enter no of edges in poly" << endl;
for(int a = 0 ; a < sizeof(m) ; a++ )
{
m[a] = 0;
m[a] = 0;
}
for(int b = 0; b < sizeof(l); b++)
{
l[b] = 0;
l[b] = 0;
}
n = 4;
int x[10][2];
for(int i = 0; i < n; i++)
{
cout << "enter x & y " << endl;
int m, n;
cin >> m >>n;
x[i][0] = m;
x[i][1] = n;
}
x[n][0] = x[0][0];
x[n][1] = x[0][1];
//int fbptx = x[0][0];
//int fbpty = x[0][1];
//int lbptx = x[n][0];
//int lbpty = x[n][1];
//float u[5];
//even tried reassigning the value, but nothing works for me!
cpts[0] = (float) 0;
cpts[1] = (float) 0.25f;
cpts[2] = (float) 0.5f;
cpts[3] = (float) 0.75f;
cpts[4] = (float) 1;
m[0] = x[0][0];
l[0] = x[0][1];
m[n] = x[n-1][0];
l[n] = x[n-1][1];
m[5] = m[0];
l[5] = l[0];
for(int j = 0 ; j < 3 ; j++ )
{
for(int i = 0 ; i < 4 ; i++ )
{
int f = x[i][0];
int y = x[i][1];
float uth = pow( cpts[j+1],i);
float uk = pow( ( 1-cpts[j+1] ) , 3-i );
bcurve(f,y,3,i,uth,uk);
}
counter++;
}
setcolor(GREEN);
for(int c = 0 ; c < 5 ; c++)
{
int x = round(m[c]);
int y = round(l[c]);
int x1 = round(m[c+1]);
int y1 = round(l[c+1]);
line(x,y,x1,y1);
}
getch();
closegraph();
return 0;
}
int round(int x)
{
return (x+0.5);
}
void bcurve(int x, int y, int n, int r, float uth, float uk)
{
int xval = x * ncr(n,r) * uth * uk;
int yval = y * ncr(n,r) * uth * uk;
m[counter] += xval;
l[counter] += yval;
}
int ncr(int n, int r)
{
int nfact = fact(n);
int rfact = fact(r);
int m = n-r;
if(m >= 0){
int nminusrfact = fact(m);
int result = nfact/(rfact*(nminusrfact));
return result;
}else{
return -1;
}
}
int fact( int n )
{
if(n == 1 || n == 0 )
return 1;
else
return (n* fact(n-1));
}
|