Sie sind auf Seite 1von 46

GRAPHICS

LAB

Anmol Goel
422/IC/12
ICE-1, 4th Semester
N.S.I.T

Digital Differential Analyzer (DDA)


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int round(float);
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
// int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
// setcolor(getmaxcolor());
// xmax = getmaxx();
// ymax = getmaxy();
/* draw a diagonal line */
// line(0, 0, xmax, ymax);
/* clean up */

int x1,x2,y1,y2,dx,dy,x,y;
printf("Enter the start and end coordinates\n");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
clrscr();
float m;
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
x=x1;
y=y1;
putpixel(x,y,RED);

if(dx>dy)
{
while(x<=x2)
{
y+=m;
putpixel(x,round (y),RED);
x++;
}
}
else
while(y<=y2)
{
x+=1/m;
putpixel(round (x),y,RED);
y++;
}
getch();
closegraph();
return 0;
}
int round(float x)
{
float d;
int w;
w=10*x;
int a,b,c;
a=x;
b=10*x;
c=w-b;
d=c/10;
if (d>0.0 && d<0.5)
return a;
else
return (a+1);
}

Line using Mid-point


#include <graphics.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
// int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
// setcolor(getmaxcolor());
// xmax = getmaxx();
// ymax = getmaxy();
/* draw a diagonal line */
// line(0, 0, xmax, ymax);
int x1,y1,x2,y2,dx,dy,d,x,y,i,temp,temp1;
printf("Enter the start coordinates of line\n");
scanf("%d%d",&x1,&y1);
printf("Enter the end coordinates of line\n");
scanf("%d%d",&x2,&y2);
if(x2<x1 && y2<y1)
{
temp=x2;
x2=x1;
x1=temp;
temp1=y2;
y2=y1;
y1=temp1;
}

clrscr();
dx=x2-x1;
dy=y2-y1;
// printf("%d %d",dx,dy);
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(x1,i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(i,y1,15);
}
else if(abs(dy)>abs(dx))
{
d=dy-2*dx;
x=x1;
y=y1;
putpixel(x,y,15);
while(y<=y2)
{
if(d>0)
d-=2*dx;
else
{
d+=dy-2*dx;
x++;
}
y++;
putpixel(x,y,15);
}
}
else if(abs(dy)<=abs(dx))
{
d=2*dy-dx;
x=x1;
y=y1;
putpixel(x,y,15);
while(x<=x2)
{
if(d<0)
d+=2*dy;
else
{

d+=2*(dy-dx);
y++;
}
x++;
putpixel(x,y,15);
}
}

/* clean up */
getch();
closegraph();
return 0;
}

Draw a line using Bresenhams Approach


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
// int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
// setcolor(getmaxcolor());
// xmax = getmaxx();
// ymax = getmaxy();
/* draw a diagonal line */
// line(0, 0, xmax, ymax);
int x,y,x1,y1,x2,y2,dx,dy,p;
printf("Enter the start and end points\n");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
putpixel(x,y,RED);
while(x<=x2)
{
if (p<=0)
p+=2*dy;
else
{

p+=2*(dy-dx);
y++;
}
x++;
putpixel(x,y,RED);
}
getch();
closegraph();
return 0;
}

Draw a Circle using Mid-point (first order).


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* 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); /* terminate with an error code */
}

int x,y,r,d;
printf("enter the value of r\n");
scanf("%d",&r);
x=0;
y=r;
d=(5/4-r);
// printf("%f",d);
putpixel(320+x,240+y,15);
putpixel(320+x,240-y,15);
putpixel(320-x,240+y,15);
putpixel(320-x,240-y,15);
putpixel(320+y,240+x,15);
putpixel(320+y,240-x,15);
putpixel(320-y,240+x,15);
putpixel(320-y,240-x,15);

while(y>=x)
{
if(d<=0)
d+=2*x+3;
else
{
d+=2*(x-y)+5;

y--;
}
x++;
putpixel(320+x,240+y,15);
putpixel(320+x,240-y,15);
putpixel(320-x,240+y,15);
putpixel(320-x,240-y,15);
putpixel(320+y,240+x,15);
putpixel(320+y,240-x,15);
putpixel(320-y,240+x,15);
putpixel(320-y,240-x,15);
}
getch();
closegraph();
return 0;
}

Draw a Circle using Mid-point (second order).


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* 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); /* terminate with an error code */
}

int x,y,r;
int deltan,deltanw,d;
printf("enter the value of r\n");
scanf("%d",&r);
x=r;
y=0;
deltan=3;
deltanw=(5-2*r);
d=(5/4-r);
// printf("%f",d);
putpixel(320+x,240+y,15);
putpixel(320+x,240-y,15);
putpixel(320-x,240+y,15);
putpixel(320-x,240-y,15);
putpixel(320+y,240+x,15);
putpixel(320+y,240-x,15);
putpixel(320-y,240+x,15);
putpixel(320-y,240-x,15);

