Beruflich Dokumente
Kultur Dokumente
GRAPHICS
-Sanchit Manchanda
293/CO/09
Draw Line
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<windows.h>
#define HEIGHT 600
#define WIDTH 800
static int x=WIDTH/2,y=HEIGHT/2;
void swap(int x1,int x2)
{
x2=x1+x2;
x1=x2-x1;
x2=x2-x1;
}
void move()
{
char ch=getch();
if(ch=='a' || ch=='A')
{
x=x-5;
putpixel(x,y,WHITE);
putpixel(x+5,y,BLACK);
}
else if(ch=='d' || ch=='D')
{
x=x+5;
putpixel(x,y,WHITE);
putpixel(x-5,y,BLACK);
}
else if(ch=='S' || ch=='s')
{
y=y+5;
putpixel(x,y,WHITE);
putpixel(x,y-5,BLACK);
}
else if(ch=='W' || ch=='w')
{
y=y-5;
putpixel(x,y,WHITE);
putpixel(x,y+5,BLACK);
}
}
void drawlinemidpoint(int x1,int y1,int x2,int y2,int color)
{
int dx, dy, p, end;
int x, y;
dx = abs(x1 - x2);
dy = abs(y1 - y2);
int m=dy/dx;
if(m<=1)
{
p = dy - dx/2;
putpixel(x, y, color);
while(y < end)
{
y++;
if(p < 0)
{
p = p + dx;
}
else
{
if(y1>y2)
{
if(x1>x2)
x++;
else
x--;
}
else
{
if(x1>x2)
x--;
else
x++;
}
p = p + (dx - dy);
}
delay(3);
putpixel(x, y, color);
}
}
}
void drawlinebresenham(int x1,int y1,int x2,int y2,int color)
{
int dx, dy, p, end;
int x, y;
dx = abs(x1 - x2);
dy = abs(y1 - y2);
int m=dy/dx;
if(m<1)
{
p = 2 * dy - dx;
if(x1 > x2)
{
x = x2;
y = y2;
end = x1;
}
else
{
x = x1;
y = y1;
end = x2;
}
putpixel(x, y, color);
while(x < end)
{
x++;
if(p < 0)
{
p = p + 2 * dy;
}
else
{
if(x1>x2)
{
if(y1>y2)
y++;
else
y--;
}
else
{
if(y1>y2)
y--;
else
y++;
}
p = p + 2 * (dy - dx);
}
delay(3);
putpixel(x, y, color);
}
}
if(m>=1)
{
p = 2 * dx - dy;
if(y1 > y2)
{
x = x2;
y = y2;
end = y1;
}
else
{
x = x1;
y = y1;
end = y2;
}
putpixel(x, y, color);
while(y < end)
{
y++;
if(p < 0)
{
p = p + 2 * dx;
}
else
{
if(y1>y2)
{
if(x1>x2)
x++;
else
x--;
}
else
{
if(x1>x2)
x--;
else
x++;
}
p = p + 2 * (dx - dy);
}
delay(3);
putpixel(x, y, color);
}
}
}
void drawlinedda(int x1,int y1,int x2,int y2,int color)
{
float x,y,xinc,yinc,dx,dy;
int k=1;
int step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(x,y,1);
while(k<=step)
{
x=x+xinc;
y=y+yinc;
delay(3);
putpixel(x,y,color);
k++;
}
}
int main()
{
HANDLE hStdin;
DWORD cNumRead;
INPUT_RECORD irInBuf[1];
POINT pos;
if ( HANDLE(hStdin = GetStdHandle( STD_INPUT_HANDLE )) == INVALID_HANDLE_VALUE )
return 1;
initwindow(WIDTH,HEIGHT);
char ch;
int x1=-1,y1=-1,x2=-1,y2=-1;
int choice;
scanf("%d",&choice);
while(1)
{
if(choice==1)
{
int m,n;
if(ismouseclick(WM_LBUTTONDOWN))
{
getmouseclick(WM_LBUTTONDOWN, m, n);
if(x1==-1 && x2==-1)
{
x1=m;
y1=n;
putpixel(x1,y1,RED);
}
else if(x1!=-1 && x2==-1)
{
x2=m;
y2=n;
putpixel(x2,y2,RED);
drawlinedda(x1,y1,x2,y2,GREEN);
drawlinemidpoint(x1,y1,x2,y2,RED);
drawlinebresenham(x1,y1,x2,y2,BLUE);
x1=-1,x2=-1,y1=-1,y2=-1;
}
}
}
if(choice==2)
{
if((ch=getch())=='0')
break;
if((ch=getch())=='i')
{
if(x1==-1 && x2==-1)
{
x1=x;
y1=y;
putpixel(x1,y1,RED);
}
else if(x1!=-1 && x2==-1)
{
x2=x;
y2=y;
putpixel(x2,y2,RED);
}
}
if(x1!=-1 && x2!=-1)
{
drawlinebresenham(x1,y1,x2,y2,BLUE);
drawlinemidpoint(x1,y1,x2,y2,RED);
drawlinedda(x1,y1,x2,y2,GREEN);
x1=-1,x2=-1,y1=-1,y2=-1;
}
else
{
move();
}
}
}
getch();
return 0;
}
Draw Circle
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#define HEIGHT 600
#define WIDTH 800
static int x=WIDTH/2,y=HEIGHT/2;
void move()
{
char ch=getch();
if(ch=='a' || ch=='A')
{
x=x-5;
putpixel(x,y,WHITE);
putpixel(x+5,y,BLACK);
}
else if(ch=='d' || ch=='D')
{
x=x+5;
putpixel(x,y,WHITE);
putpixel(x-5,y,BLACK);
}
else if(ch=='S' || ch=='s')
{
y=y+5;
putpixel(x,y,WHITE);
putpixel(x,y-5,BLACK);
}
else if(ch=='W' || ch=='w')
{
y=y-5;
putpixel(x,y,WHITE);
putpixel(x,y+5,BLACK);
}
}
void plotpoints(int xcentre,int ycentre,int x,int y,int color)
{
delay(10);
putpixel(xcentre+x,ycentre+y,color);
putpixel(xcentre-x,ycentre+y,color);
putpixel(xcentre+x,ycentre-y,color);
putpixel(xcentre-x,ycentre-y,color);
delay(10);
putpixel(xcentre+y,ycentre+x,color);
putpixel(xcentre-y,ycentre+x,color);
putpixel(xcentre+y,ycentre-x,color);
putpixel(xcentre-y,ycentre-x,color);
}
void drawcirclebresenham(int xcentre,int ycentre,int radius)
{
int x=0;
int y=radius;
int p=3-2*radius;
plotpoints(xcentre,ycentre,x,y,RED);
while(x<y)
{
if(p<0)
p+=4*x+6;
else
{
p+=4*(x-y)+10;
y--;
}
x++;
plotpoints(xcentre,ycentre,x,y,RED);
}
}
void drawcirclemidpoint(int xcentre,int ycentre,int radius)
{
int x=0;
int y=radius;
int p=5/4-radius;
plotpoints(xcentre,ycentre,x,y,GREEN);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
plotpoints(xcentre,ycentre,x,y,GREEN);
}
}
int main()
{
initwindow(WIDTH,HEIGHT);
char ch;
int xcentre=-1,ycentre=-1,radius=-1;
int choice;
scanf("%d",&choice);
while(1)
{
if(choice==1)
{
int m,n;
if(ismouseclick(WM_LBUTTONDOWN))
{
getmouseclick(WM_LBUTTONDOWN, m, n);
if(xcentre==-1 && radius==-1)
{
xcentre=m;
ycentre=n;
putpixel(m,n,RED);
}
else if(xcentre!=-1 && radius==-1)
{
radius= sqrt( pow((xcentre-m),2)+pow((ycentre-n),2));
putpixel(m,n,RED);
drawcirclebresenham(xcentre,ycentre,radius);
drawcirclemidpoint(xcentre,ycentre,radius);
xcentre=-1,ycentre=-1,radius=-1;
}
}
}
if(choice==2)
{
if((ch=getch())=='0')
break;
if((ch=getch())=='i')
{
if(xcentre==-1 && radius==-1)
{
xcentre=x;
ycentre=y;
putpixel(x,y,RED);
}
else if(xcentre!=-1 && radius==-1)
{
radius= sqrt( pow((xcentre-x),2)+pow((ycentre-y),2));
putpixel(x,y,RED);
drawcirclebresenham(xcentre,ycentre,radius);
drawcirclemidpoint(xcentre,ycentre,radius);
xcentre=-1,ycentre=-1,radius=-1;
}
}
else
{
move();
}
}
}
getch();
return 0;
}
Draw Ellipse
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include <math.h>
#define HEIGHT 600
#define WIDTH 800
static int x=WIDTH/2,y=HEIGHT/2;
void ellipseMidpoint(float, float, float, float);
void drawEllipse(float, float, float, float, int);
void ellipseBresenham(float xc, float yc, float rx, float ry);
void move()
{
char ch=getch();
if(ch=='a' || ch=='A')
{
x=x-5;
putpixel(x,y,WHITE);
putpixel(x+5,y,BLACK);
}
else if(ch=='d' || ch=='D')
{
x=x+5;
putpixel(x,y,WHITE);
putpixel(x-5,y,BLACK);
}
else if(ch=='S' || ch=='s')
{
y=y+5;
putpixel(x,y,WHITE);
putpixel(x,y-5,BLACK);
}
else if(ch=='W' || ch=='w')
{
y=y-5;
putpixel(x,y,WHITE);
putpixel(x,y+5,BLACK);
}
}
int main()
{
initwindow(WIDTH,HEIGHT);
char ch;
int xcentre=-1,ycentre=-1,xradius=-1,yradius=-1;
int choice;
scanf("%d",&choice);
while(1)
{
if(choice==1)
{
int m,n;
if(ismouseclick(WM_LBUTTONDOWN))
{
getmouseclick(WM_LBUTTONDOWN, m, n);
if(xcentre==-1 && xradius==-1 && yradius==-1)
{
xcentre=m;
ycentre=n;
putpixel(m,n,RED);
}
else if(xcentre!=-1 && xradius==-1 && yradius==-1)
{
xradius= abs(xcentre-m);
//putpixel(m,n,RED);
}
else if(xcentre!=-1 && xradius!=-1 && yradius==-1)
{
yradius= abs(ycentre-n);
//putpixel(x,y,RED);
ellipseMidpoint(xcentre, ycentre, xradius, yradius);
ellipseBresenham(xcentre, ycentre, yradius, xradius);
xcentre=-1,ycentre=-1,xradius=-1,yradius=-1;
}
}
}
if(choice==2)
{
if((ch=getch())=='0')
break;
if((ch=getch())=='i')
{
if(xcentre==-1 && xradius==-1 && yradius==-1)
{
xcentre=x;
ycentre=y;
putpixel(xcentre,ycentre,RED);
}
else if(xcentre!=-1 && xradius==-1 && yradius==-1)
{
xradius= abs(xcentre-x);
putpixel(x,y,RED);
}
else if(xcentre!=-1 && xradius!=-1 && yradius==-1)
{
yradius= abs(ycentre-y);
putpixel(x,y,RED);
}
}
if(xcentre!=-1 && xradius!=-1 && yradius!=-1)
{
ellipseMidpoint(xcentre, ycentre, xradius, yradius);
xcentre=-1,ycentre=-1,xradius=-1,yradius=-1;
}
else
{
move();
}
}
}
getch();
return 0;
}
void ellipseMidpoint(float xc, float yc, float rx, float ry)
{
float rxSq = rx * rx;
float rySq = ry * ry;
float x = 0, y = ry, p;
float px=0, py = 2 * rxSq * y;
//drawEllipse(xc, yc, x, y, 3);
p = rySq - (rxSq * ry) + (0.25 * rxSq);
while (px < py)
{
x++;
px = 2 * rySq * x + 3*rySq;
if (p < 0)
p = p + px;
else
{
y--;
py = 2*rxSq*y - 2*rxSq;
p = p + px - py;
}
drawEllipse(xc, yc, x, y, 5);
delay(5);
}
p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;
while (y > 0)
{
y--;
py = 2 * rxSq * y - 3 * rxSq;
if (p > 0)
p = p - py;
else
{
x++;
px= 2 * rySq * x + 2 * rySq;
p = p - py + px;
}
drawEllipse(xc, yc, x, y, GREEN);
delay(5);
}
}
void ellipseBresenham(float xc, float yc, float rx, float ry)
{
float rxSq = rx * rx;
Draw Hyperbola
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include <math.h>
#define HEIGHT 600
#define WIDTH 800
static int x=WIDTH/2,y=HEIGHT/2;
void hyperbolaMidpoint(float, float, float, float);
void drawHyperbola(float, float, float, float, int);
void hyperbolaBresenham(float xc, float yc, float rx, float ry);
void move()
{
char ch=getch();
if(ch=='a' || ch=='A')
{
x=x-5;
putpixel(x,y,WHITE);
putpixel(x+5,y,BLACK);
}
else if(ch=='d' || ch=='D')
{
x=x+5;
putpixel(x,y,WHITE);
putpixel(x-5,y,BLACK);
}
else if(ch=='S' || ch=='s')
{
y=y+5;
putpixel(x,y,WHITE);
putpixel(x,y-5,BLACK);
}
else if(ch=='W' || ch=='w')
{
y=y-5;
putpixel(x,y,WHITE);
putpixel(x,y+5,BLACK);
}
}
int main()
{
initwindow(WIDTH,HEIGHT);
char ch;
int xcentre=-1,ycentre=-1,xradius=-1,yradius=-1;
int choice;
scanf("%d",&choice);
while(1)
{
if(choice==1)
{
int m,n;
if(ismouseclick(WM_LBUTTONDOWN))
{
getmouseclick(WM_LBUTTONDOWN, m, n);
if(xcentre==-1 && xradius==-1 && yradius==-1)
{
xcentre=m;
ycentre=n;
putpixel(m,n,RED);
}
else if(xcentre!=-1 && xradius==-1 && yradius==-1)
{
xradius= abs(xcentre-m);
//putpixel(m,n,RED);
}
else if(xcentre!=-1 && xradius!=-1 && yradius==-1)
{
yradius= abs(ycentre-n);
//putpixel(x,y,RED);
hyperbolaMidpoint(xcentre, ycentre, xradius, yradius);
hyperbolaBresenham(xcentre+100, ycentre+100, xradius, yradius);
xcentre=-1,ycentre=-1,xradius=-1,yradius=-1;
}
}
}
if(choice==2)
{
if((ch=getch())=='0')
break;
if((ch=getch())=='i')
{
if(xcentre==-1 && xradius==-1 && yradius==-1)
{
xcentre=x;
ycentre=y;
putpixel(xcentre,ycentre,RED);
}
else if(xcentre!=-1 && xradius==-1 && yradius==-1)
{
xradius= abs(xcentre-x);
putpixel(x,y,RED);
}
else if(xcentre!=-1 && xradius!=-1 && yradius==-1)
{
yradius= abs(ycentre-y);
putpixel(x,y,RED);
}
}
if(xcentre!=-1 && xradius!=-1 && yradius!=-1)
{
hyperbolaMidpoint(xcentre, ycentre, xradius, yradius);
hyperbolaBresenham(xcentre, ycentre, xradius, yradius);
xcentre=-1,ycentre=-1,xradius=-1,yradius=-1;
}
else
{
move();
}
}
}
getch();
return 0;
}
void hyperbolaMidpoint(float xc, float yc, float rx, float ry)
{
float rxSq = rx * rx;
float rySq = ry * ry;
float x = rx, y = 0, p;
float px= 2*rySq *x, py=0;
p=rySq*(x+0.5)*(x+0.5)- rxSq -rxSq*rySq;
while(py<=px)
{
if(p>=0)
{
p = p - rxSq*(2*y-3);
}
else
{
p = p - rxSq*(2*y+3) + rySq*(2*x+2);
x++;
//px = px + 2*rySq;
}
y++;
py = py + 2 * rxSq;
drawHyperbola(xc, yc, x, y, 5);
}
p = (rySq - (rxSq * ry) + (0.25 * rxSq))/0.5;
while(y<ry)
{
x++;
if(p>=0)
{
p = p - rySq*(2*x+3);
}
else
{
p = p + rxSq*(2*y-2) - rySq*(2*x+3);
y++;
}
drawHyperbola(xc, yc, x, y, GREEN);
}
}
Draw Parabola
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include <math.h>
#define HEIGHT 600
#define WIDTH 800
static int x=WIDTH/2,y=HEIGHT/2;
void parabolaMidpoint(float, float, float);
void drawparabola(float, float, float, float, int);
void parabolaBresenham(float, float, float);
void move()
{
char ch=getch();
if(ch=='a' || ch=='A')
{
x=x-5;
putpixel(x,y,WHITE);
putpixel(x+5,y,BLACK);
}
else if(ch=='d' || ch=='D')
{
x=x+5;
putpixel(x,y,WHITE);
putpixel(x-5,y,BLACK);
}
else if(ch=='S' || ch=='s')
{
y=y+5;
putpixel(x,y,WHITE);
putpixel(x,y-5,BLACK);
}
else if(ch=='W' || ch=='w')
{
y=y-5;
putpixel(x,y,WHITE);
putpixel(x,y+5,BLACK);
}
}
int main()
{
initwindow(WIDTH,HEIGHT);
char ch;
int xcentre=-1,ycentre=-1,xradius=-1;
int choice;
scanf("%d",&choice);
while(1)
{
if(choice==1)
{
int m,n;
if(ismouseclick(WM_LBUTTONDOWN))
{
getmouseclick(WM_LBUTTONDOWN, m, n);
if(xcentre==-1 && xradius==-1)
{
xcentre=m;
ycentre=n;
putpixel(m,n,RED);
}
else if(xcentre!=-1 && xradius==-1)
{
xradius= abs(xcentre-x);
putpixel(x,y,RED);
}
else if(xcentre!=-1 && xradius!=-1)
{
xradius= abs(xcentre-m);
parabolaMidpoint(0, 0, xradius);
parabolaBresenham(100, 100, xradius);
xcentre=-1,ycentre=-1,xradius=-1;
}
}
}
if(choice==2)
{
if((ch=getch())=='0')
break;
if((ch=getch())=='i')
{
if(xcentre==-1 && xradius==-1)
{
xcentre=x;
ycentre=y;
putpixel(xcentre,ycentre,RED);
}
else if(xcentre!=-1 && xradius==-1)
{
xradius= abs(xcentre-x);
putpixel(x,y,RED);
}
}
if(xcentre!=-1 && xradius!=-1)
{
parabolaMidpoint(xcentre, ycentre, xradius);
parabolaBresenham(xcentre, ycentre, xradius);
xcentre=-1,ycentre=-1,xradius=-1;
}
else
{
move();
}
}
}
getch();
return 0;
}
void drawParabola(int x,int y,int color)
{
putpixel(x+400,y+300,color);
putpixel(x+400,-y+300,color);
}
double d1;
d1 = (2*a) - 1;
drawParabola(x+100,y,GREEN);
while(y <= (2*a))
{
if(d1<0)
{
d1+= 4*a-3-2*y;
x++;
}
else
{
d1-= 3 + 2*y;
}
y++;
drawParabola(x+100,y,RED);
}
d1 = (4.0*a*(x+1) - (y+0.5)*(y+0.5) );
while( y < 5*a )
{
if(d1<0)
{
d1+= 4*a;
}
else
{
d1+= 4*a - 2 - 2*y ;
y++;
}
x++;
drawParabola(x+100,y,GREEN);
}
}
}
void drawsin()
{
int x=0,y;
while(x<WIDTH)
{
y=-100*sin(x*PI/180);
putpixel(x+WIDTH/2,y+HEIGHT/2,RED);
putpixel(-x+WIDTH/2,-y+HEIGHT/2,RED);
x++;
}
}
void drawcos()
{
int x=0,y;
while(x<WIDTH)
{
y=-100*cos(x*PI/180);
putpixel(x+WIDTH/2,y+HEIGHT/2,WHITE);
putpixel(-x+WIDTH/2,y+HEIGHT/2,WHITE);
x++;
}
}
void drawcircle()
{
int x=0,y=0,r=200;
while(x<=r/sqrt(2))
{
y= sqrt(pow(r,2)-pow(x,2));
putpixel(x+WIDTH/2,y+HEIGHT/2,GREEN);
putpixel(x+WIDTH/2,-y+HEIGHT/2,GREEN);
putpixel(-x+WIDTH/2,y+HEIGHT/2,GREEN);
putpixel(-x+WIDTH/2,-y+HEIGHT/2,GREEN);
putpixel(y+WIDTH/2,x+HEIGHT/2,GREEN);
putpixel(y+WIDTH/2,-x+HEIGHT/2,GREEN);
putpixel(-y+WIDTH/2,x+HEIGHT/2,GREEN);
putpixel(-y+WIDTH/2,-x+HEIGHT/2,GREEN);
x++;
}
}
void drawEllipse()
{
int x=0,y=0,a=300,b=150;
while(x<=sqrt(abs(pow(a,2)-pow(b,2))))
{
y=sqrt(pow(a,2)*pow(b,2)-pow(b,2)*pow(x,2))/a;
putpixel(x+WIDTH/2,y+HEIGHT/2,YELLOW);
putpixel(x+WIDTH/2,-y+HEIGHT/2,YELLOW);
putpixel(-x+WIDTH/2,y+HEIGHT/2,YELLOW);
putpixel(-x+WIDTH/2,-y+HEIGHT/2,YELLOW);
x++;
}
while(y>=0)
{
x=sqrt(pow(a,2)*pow(b,2)-pow(a,2)*pow(y,2))/b;
putpixel(x+WIDTH/2,y+HEIGHT/2,YELLOW);
putpixel(x+WIDTH/2,-y+HEIGHT/2,YELLOW);
putpixel(-x+WIDTH/2,y+HEIGHT/2,YELLOW);
putpixel(-x+WIDTH/2,-y+HEIGHT/2,YELLOW);
y--;
}
}
void drawpara()
{
int x=0,y=0,a=100;
while(y<=4*pow(a,2))
{
x=pow(y,2)/(4*a);
putpixel(x+WIDTH/2,y+HEIGHT/2,BLUE);
putpixel(x+WIDTH/2,-y+HEIGHT/2,BLUE);
y++;
}
while(x<WIDTH/2)
{
y= -sqrt(4*a*x);
putpixel(x+WIDTH/2,y+HEIGHT/2,BLUE);
putpixel(x+WIDTH/2,-y+HEIGHT/2,BLUE);
x++;
}
}
void init()
{
line(0,HEIGHT/2,WIDTH,HEIGHT/2);
line(WIDTH/2,0,WIDTH/2,HEIGHT);
}
int main()
{
initwindow(WIDTH,HEIGHT);
init();
drawsin();
drawcos();
drawline();
drawcircle();
drawpara();
drawEllipse();
getch();
return 0;
}
2D-Transformation
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
#include <math.h>
#define HEIGHT 600
#define WIDTH 800
void translateX(int a[],int t)
{
for(int i=0;i<4;i++)
{
a[i]=a[i]+t;
}
}
void translateY(int a[],int t)
{
for(int i=0;i<4;i++)
{
a[i]=a[i]+t;
}
}
void scaleX(int a[],int t)
{
for(int i=0;i<4;i++)
{
a[i]=a[i]*t;
}
}
void scaleY(int a[],int t)
{
for(int i=0;i<4;i++)
{
a[i]=a[i]*t;
}
}
void rotate(int a[],int b[],float t)
{
for(int i=0;i<4;i++)
{
int m=a[i],n=b[i];
a[i]=m*cos(3.14*t/180) - n*sin(3.14*t/180);
b[i]=m*sin(3.14*t/180) + n*cos(3.14*t/180);
}
}
void draw(int a[],int b[])
{
line(a[0],b[0],a[1],b[1]);
line(a[1],b[1],a[2],b[2]);
line(a[2],b[2],a[3],b[3]);
line(a[3],b[3],a[0],b[0]);
}
int main()
{
initwindow(WIDTH,HEIGHT);
int a[4],b[4];
a[0]=100,b[0]=100;
a[1]=300,b[1]=100;
a[2]=100,b[2]=300;
a[3]=300,b[3]=300;
int t;
float angle;
draw(a,b);
int choice;
do
{
printf("\nEnter choice: 1.rotate 2.scaleX 3.scaleY 4.translateX 5.translateY 0.exit \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
scanf("%f",&angle);
rotate(a,b,angle);
cleardevice();
draw(a,b);
break;
case 2:
scanf("%d",&t);
scaleX(a,t);
cleardevice();
draw(a,b);
break;
case 3:
scanf("%d",&t);
scaleY(b,t);
cleardevice();
draw(a,b);
break;
case 4:
scanf("%d",&t);
translateX(a,t);
cleardevice();
draw(a,b);
break;
case 5:
scanf("%d",&t);
translateY(b,t);
cleardevice();
draw(a,b);
break;
default:
break;
}
}while(choice!=0);
getch();
return 0;
if (x > xmax)
oc |= RIGHT;
else if (x < xmin)
oc |= LEFT;
return oc;
}
void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{
int accept;
int done;
outcode outcode1, outcode2;
accept = FALSE;
done = FALSE;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
do
{
if (outcode1 == 0 && outcode2 == 0)
{
accept = TRUE;
done = TRUE;
}
else if (outcode1 & outcode2)
{
done = TRUE;
}
else
{
double x, y;
int main()
{
int n;
int i, j;
int ln[MAX][4];
int clip[4];
int gd = DETECT, gm;
printf ("Enter the number of lines to be clipped");
scanf ("%d", &n);
u1 = r1>u1?r1:u1;
else
u2 = r1<u2?r1:u2;
}
if( p2 != 0.0 )
{
r2 = (float ) q2 /p2 ;
if( p2 < 0 )
u1 = r2>u1?r2:u1;
else
u2 = r2<u2?r2:u2;
}
if( p3 != 0.0 )
{
r3 = (float )q3 /p3 ;
if( p3 < 0 )
u1 = r3>u1?r3:u1;
else
u2 = r3<u2?r3:u2;
}
if( p4 != 0.0 )
{
r4 = (float )q4 /p4 ;
if( p4 < 0 )
u1 = r4>u1?r4:u1;
else
u2 = r4<u2?r4:u2;
}
if( u1 > u2 )
printf("line rejected\n");
else
{
x11 = x1 + u1 * ( x2 - x1 ) ;
y11 = y1 + u1 * ( y2 - y1 ) ;
x22 = x1 + u2 * ( x2 - x1 );
y22 = y1 + u2 * ( y2 - y1 );
printf("Original line cordinates\n");
printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);
printf("Windows coordinate are \n");
printf("wxmin = %d, wymin = %d,wxmax = %d , wymax = %d \n",wxmin,wymin,wxmax,wymax);
printf("New coordinates are \n");
printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
setcolor(GREEN);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(RED);
line(x1,y1,x2,y2);
getch();
setcolor(BLUE);
line(x1,y1,x2,y2);
setcolor(WHITE);
line(x11,y11,x22,y22);
getch();
}
}
return 0;
}
Flood Fill
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void fill(int x,int y)
{
if(getpixel(x,y) == 0)
{
putpixel(x,y,RED);
fill(x+1,y);
fill(x-1,y);
fill(x,y-1);
fill(x,y+1);
}
}
int main()
{
int x , y ,a[10][10];
int gd, gm ,n,i;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter the no. of edges of polygon : ");
scanf("%d",&n);
printf("\nEnter the cordinates of polygon :\n\n\n ");
for(i=0;i<n;i++)
{
printf("X%d Y%d : ",i,i);
scanf("%d %d",&a[i][0],&a[i][1]);
}
a[n][0]=a[0][0];
a[n][1]=a[0][1];
printf("\nEnter the seed pt. : ");
scanf("%d%d",&x,&y);
cleardevice();
setcolor(WHITE);
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill(x,y);
getch();
return 0;
}
Boundary Fill
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void fill(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill(x+1,y);
fill(x-1,y);
fill(x,y-1);
fill(x,y+1);
}
}
int main()
{
int x,y,n,i;
int gd=DETECT,gm;
initgraph(&gd,&gm,"......\\bgi");
line (100,100,100,400);
line (100,400,400,400);
line (400,400,400,100);
line (400,100,100,100);
x = 250; y = 250;
fill(x,y);
getch();
return 0;
}
Animation
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
int main()
{
int gd,gm;
int x,y;
int i,j,k;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(WHITE);
line(0,400,640,400);
rectangle(300,330,340,400);
rectangle(310,320,330,330);
setcolor(4);
line(319,280,319,398);
line(320,280,320,398);
rectangle(320,280,330,300);
outtextxy(340,280,"PRESS ANY KEY TO LAUNCH THE ROCKET");
getch();
for(j=400;j<640;j++)
{
cleardevice();
setcolor(WHITE);
line(0,j,640,j);
rectangle(300,j-70,340,j);
rectangle(310,j-80,330,j-70);
setcolor(RED);
line(319,280,319,400);
line(320,280,320,400);
rectangle(320,280,330,300);
setcolor(YELLOW);
circle(325,300,2);
delay(5);
}
for(i=400;i>340;i--)
{
cleardevice();
setcolor(RED);
line(319,i,319,i-120);
line(320,i,320,i-120);
rectangle(320,i-120,330,i-100);
setcolor(YELLOW);
circle(325,i-100,2);
delay(25);
}
cleardevice();
k=0;
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(k);
k=k+3;
delay(50);
}
ellipse(320,30,0,360,j+100,j+0);
}
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(BLACK);
delay(2);
}
ellipse(320,30,0,360,j+100,j+0);
}
cleardevice();
getch();
return 0;
}
3D- Transformation
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#include<iostream.h>
using namespace std;
static float a[8][3],a2[8][3]={0};
static float x1=300, x2=400, y3=300, y2=400, z1=300, z2=400;
static float mid[1][3]={(x1+x2)/2,(y3+y2)/2,(z1+z2)/2};
void draw(float a[8][3])
{
for(int i=0;i<3;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[3][0],a[3][1],a[0][0],a[0][1]);
for(int i=4;i<7;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[7][0],a[7][1],a[4][0],a[4][1]);
for(int i=0;i<4;i++)
{
line(a[i][0],a[i][1],a[i+4][0],a[i+4][1]);
}
}
void rotabtX(float a[8][3],float theta)
{
theta=theta*3.14/180;
float S=sin(theta),C=cos(theta);
for(int i=0;i<8;i++)
{
a2[i][1]=(a[i][1]-mid[0][1])*C-(a[i][2]-mid[0][2])*S;
a2[i][2]=(a[i][1]-mid[0][1])*S+(a[i][2]-mid[0][2])*C;
}
for(int i=0;i<8;i++)
{
for(int j=1;j<3;j++)
a[i][j]=a2[i][j]+mid[0][j];
}
int sumx=0,sumy=0,sumz=0;
for(int i=0;i<8;i++)
{
sumx+=a[i][0];
sumy+=a[i][1];
sumz+=a[i][2];
}
mid[0][0]=sumx/8;
mid[0][1]=sumy/8;
mid[0][2]=sumz/8;
cleardevice();
}
void rotabtY(float a[8][3],float theta)
{
theta=theta*3.14/180;
float S=sin(theta),C=cos(theta);
for(int i=0;i<8;i++)
{
a2[i][0]=(a[i][0]-mid[0][0])*C+(a[i][2]-mid[0][2])*S;
a2[i][2]=-(a[i][0]-mid[0][0])*S+(a[i][2]-mid[0][2])*C;
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
if(j==1);
else
a[i][j]=a2[i][j]+mid[0][j];
}
}
int sumx=0,sumy=0,sumz=0;
for(int i=0;i<8;i++)
{
sumx+=a[i][0];
sumy+=a[i][1];
sumz+=a[i][2];
}
mid[0][0]=sumx/8;
mid[0][1]=sumy/8;
mid[0][2]=sumz/8;
cleardevice();
}
void rotabtZ(float a[8][3],float theta)
{
theta=theta*3.14/180;
float S=sin(theta),C=cos(theta);
for(int i=0;i<8;i++)
{
a2[i][0]=(a[i][0]-mid[0][0])*C-(a[i][1]-mid[0][1])*S;
a2[i][1]=(a[i][0]-mid[0][0])*S+(a[i][1]-mid[0][1])*C;
}
for(int i=0;i<8;i++)
{
for(int j=0;j<2;j++)
{
a[i][j]=a2[i][j]+mid[0][j];
}
}
int sumx=0,sumy=0,sumz=0;
for(int i=0;i<8;i++)
{
sumx+=a[i][0];
sumy+=a[i][1];
sumz+=a[i][2];
}
mid[0][0]=sumx/8;
mid[0][1]=sumy/8;
mid[0][2]=sumz/8;
cleardevice();
}
void translate(float a[8][3],int tx,int ty)
{
for(int i=0;i<8;i++)
{
a[i][0]+=tx;
a[i][1]+=ty;
}
cleardevice();
}
void scale(float a[8][3],float sx,float sy)
{
for(int i=0;i<8;i++)
{
a2[i][0]=(a[i][0]-mid[0][0])*sx;
a2[i][1]=(a[i][1]-mid[0][1])*sy;
}
for(int i=0;i<8;i++)
{
a[i][0]=a2[i][0]+mid[0][0];
a[i][1]=a2[i][1]+mid[0][1];
}
cleardevice();
}
int main()
{
a[0][0]=x1,a[0][1]=y3,a[0][2]=z1;
a[1][0]=x2,a[1][1]=y3,a[1][2]=z1;
a[2][0]=x2,a[2][1]=y2,a[2][2]=z1;
a[3][0]=x1,a[3][1]=y2,a[3][2]=z1;
a[4][0]=x1,a[4][1]=y3,a[4][2]=z2;
a[5][0]=x2,a[5][1]=y3,a[5][2]=z2;
a[6][0]=x2,a[6][1]=y2,a[6][2]=z2;
a[7][0]=x1,a[7][1]=y2,a[7][2]=z2;
initwindow(800,600);
draw(a);
char ch,ch1,ch2;
do
{
cout<<"\n1.Rotate 2.Translate 3.Scale 0.Exit";
cin>>ch;
switch(ch)
{
case '1':
cout<<"\nEnter Axis x/y/z: ";
cin>>ch1;
if(ch1=='x')
{
while(1)
{
if(ch2=getch()=='1')
rotabtX(a,-1);
else if(ch2=getch()=='2')
rotabtX(a,1);
else if(ch2=getch()=='3')
break;
cleardevice();
draw(a);
}
}
if(ch1=='y')
{
while(1)
{
if(ch2=getch()=='1')
rotabtY(a,-1);
else if(ch2=getch()=='2')
rotabtY(a,1);
else if(ch2=getch()=='3')
break;
cleardevice();
draw(a);
}
}
if(ch1=='z')
{
while(1)
{
if(ch2=getch()=='1')
rotabtZ(a,-1);
else if(ch2=getch()=='2')
rotabtZ(a,1);
else if(ch2=getch()=='3')
break;
cleardevice();
draw(a);
}
}
break;
case '2':
cout<<"\nEnter Axis x/y: ";
cin>>ch1;
if(ch1=='x')
{
while(1)
{
if(ch2=getch()=='1')
translate(a,-1,0);
else if(ch2=getch()=='2')
translate(a,1,0);
else if(ch2=getch()=='3')
break;
cleardevice();
draw(a);
}
}
if(ch1=='y')
{
while(1)
{
if(ch2=getch()=='1')
translate(a,0,-1);
else if(ch2=getch()=='2')
translate(a,0,1);
else if(ch2=getch()=='3')
break;
cleardevice();
draw(a);
}
}
break;
case '3':
cout<<"\nEnter Axis x/y: ";
cin>>ch1;
if(ch1=='x')
{
while(1)
{
if(ch2=getch()=='1')
scale(a,0.9,1);
else if(ch2=getch()=='2')
scale(a,1.1,1);
else if(ch2=getch()=='3')
break;
cleardevice();
draw(a);
}
}
if(ch1=='y')
{
while(1)
{
if(ch2=getch()=='1')
scale(a,1,0.9);
else if(ch2=getch()=='2')
scale(a,1,1.1);
else if(ch2=getch()=='3')
break;
cleardevice();
draw(a);
}
}
break;
default:
break;
}
}while(ch!='0');
getch();
return 0;
}
for(int i=0;i<8;i++)
{
a[i][0]= (a[i][0] + mid[0][0]);
a[i][1]= (a[i][1] + mid[0][1]);
}
}
void cabinet45(float a[8][3])
{
float phi=45;
for(int i=0;i<8;i++)
{
a[i][0]= (a[i][0] - mid[0][0]) + (a[i][2] - mid[0][2])*cos(3.14*phi/180)/2;
a[i][1]= (a[i][1] - mid[0][1]) + (a[i][2] - mid[0][2])*sin(3.14*phi/180)/2;
}
for(int i=0;i<8;i++)
{
a[i][0]= (a[i][0] + mid[0][0]);
a[i][1]= (a[i][1] + mid[0][1]);
}
}
void draw(float a[8][3])
{
for(int i=0;i<3;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[3][0],a[3][1],a[0][0],a[0][1]);
for(int i=4;i<7;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[7][0],a[7][1],a[4][0],a[4][1]);
for(int i=0;i<4;i++)
{
line(a[i][0],a[i][1],a[i+4][0],a[i+4][1]);
}
}
void init(float a[8][3])
{
a[0][0]=x1,a[0][1]=y3,a[0][2]=z1;
a[1][0]=x2,a[1][1]=y3,a[1][2]=z1;
a[2][0]=x2,a[2][1]=y2,a[2][2]=z1;
a[3][0]=x1,a[3][1]=y2,a[3][2]=z1;
a[4][0]=x1,a[4][1]=y3,a[4][2]=z2;
a[5][0]=x2,a[5][1]=y3,a[5][2]=z2;
a[6][0]=x2,a[6][1]=y2,a[6][2]=z2;
a[7][0]=x1,a[7][1]=y2,a[7][2]=z2;
}
int main()
{
initwindow(800,600);
init(a);
draw(a);
//cavalier
cavalier30(a);
delay(1000);
cleardevice();
outtext("Cavalier phi=30");
draw(a);
delay(1000);
cleardevice();
init(a);
draw(a);
delay(1000);
cleardevice();
cavalier45(a);
outtext("Cavalier phi=45");
draw(a);
//cabinet
delay(1000);
cleardevice();
init(a);
draw(a);
delay(1000);
cabinet30(a);
cleardevice();
outtext("Cabinet phi=30");
draw(a);
delay(1000);
cleardevice();
init(a);
draw(a);
delay(1000);
cleardevice();
cabinet45(a);
outtext("Cabinet phi=45");
draw(a);
getch();
return 0;
}
Perspective Projection
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<iostream.h>
using namespace std;
static float a[8][3],a2[8][3]={0};
static float x1=300, x2=400, y3=300, y2=400, z1=300, z2=400;
static float mid[1][3]={(x1+x2)/2,(y3+y2)/2,(z1+z2)/2};
static float zprp,zvp;
//formulae used x = x * (zprp -zvp)/(zprp - z)
// y= y * (zprp -zvp)/(zprp - z)
void perspective(float a[8][3])
{
for(int i=0;i<8;i++)
{
a[i][0]= (a[i][0] - mid[0][0]) * (zprp-zvp)/(zprp-a[i][2]);
a[i][1]= (a[i][1] - mid[0][1]) * (zprp-zvp)/(zprp-a[i][2]);
}
for(int i=0;i<8;i++)
{
a[i][0]= (a[i][0] + mid[0][0]);
a[i][1]= (a[i][1] + mid[0][1]);
}
}
void draw(float a[8][3])
{
for(int i=0;i<3;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[3][0],a[3][1],a[0][0],a[0][1]);
for(int i=4;i<7;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[7][0],a[7][1],a[4][0],a[4][1]);
for(int i=0;i<4;i++)
{
line(a[i][0],a[i][1],a[i+4][0],a[i+4][1]);
}
}
void init(float a[8][3])
{
a[0][0]=x1,a[0][1]=y3,a[0][2]=z1;
a[1][0]=x2,a[1][1]=y3,a[1][2]=z1;
a[2][0]=x2,a[2][1]=y2,a[2][2]=z1;
a[3][0]=x1,a[3][1]=y2,a[3][2]=z1;
a[4][0]=x1,a[4][1]=y3,a[4][2]=z2;
a[5][0]=x2,a[5][1]=y3,a[5][2]=z2;
a[6][0]=x2,a[6][1]=y2,a[6][2]=z2;
a[7][0]=x1,a[7][1]=y2,a[7][2]=z2;
}
void rotabtY(float a[8][3],float theta)
{
theta=theta*3.14/180;
float S=sin(theta),C=cos(theta);
for(int i=0;i<8;i++)
{
a2[i][0]=(a[i][0]-mid[0][0])*C+(a[i][2]-mid[0][2])*S;
a2[i][2]=-(a[i][0]-mid[0][0])*S+(a[i][2]-mid[0][2])*C;
}
for(int i=0;i<8;i++)
{
for(int j=0;j<3;j++)
{
if(j==1);
else
a[i][j]=a2[i][j]+mid[0][j];
}
}
int sumx=0,sumy=0,sumz=0;
for(int i=0;i<8;i++)
{
sumx+=a[i][0];
sumy+=a[i][1];
sumz+=a[i][2];
}
mid[0][0]=sumx/8;
mid[0][1]=sumy/8;
mid[0][2]=sumz/8;
cleardevice();
}
void rotabtX(float a[8][3],float theta)
{
theta=theta*3.14/180;
float S=sin(theta),C=cos(theta);
for(int i=0;i<8;i++)
{
a2[i][1]=(a[i][1]-mid[0][1])*C-(a[i][2]-mid[0][2])*S;
a2[i][2]=(a[i][1]-mid[0][1])*S+(a[i][2]-mid[0][2])*C;
}
for(int i=0;i<8;i++)
{
for(int j=1;j<3;j++)
a[i][j]=a2[i][j]+mid[0][j];
}
int sumx=0,sumy=0,sumz=0;
for(int i=0;i<8;i++)
{
sumx+=a[i][0];
sumy+=a[i][1];
sumz+=a[i][2];
}
mid[0][0]=sumx/8;
mid[0][1]=sumy/8;
mid[0][2]=sumz/8;
cleardevice();
}
int main()
{
initwindow(800,600);
init(a);
draw(a);
cout<<"\nenter z-coordinate for perspective ref point: ";
cin>>zprp;
cout<<"\nenter z-coordinate for view plane: ";
cin>>zvp;
//perspective
outtext("Perspective Projection");
perspective(a);
delay(1000);
draw(a);
rotabtX(a,30);
rotabtY(a,30);
delay(1000);
cleardevice();
outtext("Perspective Projection After Rotation");
draw(a);
getch();
return 0;
}
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);
}
int main()
{
initwindow(800,600);
cout<<"Enter the no. of vertices of clipping window : ";
int n;
cin>>n;
point pol[10];
cout<<"Enter the vertices in clockwise order \n";
int i;
for(i=0;i<n;i++)
{
cout<<" Enter vertex : ";
cin>>pol[i].x>>pol[i].y;
}
cout<<"Enter the end points of the line : ";
point p1,p2;
cin>>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;
}
clip(pol,p1,p2,n);
getch();
return 0;
}
p2=t;
}
midpt(wmin,wmax,p1,p2);
}
}
}
}
int main()
{
initwindow(800,600);
point wmin,wmax,p1,p2;
cout<<" Enter the clipping window limits : Xwmin
cin>>wmin.x;
cout<<" Enter the clipping window limits : Ywmin
cin>>wmin.y;
cout<<" Enter the clipping window limits : Xwmax
cin>>wmax.x;
cout<<" Enter the clipping window limits : Ywmax
cin>>wmax.y;
cout<<"\n Enter the end points of the line : P1x
cin>>p1.x;
cout<<"\n Enter the end points of the line : P1y
cin>>p1.y;
cout<<"\n Enter the end points of the line : P2x
cin>>p2.x;
cout<<"\n Enter the end points of the line : P2y
cin>>p2.y;
setcolor(WHITE);
rectangle(wmin.x,wmin.y,wmax.x,wmax.y);
setcolor(YELLOW);
line(p1.x,p1.y,p2.x,p2.y);
getch();
midpt(wmin,wmax,p1,p2);
getch();
return 0;
}
";
";
";
";
";
";
";
";
intensepix(x,y,t*id);
intensepix(x,y+1,di-t*id);
intensepix(x,y-1,di+t*id);
}
getch();
}
Hermite Curve
#include<conio.h>
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
int main()
{
float p0x,p1x,p0y,p1y;
float dp0x,dp1x,dp0y,dp1y;
float Pux,Puy;
initwindow(800,600);
cout<<"Enter p0x,p0y,p1x,p1y: ";
cin>>p0x>>p0y>>p1x>>p1y;
cout<<"Enter dp0x,dp0y,dp1x,dp1y: ";
cin>>dp0x>>dp0y>>dp1x>>dp1y;
float u=0;
while(u<=1)
{
Pux = p0x*(2*pow(u,3)-3*pow(u,2)+1) + p1x*(-2*pow(u,3)+3*pow(u,2)) + dp0x*(pow(u,3)-2*pow(u,2)+u) +
dp1x*(pow(u,3)-pow(u,2));
Puy = p0y*(2*pow(u,3)-3*pow(u,2)+1) + p1y*(-2*pow(u,3)+3*pow(u,2)) + dp0y*(pow(u,3)-2*pow(u,2)+u) +
dp1y*(pow(u,3)-pow(u,2));
putpixel(Pux,Puy,WHITE);
cout<<"\n"<<Pux<<" "<<Puy;
u=u+0.001;
}
getch();
return 0;
}
Bezier Curve
#include<iostream>
#include<conio.h>
#include<graphics.h>
#include<math.h>
using namespace std;
int fact(int n)
{
if(n<=1)
return 1;
else
return n*fact(n-1);
}
float bez(int k,int n,float u)
{
return fact(n) / (fact(k)*fact(n-k)) * pow(u,k) * pow((1-u),n-k);
}
int main()
{
float px[100],py[100],n;
float outx,outy;
initwindow(800,600);
cout<<"enter total no. of control points: ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"enter px,py: ";
cin>>px[i]>>py[i];
}
float u=0;
while(u<=1)
{
outx=0,outy=0;
for(int k=0;k<n;k++)
{
outx += px[k]*bez(k,n-1,u);
outy += py[k]*bez(k,n-1,u);
}
cout<<"\n"<<outx<<" "<<outy;
putpixel(outx,outy,RED);
u+=0.001;
}
getch();
return 0;
}
int ay = int(((cl[1].x-s[j].x)*(s[j+1].y-s[j].y)/(s[j+1].x-s[j].x)*1.0)+s[j].y);
t[k].x = cl[1].x;
t[k].y = ay;
k++;
t[k] = s[j+1];
k++;
}
//i -> o
else if(s[j].x<cl[1].x && s[j+1].x>cl[1].x)
{
int ay = int(((cl[1].x-s[j].x)*(s[j+1].y-s[j].y)/(s[j+1].x-s[j].x)*1.0)+s[j].y);
t[k].x = cl[1].x;
t[k].y = ay;
k++;
}
//i -> i
else if(s[j].x<cl[1].x && s[j+1].x<cl[1].x)
{
t[k] = s[j+1];
k++;
}
//o -> o => do nothing
}
}
else if(i==2)
// BOTTOM EDGE
{
for(int j=0;j<ns;j++)
{
// o -> i
if(s[j].y>cl[2].y && s[j+1].y<cl[2].y)
{
//find point of intersection
int ax = int(((cl[2].y-s[j].y)*(s[j+1].x-s[j].x)/(s[j+1].y-s[j].y)*1.0)+s[j].x);
t[k].x = ax;
t[k].y = cl[2].y;
k++;
t[k] = s[j+1];
k++;
}
//i -> o
else if(s[j].y<cl[2].y && s[j+1].y>cl[2].y)
{
int ax = int(((cl[2].y-s[j].y)*(s[j+1].x-s[j].x)/(s[j+1].y-s[j].y)*1.0)+s[j].x);
t[k].x = ax;
t[k].y = cl[2].y;
k++;
}
//i -> i
else if(s[j].y<cl[2].y && s[j+1].y<cl[2].y)
{
t[k] = s[j+1];
k++;
}
//o -> o => do nothing
}
}
else if(i==3) // LEFT EDGE
{
for(int j=0;j<ns;j++)
{
// o -> i
if(s[j].x<cl[0].x && s[j+1].x>cl[0].x)
{
//find point of intersection
int ay = int(((cl[0].x-s[j].x)*(s[j+1].y-s[j].y)/(s[j+1].x-s[j].x)*1.0)+s[j].y);
t[k].x = cl[0].x;
t[k].y = ay;
k++;
t[k] = s[j+1];
k++;
}
//i -> o
else if(s[j].x>cl[0].x && s[j+1].x<cl[0].x)
{
int ay = int(((cl[0].x-s[j].x)*(s[j+1].y-s[j].y)/(s[j+1].x-s[j].x)*1.0)+s[j].y);
t[k].x = cl[0].x;
t[k].y = ay;
k++;
}
//i -> i
else if(s[j].x>cl[0].x && s[j+1].x>cl[0].x)
{
t[k] = s[j+1];
k++;
}
//o -> o => do nothing
}
}
t[k]=t[0];
for(int l=0;l<=k;l++)
{
s[l]=t[l];
}
return k;
}
void suthodg( point cl[], int nc, point s[], int ns)
{
for(int i=0;i<nc;i++)
{
ns = createlist(i, cl, nc, s, ns);
}
setcolor(GREEN);
for(int i=0;i<ns;i++)
{
line(s[i].x,s[i].y,s[i+1].x,s[i+1].y);
}
}
int main()
{
point cl[4];
point sub[10], dup_sub[10];
int nc;
cout<<" Clipping Polygon ";
//min is top left and max is bottom right
cout<<"\n Enter the co-ordinates (x,y) ";
cout<<"\n Xwmin : ";
cin>>cl[0].x;
cout<<"\n Ywmin : ";
cin>>cl[0].y;
cout<<"\n Xwmax : ";
cin>>cl[2].x;
cout<<"\n Ywmax : ";
cin>>cl[2].y;
cl[1].x = cl[2].x;
cl[1].y = cl[0].y;
cl[3].x = cl[0].x;
cl[3].y = cl[2].y;
int ns;
cout<<" Subject Polygon ";
do
{
cout<<"\n Enter the no. of vertices : ";
cin>>ns;
}while(ns>10);
cout<<"\n Enter the co-ordinates in clockwise order (x,y) ";
int i;
for( i=0;i<ns;i++)
{
cout <<i+1<<". ";
cin>>sub[i].x>>sub[i].y;
dup_sub[i] = sub[i];
}
sub[i] = sub[0];
dup_sub[i] = sub[0];
initwindow(800,600);
setcolor(RED);
for(i=0;i<3;i++)
{
line(cl[i].x, cl[i].y, cl[i+1].x, cl[i+1].y);
}
line( cl[3].x, cl[3].y, cl[0].x, cl[0].y);
setcolor(YELLOW);
for(i=0;i<ns;i++)
{
line(sub[i].x,sub[i].y,sub[i+1].x,sub[i+1].y);
}
getch();
suthodg(cl,4,dup_sub,ns);
getch();
return 0;
}