Sie sind auf Seite 1von 87

COMPUTER

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;

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 + dy;
}
else
{
if(x1>x2)
{
if(y1>y2)
y++;
else
y--;
}
else
{
if(y1>y2)
y--;
else
y++;
}
p = p + (dy - dx);
}
delay(3);
putpixel(x, y, color);
}
}
if(m>1)
{
p = dx - dy/2;
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 + 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;

float rySq = ry * ry;


float x = 0, y = ry, p;
float px=0, py = 4 * rxSq * y;
//drawEllipse(xc, yc, x, y, 3);
p = rySq - (rxSq * ry) + (0.25 * rxSq);
while (px < py)
{
x++;
px = 2 * (2 * rySq * x + 3*rySq);
if (p < 0)
p = p + px;
else
{
y--;
py = 2 * (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 * (2 * rxSq * y - 3 * rxSq);
if (p > 0)
p = p - py;
else
{
x++;
px= 2 * (2 * rySq * x + 2 * rySq);
p = p - py + px;
}
drawEllipse(xc, yc, x, y, GREEN);
delay(5);
}
}
void drawEllipse(float xc, float yc, float x, float y,int color)
{
putpixel(xc+x, yc+y, color);
putpixel(xc-x, yc+y, color);
putpixel(xc+x, yc-y, color);
putpixel(xc-x, yc-y, color);
}

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);
}
}

void hyperbolaBresenham(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=2*(rySq*(x+0.5)*(x+0.5)- rxSq -rxSq*rySq);
while(py<=px)
{
if(p>=0)
{
p = p - 2*rxSq*(2*y-3);
}
else
{
p = p - 2*rxSq*(2*y+3) + 2*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);
while(y<ry)
{
x++;
if(p>=0)
{
p = p - 2*rySq*(2*x+3);
}
else
{
p = p + 2*rxSq*(2*y-2) - 2*rySq*(2*x+3);
y++;
}
drawHyperbola(xc, yc, x, y, GREEN);
}
}
void drawHyperbola(float xc, float yc, float x, float y,int color)
{
putpixel(xc+x, yc+y, color);
putpixel(xc-x, yc+y, color);
putpixel(xc+x, yc-y, color);
putpixel(xc-x, yc-y, color);
}

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);
}

void parabolaMidpoint(float xc, float yc, float a)


{
double x=0,y=0;
double d1;
d1 = (2*a) - 1;
drawParabola(x,y,GREEN);
while(y <= (2*a))
{
if(d1<0)
{
d1+= 4*a-3-2*y;
x++;
}
else
{
d1-= 3 + 2*y;
}
y++;
drawParabola(x,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,y,GREEN);
}
}

void parabolaBresenham(float xc, float yc, float a)


{
double x=0,y=0;

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);
}
}

Draw With Curve Equations


#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#define HEIGHT 600
#define WIDTH 800
#define PI 3.14159265
void drawline()
{
int x1=100,y1=200,x2=200,y2=100;
int m=(y2-y1)/(x2-x1);
int c=y1-m*x1;
int x,y;
if(m>1 || m<-1)
{
if(y1>y2)
{
int temp;
temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
}
y=y1;
while(y<=y2)
{
x=(y-c)/m;
putpixel(x,y,2);
y++;
}
}
else
{
if(x1>x2)
{
int temp;
temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
}
while(x<=x2)
{
y=m*x+c;
putpixel(x,y,2);
x++;
}
}

}
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;

Cohen Sutherland Clipping