while(y<=x)
{
if(d<=0)
{

d+=deltan;
deltan+=2;
deltanw+=2;
}
else
{
d+=deltanw;
deltan+=2;
deltanw+=4;
x--;
}
y++;
putpixel(320+x,240+y,15);
putpixel(320+x,240-y,15);
putpixel(320-x,240+y,15);
putpixel(320-x,240-y,15);
putpixel(320+y,240+x,15);
putpixel(320+y,240-x,15);
putpixel(320-y,240+x,15);
putpixel(320-y,240-x,15);
}
getch();
closegraph();
return 0;
}

Ellipse using Mid-point


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/*setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy(); */
/* draw a diagonal line
line(0, 0, xmax, ymax); */
float x,y,a,b,d;
printf("enter the value of a and b");
scanf("%f %f",&a,&b);
//a=100,b=75;
x=0;
y=b;
d=0.25*a*a+b*b-a*a*b;
putpixel(320+x,240-y,15);
while(2*b*b*(x+1)<2*a*a*(y-0.5))
{
if(d<=0)
d+=b*b*(2*x+3);
else
{
d+=b*b*(2*x+3)+2*a*a*(1-y);
y--;
}

x++;
putpixel(320+x,240-y,15);
putpixel(320+x,240+y,15);
putpixel(320-x,240-y,15);
putpixel(320-x,240+y,15);
}
d=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1)-a*a*b*b;
while(y>=0)
{
if(d<=0)
{
d+=a*a*(3-2*y)+2*b*b*(x+1);
x++;
}
else
d+=a*a*(3-2*y);
y--;
putpixel(320+x,240-y,15);
putpixel(320+x,240+y,15);
putpixel(320-x,240-y,15);
putpixel(320-x,240+y,15);
}
/* clean up */
getch();
closegraph();
return 0;
}

Draw line clipping using Cyrus-Beck.


#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
struct point
{
float x,y;
};
void clip(point pol[10], point p1, point p2, int n)
{
cleardevice();
setcolor(YELLOW);
for(int i=0;i<n;i++)
{
line(pol[i].x,pol[i].y,pol[i+1].x,pol[i+1].y);
}
setcolor(WHITE);
line(p1.x,p1.y,p2.x,p2.y);
getch();
float t_enter=0,t_leave=1;
for(i=0;i<n;i++)
{
point n,pei;
pei=pol[i];
n.x=(pol[i+1].y-pol[i].y);
n.y=(pol[i+1].x-pol[i].x);
float num,den;
num = n.x*(pei.x-p1.x) - n.y*(pei.y-p1.y);
den = n.x*(p2.x-p1.x) + n.y*(p1.y-p2.y);
float t;
if(den!=0)
t= num*1.0/den;
if(t>=0 && t<=1)
{
if(den<0)
{
if(t>t_enter)
t_enter = t;
}
else if(den>0)
{
if(t<t_leave)
t_leave = t;
}

}
}
point pi,pl;
pi.x=p1.x+(p2.x-p1.x)*t_enter;
pi.y=p1.y+(p2.y-p1.y)*t_enter;
pl.x=p1.x+(p2.x-p1.x)*t_leave;
pl.y=p1.y+(p2.y-p1.y)*t_leave;
setcolor(GREEN);
line(pi.x,pi.y,pl.x,pl.y);
}
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("Enter the no. of vertices of clipping window :\n ");
int n;
scanf("%d",&n);
point pol[10];
printf("Enter the vertices in clockwise order \n");
for(int i=0;i<n;i++)
{
printf(" Enter vertex %d :\n ",i+1);
scanf("%f %f",&pol[i].x,&pol[i].y);
pol[i].x+=320;
pol[i].y=240-pol[i].y;
}
pol[i].x=pol[0].x;
pol[i].y=pol[0].y;
printf("Enter the start and end points of the line :\n ");
point p1,p2;
scanf("%f %f %f %f",&p1.x,&p1.y,&p2.x,&p2.y);
int t;
if(p1.x>p2.x)
{
t=p1.x;
p1.x=p2.x;
p2.x=t;
t=p1.y;
p1.y=p2.y;
p2.y=t;
}
p1.x+=320;p2.x+=320;
p1.y=240-p1.y;

p2.y=240-p2.y;
clip(pol,p1,p2,n);
getch();
closegraph();
}

Polygon Clipping- Sutherland Hodgemann


#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
void main()
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,xmin,ymin,i,j;
float a[10],b[10],c[10],D[10],e[10],f[10],g[10],h[10],k[10],l[10];
float x1,x2,y1,y2,dx,dy,X,Y,d,t;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

/* RECTANGULAR WINDOW */
printf("Enter the maximum value of co-ordinates\n");
scanf("%d %d",&xmax,&ymax);
printf("Enter the minimum value of co-ordinates\n");
scanf("%d %d",&xmin,&ymin);
line(320+xmin,240-ymin,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmax,240-ymax);
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
printf("Enter the co-ordinates of polygon in clockwise dirn starting from left\n");
for(i=1;i<=5;i++)
{
scanf("%f %f",&a[i],&b[i]);
}

/*Sutherland Hodgman Approach */


/* Left edge*/

