19BCM032 - CG Record

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:1
19BCM032 - KIRUPPA SRI S - CG RECORD

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

#include<math.h>

void main()

int gd=DETECT,gm;

int x1,y1,x2,y2,dx,dy,x,y,i,length;

float xinc,yinc;

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

printf(“Enter starting coordinates : “);

scanf(“%d%d”,&x1,&y1);

printf(“Enter Ending coordinates : “);

scanf(“%d%d”,&x2,&y2);

dx=x2-x1;

dy=y2-y1;

if(abs(dx)>abs(dy))

length=abs(dx);

else

length=abs(dy);

xinc=dx/(float)length;

yinc=dy/(float)length;

x=x1;

y=y1;

putpixel(x,y,10);

for(i=0;i<length;i++)

{
19BCM032 - KIRUPPA SRI S - CG RECORD

x=x+xinc;

y=y+yinc;

putpixel(x,y,10);

delay(10);

getch();

closegraph();

OUTPUT:
19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:2
19BCM032 - KIRUPPA SRI S - CG RECORD

#include<graphics.h>

#include<conio.h>

#include<stdio.h>

void main()

int gd=DETECT,gm;

int x,y,x_mid,y_mid,radius,dp;

clrscr();

initgraph(&gd,&gm,”C:\\TURBOC3\\BGI”);

printf(“******MID POINT CIRCLE ALGORITHM******\n\n”);

printf(“\nEnter the co-ordinates= “);

scanf(“%d%d”,&x_mid,&y_mid);

printf(“\n Now enter the radius= “);

scanf(“%d”,&radius);

x=0;

y=radius;

dp=1-radius;

do

putpixel(x_mid+x,y_mid+y,YELLOW);

putpixel(x_mid+y,y_mid+x,YELLOW);

putpixel(x_mid-y,y_mid+x,YELLOW);

putpixel(x_mid-x,y_mid+y,YELLOW);

putpixel(x_mid-x,y_mid-y,YELLOW);

putpixel(x_mid-y,y_mid-x,YELLOW);

putpixel(x_mid+y,y_mid-x,YELLOW);

putpixel(x_mid+x,y_mid-y,YELLOW);
19BCM032 - KIRUPPA SRI S - CG RECORD

if(dp<0)

dp+=(2*x)+1;

else

y=y-1;

dp+=(2*x)-(2*y)+1;

x=x+1;

}while(y>x);

getch();

OUTPUT:
19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:3
19BCM032 - KIRUPPA SRI S - CG RECORD
19BCM032 - KIRUPPA SRI S - CG RECORD

#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>

void main()
{
clrscr();
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c,sx,sy,tx,ty;
float t,r;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
printf("\t******BASIC 2D TRANSLATIONS******");
printf("\n\nEnter the points of triangle : \n");
setcolor(30);
scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();

do
{
printf("\n\n1.Translation\n2.Rotation\n3.Scalling\n4.Exit");
printf("\n\nEnter your choice : ");
scanf("%d",&c);

switch(c)
{

case 1:
{
printf(“\n\n------TRANSLATION------“);
printf("\n Enter the translational factor tx,ty : ");
scanf("%d%d",&tx,&ty);
nx1=x1+tx;
ny1=y1+ty;
nx2=x2+tx;
ny2=y2+ty;
nx3=x3+tx;
ny3=y3+ty;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
break;
}
case 2:
{
printf(“\n\n------ROTATION------“);
printf("\n\nEnter the angle of rotation : ");
scanf("%f",&r);
cleardevice();
19BCM032 - KIRUPPA SRI S - CG RECORD

t=3.14*(r/180);
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(y1*cos(t)+x1*sin(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(y2*cos(t)+x2*sint));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(y3*cos(t)+x3*sin(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
break;
}

case 3:
{
printf(“------SCALLING------“);
printf("\n\nEnter the scalling factor sx,sy : ");
scanf("%d%d",&sx,&sy);
cleardevice();
nx1=x1*sx;
ny1=y2*sy;
nx2=x2*sx;
ny2=y2*sy;
nx3=x3*sx;
ny3=y3*sy;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
break;
}

case 4:
break;

default:
printf("Enter the correct choice : ");
}
}while(c<4);
closegraph();
}
19BCM032 - KIRUPPA SRI S - CG RECORD

OUTPUT:
19BCM032 - KIRUPPA SRI S - CG RECORD
19BCM032 - KIRUPPA SRI S - CG RECORD
19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:4
19BCM032 - KIRUPPA SRI S - CG RECORD

#include<conio.h>
#include<iostream.h>
#include<graphics.h>

static int LEFT=1,RIGHT=2,BOTTOM=4,TOP=8,xl,yl,xh,yh;

int getcode(int x,int y)

{
int code = 0;
//Perform Bitwise OR to get outcode
if(y > yh) code |=TOP;
if(y < yl) code |=BOTTOM;
if(x < xl) code |=LEFT;
if(x > xh) code |=RIGHT;
return code;
}

void main()
{
int gdriver = DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TC\\BGI");
setcolor(BLUE);
cout<<"Enter bottom left and top right co-ordinates of window: ";
cin>>xl>>yl>>xh>>yh;
rectangle(xl,yl,xh,yh);
int x1,y1,x2,y2;
cout<<"Enter the endpoints of the line: ";
cin>>x1>>y1>>x2>>y2;
line(x1,y1,x2,y2);
getch();

int outcode1=getcode(x1,y1), outcode2=getcode(x2,y2);


int accept = 0; //decides if line is to be drawn
while(1){
float m =(float)(y2-y1)/(x2-x1);
//Both points inside. Accept line
if(outcode1==0 && outcode2==0)
{
accept = 1;
break;
}
//AND of both codes != 0.Line is outside. Reject line
else if((outcode1 & outcode2)!=0)
{
break;
19BCM032 - KIRUPPA SRI S - CG RECORD

}
else
{
int x,y;
int temp;
//Decide if point1 is inside, if not, calculate intersection
if(outcode1==0)
temp = outcode2;
else
temp = outcode1;

if(temp & TOP) //Line clips top edge


{
x = x1+ (yh-y1)/m;
y = yh;
}
else if(temp & BOTTOM) //Line clips bottom edge
{
x = x1+ (yl-y1)/m;
y = yl;
}
else if(temp & LEFT) //Line clips left edge
{
x = xl;
y = y1+ m*(xl-x1);
}
else if(temp & RIGHT) //Line clips right edge
{
x = xh;
y = y1+ m*(xh-x1);
}
//Check which point we had selected earlier as temp, and replace its co-ordinates
if(temp == outcode1)
{
x1 = x;
y1 = y;
outcode1 = getcode(x1,y1);
}
else
{
x2 = x;
y2 = y;
outcode2 = getcode(x2,y2);
}
}
}
19BCM032 - KIRUPPA SRI S - CG RECORD

setcolor(WHITE);
cout<<"After clipping:";
if(accept)
line(x1,y1,x2,y2);
getch();
closegraph();
}

OUTPUT:
19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:5
19BCM032 - KIRUPPA SRI S - CG RECORD

OUTPUT:
19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:7
19BCM032 - KIRUPPA SRI S - CG RECORD

OUTPUT:
19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:8
19BCM032 - KIRUPPA SRI S - CG RECORD

OUTPUT:
19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:9
19BCM032 - KIRUPPA SRI S - CG RECORD

OUTPUT:
19BCM032 - KIRUPPA SRI S - CG RECORD

Ex.No:10
19BCM032 - KIRUPPA SRI S - CG RECORD

OUTPUT:

You might also like