#include<conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20
enum { TOP = 0x1, BOTTOM = 0x2, RIGHT = 0x4, LEFT = 0x8 };
typedef unsigned int outcode;
outcode compute_outcode(int x, int y,
int xmin, int ymin, int xmax, int ymax)
{
outcode oc = 0;
if (y > ymax)
oc |= TOP;
else if (y < ymin)
oc |= BOTTOM;

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 outcode_ex = outcode1 ? outcode1 : outcode2;


if (outcode_ex & TOP)
{
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
}
else if (outcode_ex & BOTTOM)
{
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1);
y = ymin;
}
else if (outcode_ex & RIGHT)
{
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1);
x = xmax;
}
else
{
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1);
x = xmin;
}
if (outcode_ex == outcode1)
{
x1 = x;
y1 = y;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
}
else
{
x2 = x;
y2 = y;
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
}
}
} while (done == FALSE);
if (accept == TRUE)
{
setcolor(RED);
line (x1, y1, x2, y2);
}
}

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);

printf ("Enter the x- and y-coordinates of the line-endpoints:\n");


for (i=0; i<n; i++)
for (j=0; j<4; j++)
scanf ("%d", &ln[i][j]);
printf ("Enter the x- and y-coordinates of the left-top and right-");
printf ("bottom corners\nof the clip window:\n");
for (i=0; i<4; i++)
scanf ("%d", &clip[i]);
initgraph (&gd, &gm, "..//bgi");
rectangle (clip[0], clip[1], clip[2], clip[3]);
for (i=0; i<n; i++)
line (ln[i][0], ln[i][1], ln[i][2], ln[i][3]);
getch();

rectangle (clip[0], clip[1], clip[2], clip[3]);


for (i=0; i<n; i++)
{
cohen_sutherland (ln[i][0], ln[i][1], ln[i][2], ln[i][3],
clip[0], clip[1], clip[2], clip[3]);
getch();
}
closegraph();
return 0;
}

Liang Barsky Line Clipping


#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");
if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) ||
( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) ||
( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) ||
( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) )
{
printf("Line is rejected\n");
getch();
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(RED);
rectangle(wxmin,wymax,wxmax,wymin);
setcolor(BLUE);
line(x1,y1,x2,y2);
getch();
setcolor(WHITE);
line(x1,y1,x2,y2);
getch();
}
else
{
if( p1 != 0.0 )
{
r1 =(float) q1 /p1 ;
if( p1 < 0 )

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;
}

Orthographic Projection (Cavalier & Cabinet)


#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};
//formulae used x = x + z*L1*cos(phi)
// y= y + z*L1* sin(phi)
// L1= 1/tan(alpha)
// cavalier L1=1, cabinet L1=1/2
void cavalier30(float a[8][3])
{
float phi=30;
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);
a[i][1]= (a[i][1] - mid[0][1]) + (a[i][2] - mid[0][2])*sin(3.14*phi/180);
}
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 cavalier45(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);
a[i][1]= (a[i][1] - mid[0][1]) + (a[i][2] - mid[0][2])*sin(3.14*phi/180);
}
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 cabinet30(float a[8][3])
{
float phi=30;
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 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;
}

Cyrus Beck Line Clipping


#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-1;i++)
{
line(pol[i].x,pol[i].y,pol[i+1].x,pol[i+1].y);
}
line(pol[n-1].x,pol[n-1].y,pol[0].x,pol[0].y);
setcolor(WHITE);
line(p1.x,p1.y,p2.x,p2.y);
getch();
float t_enter=0,t_leave=1;
for(int 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*(p2.y-p1.y);
float t;
if(den!=0)
t= num/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);
}
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;
}

Mid-Point Subdivision Line Clipping


#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#define LEFT 0x01
#define RIGHT 0x4
#define BOTTOM 0x2
#define TOP 0x8
struct point
{
float x,y;
};
char getcode(point p, point wmin, point wmax)
{
unsigned char code = 0x00;
if(p.x<wmin.x)
code = code|LEFT;
if(p.x>wmax.x)
code = code|RIGHT;
if(p.y>wmax.y)
code = code|BOTTOM;
if(p.y<wmin.y)
code = code|TOP;
return code;
}
int isin(point p, point wmin, point wmax)
{
char stcode =0x00;
char cod = getcode(p,wmin,wmax);
if(((cod&stcode)==0) && ((cod|stcode)==0))
return 1;
else
return 0;
}
point mid( point p1, point p2)
{
point m;
m.x = (p1.x+p2.x)/2; // calculate mid point of the line segment
m.y = (p1.y+p2.y)/2;
return m;
}
float dist( point t1, point t2, point wmin, point wmax)
{
float dsx = t1.x - t2.x;
float dsy = t1.y - t2.y;
float ds = dsx>dsy?dsx:dsy;
return ds;
}