j=1;
for(i=1;i<=4;i++)
{
if(a[i+1]>xmin)
{
if(a[i]>xmin)
{
c[j]=a[i+1];
D[j]=b[i+1];
}
else
{
t=-(a[i]-xmin)/(a[i+1]-a[i]);
c[j]=a[i]+(a[i+1]-a[i])*t;
D[j]=b[i]+(b[i+1]-b[i])*t;
j=j+1;
c[j]=a[1+i];
D[j]=b[1+i];
}
}
else
{
if(a[i]>xmin)
{
t=-(a[i]-xmin)/(a[i+1]-a[i]);
c[j]=a[i]+(a[i+1]-a[i])*t;
D[j]=b[i]+(b[i+1]-b[i])*t;
}

}
j++;
}
/* Top edge */
c[6]=c[1];
D[6]=D[1];
j=1;
for(i=1;i<=5;i++)
{
if(D[i+1]<ymax)
{
if(D[i]<ymax)
{
e[j]=c[i+1];
f[j]=D[i+1];
}
else

{
t=(D[i]-ymax)/(-D[i+1]+D[i]);
e[j]=c[i]+(c[i+1]-c[i])*t;
f[j]=D[i]+(D[i+1]-D[i])*t;
j=j+1;
e[j]=c[1+i];
f[j]=D[1+i];
}
}
else
{
if(D[i]<ymax)
{
t=(D[i]-ymax)/(-D[i+1]+D[i]);
e[j]=c[i]+(c[i+1]-c[i])*t;
f[j]=D[i]+(D[i+1]-D[i])*t;
}

}
j++;
}
/* Right edge*/
e[7]=e[1];
f[7]=f[1];
j=1;
for(i=1;i<=6;i++)
{
if(e[i+1]<xmax)
{
if(e[i]<xmax)
{
g[j]=e[i+1];
h[j]=f[i+1];
}
else
{
t=(e[i]-xmax)/(-e[i+1]+e[i]);
g[j]=e[i]+(e[i+1]-e[i])*t;
h[j]=f[i]+(f[i+1]-f[i])*t;
j=j+1;
g[j]=e[1+i];
h[j]=f[1+i];
}
}
else
{
if(e[i]<xmax)

{
t=(e[i]-xmax)/(-e[i+1]+e[i]);
g[j]=e[i]+(e[i+1]-e[i])*t;
h[j]=f[i]+(f[i+1]-f[i])*t;
}

}
j++;
}
/* Bottom Edge*/
g[8]=g[1];
h[8]=h[1];
j=1;
for(i=1;i<=7;i++)
{
if(h[i+1]>ymin)
{
if(h[i]>ymin)
{
k[j]=g[i+1];
l[j]=h[i+1];
}
else
{
t=-(h[i]-ymin)/(h[i+1]-h[i]);
k[j]=g[i]+(g[i+1]-g[i])*t;
l[j]=h[i]+(h[i+1]-h[i])*t;
j=j+1;
k[j]=e[1+i];
l[j]=f[1+i];
}
}
else
{
if(h[i]>ymin)
{
t=-(h[i]-ymin)/(h[i+1]-h[i]);
k[j]=g[i]+(g[i+1]-g[i])*t;
l[j]=h[i]+(h[i+1]-h[i])*t;
}

}
j++;
}

k[9]=k[1];
l[9]=l[1];
// Printing the final co-ordinates
/* for(i=1;i<=9;i++)
{
printf("%f\t %f\n",k[i],l[i]);
}
*/
for(i=1;i<=8;i++)
{
line(320+k[i],240-l[i],320+k[i+1],240-l[i+1]);
}

/* clean up */
getch();
closegraph();
}

2D Transformation Translation, Scaling and Rotation


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
int c,i,j,k;
float star[3][8]={0,40,60,80,120,80,60,40,60,80,120,80,60,40,0,40,1,1,1,1,1,1,1,1};
float rect[3][4]={50,70,70,50,50,50,70,70,1,1,1,1};
do
{
printf("enter your choice\n");
printf("1. Translation\n2. Scaling\n3. Rotation\n4. Exit\n");
scanf("%d",&c);
if(c==1)
{
float a,b;
printf("enter the factor by which the object has to be translated in x and y direction\n");
scanf("%f %f",&a,&b);
float t[3][3]={1,0,a,0,1,b,0,0,1};
//Matrix multiplication
float tnew1[3][8],tnew2[3][4];
for(i=0;i<3;i++)
{
for(j=0;j<8;j++)
{ tnew1[i][j]=0;
for(k=0;k<3;k++)

{
tnew1[i][j]+= star[k][j]*t[i][k];
}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{ tnew2[i][j]=0;
for(k=0;k<3;k++)
{
tnew2[i][j]+= rect[k][j]*t[i][k];
}
}
}
//to draw star
line(320+tnew1[0][0],240-tnew1[1][0],320+tnew1[0][1],240-tnew1[1][1]);
line(320+tnew1[0][1],240-tnew1[1][1],320+tnew1[0][2],240-tnew1[1][2]);
line(320+tnew1[0][2],240-tnew1[1][2],320+tnew1[0][3],240-tnew1[1][3]);
line(320+tnew1[0][3],240-tnew1[1][3],320+tnew1[0][4],240-tnew1[1][4]);
line(320+tnew1[0][4],240-tnew1[1][4],320+tnew1[0][5],240-tnew1[1][5]);
line(320+tnew1[0][5],240-tnew1[1][5],320+tnew1[0][6],240-tnew1[1][6]);
line(320+tnew1[0][6],240-tnew1[1][6],320+tnew1[0][7],240-tnew1[1][7]);
line(320+tnew1[0][7],240-tnew1[1][7],320+tnew1[0][0],240-tnew1[1][0]);
//to draw rect
line(320+tnew2[0][0],240-tnew2[1][0],320+tnew2[0][1],240-tnew2[1][1]);
line(320+tnew2[0][1],240-tnew2[1][1],320+tnew2[0][2],240-tnew2[1][2]);
line(320+tnew2[0][2],240-tnew2[1][2],320+tnew2[0][3],240-tnew2[1][3]);
line(320+tnew2[0][3],240-tnew2[1][3],320+tnew2[0][0],240-tnew2[1][0]);
}
else
if(c==2)
{
float sx1,sy1,sx2,sy2;
printf("for star: Sx and Sy\n");
scanf("%f %f",&sx1,&sy1);
printf("for rectange: Sx and Sy\n");
scanf("%f %f",&sx2,&sy2);
float scale1[3][3]={sx1,0,0,0,sy1,0,0,0,1};
float scale2[3][3]={sx2,0,0,0,sy2,0,0,0,1};
float snew1[3][8],snew2[3][4];
//Matrix multiplication
for(i=0;i<3;i++)
{
for(j=0;j<8;j++)
{ snew1[i][j]=0;
for(k=0;k<3;k++)
{
snew1[i][j]=snew1[i][j]+ star[k][j]*scale1[i][k];

}
}
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{ snew2[i][j]=0;
for(k=0;k<3;k++)
{
snew2[i][j]+= rect[k][j]*scale2[i][k];
}
}
}
//to draw star
line(320+snew1[0][0],240-snew1[1][0],320+snew1[0][1],240-snew1[1][1]);
line(320+snew1[0][1],240-snew1[1][1],320+snew1[0][2],240-snew1[1][2]);
line(320+snew1[0][2],240-snew1[1][2],320+snew1[0][3],240-snew1[1][3]);
line(320+snew1[0][3],240-snew1[1][3],320+snew1[0][4],240-snew1[1][4]);
line(320+snew1[0][4],240-snew1[1][4],320+snew1[0][5],240-snew1[1][5]);
line(320+snew1[0][5],240-snew1[1][5],320+snew1[0][6],240-snew1[1][6]);
line(320+snew1[0][6],240-snew1[1][6],320+snew1[0][7],240-snew1[1][7]);
line(320+snew1[0][7],240-snew1[1][7],320+snew1[0][0],240-snew1[1][0]);
//to draw rect
line(320+snew2[0][0],240-snew2[1][0],320+snew2[0][1],240-snew2[1][1]);
line(320+snew2[0][1],240-snew2[1][1],320+snew2[0][2],240-snew2[1][2]);
line(320+snew2[0][2],240-snew2[1][2],320+snew2[0][3],240-snew2[1][3]);
line(320+snew2[0][3],240-snew2[1][3],320+snew2[0][0],240-snew2[1][0]);
}
else
if(c==3)
{
float x1,x;
printf("enter the angle by which the object has to be rotated\n");
scanf("%f",&x);
x1=((3.14)*x)/(180);
float r[3][3]={cos(x1),(-1)*sin(x1),0,sin(x1),cos(x1),0,0,0,1};
float rnew1[3][8],rnew2[4][8];
//Matrix multiplication
for(i=0;i<3;i++)
{
for(j=0;j<8;j++)
{rnew1[i][j]=0;
for(k=0;k<3;k++)
{
rnew1[i][j]+= star[k][j]*r[i][k];
}
}
}
for(i=0;i<3;i++)
{

for(j=0;j<8;j++)
{ rnew2[i][j]=0;
for(k=0;k<3;k++)
{
rnew2[i][j]+= rect[k][j]*r[i][k];
}
}
}
//to draw star
line(320+rnew1[0][0],240-rnew1[1][0],320+rnew1[0][1],240-rnew1[1][1]);
line(320+rnew1[0][1],240-rnew1[1][1],320+rnew1[0][2],240-rnew1[1][2]);
line(320+rnew1[0][2],240-rnew1[1][2],320+rnew1[0][3],240-rnew1[1][3]);
line(320+rnew1[0][3],240-rnew1[1][3],320+rnew1[0][4],240-rnew1[1][4]);
line(320+rnew1[0][4],240-rnew1[1][4],320+rnew1[0][5],240-rnew1[1][5]);
line(320+rnew1[0][5],240-rnew1[1][5],320+rnew1[0][6],240-rnew1[1][6]);
line(320+rnew1[0][6],240-rnew1[1][6],320+rnew1[0][7],240-rnew1[1][7]);
line(320+rnew1[0][7],240-rnew1[1][7],320+rnew1[0][0],240-rnew1[1][0]);
//to draw rect
line(320+rnew2[0][0],240-rnew2[1][0],320+rnew2[0][1],240-rnew2[1][1]);
line(320+rnew2[0][1],240-rnew2[1][1],320+rnew2[0][2],240-rnew2[1][2]);
line(320+rnew2[0][2],240-rnew2[1][2],320+rnew2[0][3],240-rnew2[1][3]);
line(320+rnew2[0][3],240-rnew2[1][3],320+rnew2[0][0],240-rnew2[1][0]);
}
}while(c!=4);
/* clean up */
getch();
closegraph();
return 0;
}