void midpt(point wmin, point wmax, point p1, point p2)


{
setcolor(GREEN);
point t;
if (p1.x > p2.x)
{
t = p1;
p1 = p2;
p2 = t;
}
char code1 = getcode(p1,wmin,wmax);
char code2 = getcode(p2,wmin,wmax);
if((code1 & code2)!=0)
{
return;
}
else if(((code1 & code2)==0) && ((code1 | code2)==0))
{
line(p1.x, p1.y, p2.x, p2.y);
}
else
{
t = mid(p1,p2);
if((dist(t,p1,wmin,wmax) >= 1) || (dist(t,p2,wmin,wmax) >= 1))
{
char codm = getcode(t, wmin, wmax);
if(isin(t,wmin,wmax))
{
if(isin(p1,wmin,wmax))
{
line(p1.x,p1.y,t.x,t.y);
p1=t;
midpt(wmin,wmax,p1,p2);
}
else if(isin(p2,wmin,wmax))
{
line(p2.x,p2.y,t.x,t.y);
p2=t;
midpt(wmin,wmax,p1,p2);
}
else
{
midpt(wmin,wmax,p1,t);
midpt(wmin,wmax,t,p2);
}
}
else
{
if ((code1&codm)!=0)
{
p1=t;
}
else if((code2&codm)!=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;
}

";
";
";
";

";
";
";
";

Antialiasing Gupta Spruolls Approach


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
#include<iostream.h>
void intensepix(float x,float y,double d)
{
int in;
if(d==0.0)
in=15;
if((d<.4)&&(d>0))
in=7;
if((d<=1.0)&&(d>=.4))
in=8;
if(d>1)
in=0;
putpixel(x+100,y,in);
}
int main(void)
{
initwindow(800,600);
float x1,x2,y1,y2,x,y,dx,dy,d,t;
double id,di;
printf("enter the end points of line");
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
dx=x2-x1,dy=y2-y1;
id=1/(2*pow((dx*dx+dy*dy),0.5));
di=2*dx*id;
d=2*dy-dx;
x=x1;
y=y1;
getch();
intensepix(x,y,0);
intensepix(x,y+1,di);
intensepix(x,y-1,di);
while(x<x2)
{
if(d<0)
{
t=d+dx;
d+=2*dy;
x++;
}
else
{
t=d-dx;
d+=2*(dy-dx);
x++,y++;
}

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;
}

Sutherland Hodgeman Polygon Clipping


#include<iostream>
#include<conio.h>
#include<graphics.h>
using namespace std;
struct point
{
int x,y;
};
int createlist(int i, point cl[], int nc, point s[], int ns)
{
point t[20];
int k=0;
if(i==0) // TOP EDGE
{
for(int j=0;j<ns;j++)
{
// o -> i
if(s[j].y<cl[0].y && s[j+1].y>cl[0].y)
{
//find point of intersection
int ax = int(((cl[0].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[0].y;
k++;
t[k] = s[j+1];
k++;
}
//i -> o
else if(s[j].y>cl[0].y && s[j+1].y<cl[0].y)
{
int ax = int(((cl[0].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[0].y;
k++;
}
//i -> i
else if(s[j].y>cl[0].y && s[j+1].y>cl[0].y)
{
t[k] = s[j+1];
k++;
}
//o -> o => do nothing
}
}
else if(i==1)
// RIGHT EDGE
{
for(int j=0;j<ns;j++)
{
// o -> i
if(s[j].x>cl[1].x && s[j+1].x<cl[1].x)
{
//find point of intersection

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;
}

Das könnte Ihnen auch gefallen