3D-Transformation Translation, Scaling and Rotation


#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,i,j,k;
float prism[4][6]={10,70,40,10,70,40,10,10,70,10,10,70,0,0,0,60,60,60,1,1,1,1,1,1};
float rot[4][4]={1,0,0,0,0,0.707,-0.707,0,0,0.707,0.707,0,0,0,0,1};
float sum, A[4][6], B[4][6];
float rot2[4][4]={1,0,0,0,0,0.707,0.707,0,0,-0.707,0.707,0,0,0,0,1};
/* struct point
{
float x[6];
float y[6];
float z[6];
}p1;*/
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/*setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();
*/
/* draw a diagonal line */
line(320+prism[0][0],240-prism[1][0],320+prism[0][1],240-prism[1][1]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][4],240-prism[1][4],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][2],240-prism[1][2],320+prism[0][5],240-prism[1][5]);

line(320+prism[0][1],240-prism[1][1],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][3],240-prism[1][3]);
clrscr();
//matrix multiplication//
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot[i][j]*prism[j][k];
}
A[i][k]=sum;
}
}
line(320+A[0][0],240-A[1][0],320+A[0][1],240-A[1][1]);
line(320+A[0][1],240-A[1][1],320+A[0][2],240-A[1][2]);
line(320+A[0][0],240-A[1][0],320+A[0][2],240-A[1][2]);
line(320+A[0][3],240-A[1][3],320+A[0][4],240-A[1][4]);
line(320+A[0][4],240-A[1][4],320+A[0][5],240-A[1][5]);
line(320+A[0][3],240-A[1][3],320+A[0][5],240-A[1][5]);
line(320+A[0][2],240-A[1][2],320+A[0][5],240-A[1][5]);
line(320+A[0][1],240-A[1][1],320+A[0][4],240-A[1][4]);
line(320+A[0][0],240-A[1][0],320+A[0][3],240-A[1][3]);
/* printf("the initial matrix is=\n");
for(i=0;i<4; i++)
{
for(j=0; j<6; j++)
{
printf("%0.2f\t", prism[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("the final matrix is(anti-clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", A[i][k]);
}
printf("\n");
}
printf("\n\n");*/
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{

sum=0;
for(j=0; j<4; j++)
{
sum+=rot2[i][j]*A[j][k];
}
B[i][k]=sum;
}
}
/* printf("the final matrix is(clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", B[i][k]);
}
printf("\n");
}
*/
/* clean up */
getch();
closegraph();
return 0;
}

Isometric Projections
#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,i,j,k;
float prism[4][6]={10,70,40,10,70,40,10,10,70,10,10,70,0,0,0,60,60,60,1,1,1,1,1,1};
// float rot[4][4]={1,0,0,0,0,0.707,-0.707,0,0,0.707,0.707,0,0,0,0,1};
float sum, A[4][6], B[4][6];
float rot1[4][4]={0.707,0,0.707,0,0.408,0.816,-0.408,0,0,0,0,0,0,0,0,1};
// float rot2[4][4]={1,0,0,0,0,0.707,0.707,0,0,-0.707,0.707,0,0,0,0,1};
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/* draw a diagonal line */
line(320+prism[0][0],240-prism[1][0],320+prism[0][1],240-prism[1][1]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][4],240-prism[1][4],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][2],240-prism[1][2],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][3],240-prism[1][3]);
cleardevice();
//matrix multiplication//
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;

for(j=0; j<4; j++)


{
sum+=rot1[i][j]*prism[j][k];
}
A[i][k]=sum;
}
}
line(320+A[0][0],240-A[1][0],320+A[0][1],240-A[1][1]);
line(320+A[0][1],240-A[1][1],320+A[0][2],240-A[1][2]);
line(320+A[0][0],240-A[1][0],320+A[0][2],240-A[1][2]);
line(320+A[0][3],240-A[1][3],320+A[0][4],240-A[1][4]);
line(320+A[0][4],240-A[1][4],320+A[0][5],240-A[1][5]);
line(320+A[0][3],240-A[1][3],320+A[0][5],240-A[1][5]);
line(320+A[0][2],240-A[1][2],320+A[0][5],240-A[1][5]);
line(320+A[0][1],240-A[1][1],320+A[0][4],240-A[1][4]);
line(320+A[0][0],240-A[1][0],320+A[0][3],240-A[1][3]);
/* printf("the initial matrix is=\n");
for(i=0;i<4; i++)
{
for(j=0; j<6; j++)
{
printf("%0.2f\t", prism[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("the final matrix is(anti-clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", A[i][k]);
}
printf("\n");
}
printf("\n\n");*/
/*
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot2[i][j]*A[j][k];
}
B[i][k]=sum;
}
}

/* printf("the final matrix is(clockwise rotation 45 deg)=\n");


for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", B[i][k]);
}
printf("\n");
}
*/

/* clean up */
getch();
closegraph();
return 0;
}

Oblique Projections
#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,i,j,k;
float a=2,b=2,c=2;
float prism[4][6]={10,70,40,10,70,40,10,10,70,10,10,70,0,0,0,60,60,60,1,1,1,1,1,1};
// float rot[4][4]={1,0,0,0,0,0.707,-0.707,0,0,0.707,0.707,0,0,0,0,1};
float sum, A[4][6], B[4][6];
float rot1[4][4]={1,0,-a/c,0,0,1,-b/c,0,0,0,0,0,0,0,0,1};
// float rot2[4][4]={1,0,0,0,0,0.707,0.707,0,0,-0.707,0.707,0,0,0,0,1};
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/* draw a diagonal line */
line(320+prism[0][0],240-prism[1][0],320+prism[0][1],240-prism[1][1]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][4],240-prism[1][4],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][2],240-prism[1][2],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][3],240-prism[1][3]);
cleardevice();
//matrix multiplication//
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{

sum=0;
for(j=0; j<4; j++)
{
sum+=rot1[i][j]*prism[j][k];
}
A[i][k]=sum;
}
}
line(320+A[0][0],240-A[1][0],320+A[0][1],240-A[1][1]);
line(320+A[0][1],240-A[1][1],320+A[0][2],240-A[1][2]);
line(320+A[0][0],240-A[1][0],320+A[0][2],240-A[1][2]);
line(320+A[0][3],240-A[1][3],320+A[0][4],240-A[1][4]);
line(320+A[0][4],240-A[1][4],320+A[0][5],240-A[1][5]);
line(320+A[0][3],240-A[1][3],320+A[0][5],240-A[1][5]);
line(320+A[0][2],240-A[1][2],320+A[0][5],240-A[1][5]);
line(320+A[0][1],240-A[1][1],320+A[0][4],240-A[1][4]);
line(320+A[0][0],240-A[1][0],320+A[0][3],240-A[1][3]);
/* printf("the initial matrix is=\n");
for(i=0;i<4; i++)
{
for(j=0; j<6; j++)
{
printf("%0.2f\t", prism[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("the final matrix is(anti-clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", A[i][k]);
}
printf("\n");
}
printf("\n\n");*/
/*
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot2[i][j]*A[j][k];
}
B[i][k]=sum;
}
}

/* printf("the final matrix is(clockwise rotation 45 deg)=\n");


for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", B[i][k]);
}
printf("\n");
}
*/

/* clean up */
getch();
closegraph();
return 0;
}

Perspective Projection
#include <math.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax,i,j,k,d=1;
float prism[4][6]={10,70,40,10,70,40,10,10,70,10,10,70,0,0,0,60,60,60,1,1,1,1,1,1};
// float rot[4][4]={1,0,0,0,0,0.707,-0.707,0,0,0.707,0.707,0,0,0,0,1};
float sum, A[4][6], B[4][6];
float rot1[4][4]={d,0,0,0,0,d,0,0,0,0,0,0,0,0,1,d};
// float rot1[4][4]={0.707,0,0.707,0,0.408,0.816,-0.408,0,0,0,0,0,0,0,0,1};
// float rot2[4][4]={1,0,0,0,0,0.707,0.707,0,0,-0.707,0.707,0,0,0,0,1};
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/* draw a diagonal line */
line(320+prism[0][0],240-prism[1][0],320+prism[0][1],240-prism[1][1]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][2],240-prism[1][2]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][4],240-prism[1][4],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][3],240-prism[1][3],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][2],240-prism[1][2],320+prism[0][5],240-prism[1][5]);
line(320+prism[0][1],240-prism[1][1],320+prism[0][4],240-prism[1][4]);
line(320+prism[0][0],240-prism[1][0],320+prism[0][3],240-prism[1][3]);
cleardevice();
//matrix multiplication//
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{

sum=0;
for(j=0; j<4; j++)
{
sum+=rot1[i][j]*prism[j][k];
}
A[i][k]=sum/(A[3][k]+d);
}
}
line(320+A[0][0],240-A[1][0],320+A[0][1],240-A[1][1]);
line(320+A[0][1],240-A[1][1],320+A[0][2],240-A[1][2]);
line(320+A[0][0],240-A[1][0],320+A[0][2],240-A[1][2]);
line(320+A[0][3],240-A[1][3],320+A[0][4],240-A[1][4]);
line(320+A[0][4],240-A[1][4],320+A[0][5],240-A[1][5]);
line(320+A[0][3],240-A[1][3],320+A[0][5],240-A[1][5]);
line(320+A[0][2],240-A[1][2],320+A[0][5],240-A[1][5]);
line(320+A[0][1],240-A[1][1],320+A[0][4],240-A[1][4]);
line(320+A[0][0],240-A[1][0],320+A[0][3],240-A[1][3]);
/* printf("the initial matrix is=\n");
for(i=0;i<4; i++)
{
for(j=0; j<6; j++)
{
printf("%0.2f\t", prism[i][j]);
}
printf("\n");
}
printf("\n\n");
printf("the final matrix is(anti-clockwise rotation 45 deg)=\n");
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", A[i][k]);
}
printf("\n");
}
printf("\n\n");*/
/*
for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
sum=0;
for(j=0; j<4; j++)
{
sum+=rot2[i][j]*A[j][k];
}
B[i][k]=sum;
}
}

/* printf("the final matrix is(clockwise rotation 45 deg)=\n");


for(i=0; i<4; i++)
{
for(k=0; k<6; k++)
{
printf("%0.2f\t", B[i][k]);
}
printf("\n");
}
*/

/* clean up */
getch();
closegraph();
return 0;
}

SeedFill
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
/* void drawline(int , int ,int,int);*/
void seedfill(int x,int y,int a, int b)
{
if( getpixel(x,y)!=a && getpixel(x,y)!=b )
{
putpixel(x,y,b);
seedfill(x+1,y,a,b);
seedfill(x,y+1,a,b);
seedfill(x-1,y,a,b);
seedfill(x,y-1,a,b);
}
}
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
// int xmax, ymax;
int p,px1[30],px2[30],py1[30],py2[30],xs,ys,bcolor=15,fcolor=12,j;
p=0;
int a[5],b[5],i;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
/*setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();*/
/* draw a diagonal line */
// LOOP TO DRAW POLYGON
printf("enter the number of sides of the polygon\n");

scanf("%d",&p);
printf("enter the co-ordinates\n");
/*
for(i=0;i<=p;i++)
{
scanf("%d",&a[i],&b[i]);
}
for(i=0;i<p;i++)
{
line(a[i],b[i],a[i+1],b[i+1]);
}*/
//printf("enter the color of the edges\n");
//scanf("%d",&bcolor);
for(j=0;j<p;j++)
{
printf("for line %d",j+1);
scanf("%d %d %d %d",&px1[j],&py1[j],&px2[j],&py2[j]);
line(320+px1[j],240-py1[j],320+px2[j],240-py2[j]);
}
//POLYGON FILLING BY SEEDFILL
/* printf("enter the color to be filled\n");
scanf("%d",&fcolor);*/
/*xs=50;
ys=50;*/
printf("Enter the coordinates that lie inside the polygon\n");
scanf("%d%d",&xs,&ys);
seedfill(xs,ys,15,12);
/* clean up */
getch();
closegraph();
return 0;
}
//seedfill program//
//program to print a line
/*void drawline(int x1, int y1, int x2, int y2)
{
int i=0;
int dx,dy,X=0,Y=0,d;
if(y2>y1&&x2>x1)
{
dx=x2-x1;
dy=y2-y1;
if(dx==0)
{
for(i=y1;i<=y2;i++)

putpixel(320+x1,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(320+i,240-y1,15);
}
else if(abs(dy)>abs(dx) && abs(dx)!=0)
{
X=x1;
Y=y1;
d=dy-2*dx;
putpixel(320+X,240-Y,15);
while(Y<=y2)
{
if(d>=0)
d+=-2*dx;
else
{
d+=2*dy-2*dx;
X++;
}
Y++;
putpixel(320+X,240-Y,15);
}
}
else if(abs(dx)>abs(dy) && abs(dy)!=0)
{
X=x1;
Y=y1;
d=2*dy-dx;
putpixel(320+X,240-Y,15);
while(X<=x2)
{
if(d<=0)
d+=2*dy;
else
{
d+=2*dy-2*dx;
Y++;
}
X++;
putpixel(320+X,240-Y,15);
}
}
else

{
X=x1;
Y=y1;
putpixel(320+X,240-Y,15);
while(X<=x2)
{
X++;
Y++;
putpixel(320+X,240-Y,15);
}
}
}*/
/*negative*/
/* else
{
dx=x2-x1;
dy=y2-y1;
if(dx==0)
{
for(i=y1;i<=y2;i++)
putpixel(320+x1,240-i,15);
}
else if(dy==0)
{
for(i=x1;i<=x2;i++)
putpixel(320+i,240-y1,15);
}
else if(abs(dy)>abs(dx) && abs(dx)!=0)
{
X=x1;
Y=y1;
d=dy-2*dx;
putpixel(320+X,240-Y,15);
while(Y>=y2)
{
if(d<0)
d+=2*dx;
else
{
d+=2*(dy+dx);
X++;
}
Y--;
putpixel(320+X,240-Y,15);
}
}

else if(abs(dx)>abs(dy) && abs(dy)!=0)


{
X=x1;
Y=y1;
d=2*dy+dx;
putpixel(320+X,240-Y,15);
while(X<=x2)
{
if(d>0)
d+=2*dy;
else
{
d+=2*dy+2*dx;
Y--;
}
X++;
putpixel(320+X,240-Y,15);
}
}
else
{
X=x1;
Y=y1;
putpixel(320+X,240-Y,15);
while(X<=x2)
{
X++;
Y++;
putpixel(320+X,240-Y,15);
}
}
}
} */

B-Spline
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

float x0,x1,y0,y1,z0,z1,x0d,y0d,z0d,x1d,y1d,z1d;
printf("enter the starting coordinates\n");
scanf("%f %f %f",&x0,&y0,&z0);
printf("enter the ending coordinates\n");
scanf("%f %f %f",&x1,&y1,&z1);
float x,y,z,t;
for(t=0;t<=1;t+=0.0001)
{
x=(2*pow(t,3)-3*pow(t,2)+1)*x0 + ((-1)*2*pow(t,3)+3*pow(t,2))*x1 + (pow(t,3)2*pow(t,2)+t)*x0d + (pow(t,3)-pow(t,2))*x1d;
y=(2*pow(t,3)-3*pow(t,2)+1)*y0 + ((-1)*2*pow(t,3)+3*pow(t,2))*y1 + (pow(t,3)2*pow(t,2)+t)*y0d + (pow(t,3)-pow(t,2))*y1d;
z=(2*pow(t,3)-3*pow(t,2)+1)*z0 + ((-1)*2*pow(t,3)+3*pow(t,2))*z1 + (pow(t,3)2*pow(t,2)+t)*z0d + (pow(t,3)-pow(t,2))*z1d;
putpixel(320+x,240-y,15);
}

printf("enter the starting coordinates\n");


scanf("%f %f %f",&x0,&y0,&z0);
printf("enter the ending coordinates\n");
scanf("%f %f %f",&x1,&y1,&z1);

// float x,y,z,t;
for(t=0;t<=1;t+=0.0001)
{
x=(2*pow(t,3)-3*pow(t,2)+1)*x0 + ((-1)*2*pow(t,3)+3*pow(t,2))*x1 + (pow(t,3)2*pow(t,2)+t)*10 + (pow(t,3)-pow(t,2))*x1d;
y=(2*pow(t,3)-3*pow(t,2)+1)*y0 + ((-1)*2*pow(t,3)+3*pow(t,2))*y1 + (pow(t,3)2*pow(t,2)+t)*10 + (pow(t,3)-pow(t,2))*y1d;
z=(2*pow(t,3)-3*pow(t,2)+1)*z0 + ((-1)*2*pow(t,3)+3*pow(t,2))*z1 + (pow(t,3)2*pow(t,2)+t)*z0d + (pow(t,3)-pow(t,2))*z1d;
putpixel(320+x,240-y,15);
}

printf("enter the starting coordinates\n");


scanf("%f %f %f",&x0,&y0,&z0);
printf("enter the ending coordinates\n");
scanf("%f %f %f",&x1,&y1,&z1);
// float x,y,z,t;
for(t=0;t<=1;t+=0.0001)
{
x=(2*pow(t,3)-3*pow(t,2)+1)*x0 + ((-1)*2*pow(t,3)+3*pow(t,2))*x1 + (pow(t,3)2*pow(t,2)+t)*50 + (pow(t,3)-pow(t,2))*(-1)*50;
y=(2*pow(t,3)-3*pow(t,2)+1)*y0 + ((-1)*2*pow(t,3)+3*pow(t,2))*y1 + (pow(t,3)2*pow(t,2)+t)*50 + (pow(t,3)-pow(t,2))*(-1)*50;
z=(2*pow(t,3)-3*pow(t,2)+1)*z0 + ((-1)*2*pow(t,3)+3*pow(t,2))*z1 + (pow(t,3)2*pow(t,2)+t)*z0d + (pow(t,3)-pow(t,2))*z1d;
putpixel(320+x,240-y,15);
}

printf("enter the starting coordinates\n");


scanf("%f %f %f",&x0,&y0,&z0);
printf("enter the ending coordinates\n");
scanf("%f %f %f",&x1,&y1,&z1);
// float x,y,z,t;
for(t=0;t<=1;t+=0.0001)
{
x=(2*pow(t,3)-3*pow(t,2)+1)*x0 + ((-1)*2*pow(t,3)+3*pow(t,2))*x1 + (pow(t,3)2*pow(t,2)+t)*120 + (pow(t,3)-pow(t,2))*(-1)*120;
y=(2*pow(t,3)-3*pow(t,2)+1)*y0 + ((-1)*2*pow(t,3)+3*pow(t,2))*y1 + (pow(t,3)2*pow(t,2)+t)*(-1)*120 + (pow(t,3)-pow(t,2))*120;
z=(2*pow(t,3)-3*pow(t,2)+1)*z0 + ((-1)*2*pow(t,3)+3*pow(t,2))*z1 + (pow(t,3)2*pow(t,2)+t)*z0d + (pow(t,3)-pow(t,2))*z1d;
putpixel(320+x,240-y,15);
}

/* clean up */
getch();
closegraph();
return 0;
}

Das könnte Ihnen auch gefallen