Computer Graphics Combined
Computer Graphics Combined
Algorithm:
Step 1:
Read the input of the two end points of the line as (x1, y1) & (x2, y2) such that
x1 != x2 and y1 != y2
Step 2:
Calculate dx = x2 – x1 and dy = y2 – y1
Step 3:
if(dx>=dy)
step=dx
else
step=dy
Step 6:
for(k = 0; k < step; k++)
{
x = x + xin
y = y + yin
putpixel(x, y)
}
Code:
#include <graphics.h>
#include <stdio.h>
#include<conio.h>
#include <math.h>
#include <dos.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the value of x1 and y1 : ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
step=dx;
else
step=dy;
dx=dx/step;
dy=dy/step;
x=x1;
y=y1;
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
getch();
closegraph();
}
Output:
Program-01(b)
Algorithm:
Step 1:
Read the input from keyboard for circle center and radius in (x c,yc) and r
Step 2:
Calculate the initial value of p=1-rc. Plot the first point on circle circumference
as x=0 and y=rc
Step 3:
while (x<y)
x=x+1
if(p<0)
then p=p+2*x+1
else
y=y-1
p=p+2*(x-y)+1
Step 4:
Plot the circle with calculated x and y value as plot(x c,yc,x,y)
Step5:
End the algorithm
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void main()
{
int xc,yc,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\TURBOC3\\BGI");
printf("Enter the center coordinates and radius\n");
scanf("%d%d%d",&xc,&yc,&r);
setbkcolor(BLACK);
setcolor(WHITE);
circl(xc,yc,r);
getch();
closegraph();
}
Output:
Program-02(a)
Algorithm:
Step1:
Input the two line endpoints and store the left endpoint in (x0,y0)
Step2:
Load (x0,y0) into frame buffer, that is, plot the first point.
Step3:
Calculate constants Δx, Δy,2Δy and 2Δy-2Δx, and obtain the starting value for
decision parameter as:
P0=2Δy-Δx
Step4:
At each xk along the line, starting at k=0, perform the following test:
If pk<0, the next point to plot is (xk+1,yk) and
Pk+1=pk+2Δy
Otherwise, the next point to plot is (xk+1,yk+1)
Pk+1=pk+2Δy-2 Δx
Step5:
Repeat step 4 Δx times
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
dx=x1-x0;
dy=y1-y0;
x=x0;
y=y0;
p=2*dy-dx;
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
Algorithm:
Step 1
Get the coordinates of the center of the circle and radius, and store them in x,
y, and R respectively. Set P=0 and Q=R.
Step 2
Set decision parameter D = 3 – 2R.
Step 3
Repeat through step-8 while P ≤ Q.
Step 4
Call Draw Circle (X, Y, P, Q).
Step 5
Increment the value of P.
Step 6
If D < 0 then D = D + 4P + 6.
Step 7
Else Set R = R - 1, D = D + 4(P-Q) + 10.
Step 8
Call Draw Circle (X, Y, P, Q).
Code:
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#include <graphics.h>
int main()
{
int xc, yc, r ;
printf("Enter the coordinate center of circle\n");
scanf("%d\n",&xc);
scanf("%d\n",&yc);
printf("Enter the radius of circle\n");
scanf("%d",&r);
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
circleBres(xc, yc, r);
getch();
return 0;
}
Output:
Program-03
Algorithm:
Step1:
Input radius r and circle center (xc,yc), and obtain the first point on the
circumference of a circle centered on the origin as
(x0,y0)=(0,r)
Step2:
Calculate the initial value of decision parameter as
P0=5/4-r
Step3:
At each xk position,starting at k=0, perform the following test:
If pk<0, the next point along the circle centered on (0,0) is (x k+1,yk) and
pk+1=pk+2xk+1+1
Otherwise, the next point along the circle is (x k+1,yk-1) and
pk+1=pk+2xk+1+1-2yk+1
where 2xk+1=2xk+2 2yk+1=2yk-2
Step4:
Determine symmetry points in the other seven octants.
Step5:
Move each calculated pixel position (x,y) onto the circular path centered on
(xc,yc) and plot the coordinate values:
x=x+xc, y=y+yc
Step6:
Repeat steps 3 through 5 until x>=y
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
if (err <= 0)
{
y += 1;
err += 2*y + 1;
}
if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
Algorithm:
Step1:
Input rx,ry, and ellipse centered (xc,yc) and obtain the first point on an ellipse
centered on the origin as
(x0,y0)=(0,ry)
Step2:
Calculate the initial value of the decision parameter in region 1 as
P10= ry2-rx2ry+0.25rx2
Step3:
At each xk position in region 1, starting at k=0, perform the following test:
If p1k<0, the next point along the ellipse centered on (0,0) is (x k+1,yk) and
P1k+1= P1k+2ry2xk+1+ry2
Otherwise, the next point along the circle is (x k+1,yk-1) and
P1k+1= P1k+2ry2xk+1-2rx2yk+1+ry2
with
2ry2xk+1= 2ry2xk+2ry2, 2ry2yk+1= 2ry2yk-2rx2
and continue until 2ry x>=2rx y
2 2
Step4:
Calculate the initial value of the decision parameter in region 2 using the
last point (x0,y0) calculated in region 1 as
P20= ry2(x0+1/2)2-rx2(y0-1)2-rx2ry2
Step5:
At each yk position in region 2, starting at k=0, perform the following test:
If p2k<0, the next point along the ellipse centered on (0,0) is (x k,yk-1)
P2k+1= P2k-2rx2yk+1+rx2
Otherwise, the next point along the circle is (x k+1,yk-1) and
P2k+1= P2k+2ry2xk+1-2rx2yk+1+rx2
using the same incremental calculations for x and y as in region 1.
Step6:
Determine symmetry points in the other three quadrants.
Step7:
Move each calculated pixel position (x,y) onto the elliptical path centered on
(xc,yc) and plot the coordinate values:
x=x+xc , y=y+yc
Step8:
Repeat the steps for region 1 until 2r y2x>=2rx2y
Code:
#include <stdio.h>
#include <conio.h>
#include<graphics.h>
void midptellipse(int rx, int ry, int xc, int yc)
{
// For region 1
while (dx < dy) {
int main()
{ int gdriver=DETECT, gmode;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
// To draw a ellipse of major and minor radius 15, 10 centred at (50, 50)
midptellipse(10, 15, 50, 50);
getch();
return 0;
}
Output:
Program-5
Code:
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;
int sx,sy,xt,yt,r;
float t;
initgraph(&gd,&gm,"c:\turboc3\bgi:");
printf("\t Program for basic transactions");
printf("\n\t Enter the points of triangle");
setcolor(1);
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);
printf("\n 1.Transaction\n 2.Rotation\n 3.Scalling\n 4.exit");
printf("Enter your choice:");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n Enter the translation factor");
scanf("%d%d",&xt,&yt);
nx1=x1+xt;
ny1=y1+yt;
nx2=x2+xt;
ny2=y2+yt;
nx3=x3+xt;
ny3=y3+yt;
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 2:
printf("\n Enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(x1*cos(t)-y1*sin(t));
ny1=abs(x1*sin(t)+y1*cos(t));
nx2=abs(x2*cos(t)-y2*sin(t));
ny2=abs(x2*sin(t)+y2*cos(t));
nx3=abs(x3*cos(t)-y3*sin(t));
ny3=abs(x3*sin(t)+y3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
case 3:
printf("\n Enter the scalling factor");
scanf("%d%d",&sx,&sy);
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();
case 4:
break;
default:
printf("Enter the correct choice");
}
getch();
closegraph();
}
Output:
Program-6
Algorithm:
3. Assign the region codes for 2 endpoints p1 and p2 using following steps:-
initialize code with 0000
a) Set bit 1 if x<wx1
b) Set bit 2 if x>wx2
c) Set bit 3 if y<wy2
d) Set bit 4 if y>wy1
7. Reject line segment if any end point of line appears outside of any boundary.
9. Stop.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
getch();
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}
if(flag==0)
return(1);
return(2);
}
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
return(temp);
}
else
return(p1);
}
Output:
Algorithm:
5. Initialize t1 & t2 as
t1 = 0 & t2 = 1
7. Select values of qi/pi where pi < 0 and assign maximum out of them as t1.
8. Select values of qi/pi where pi > 0 and assign minimum out of them as t2.
10. Stop.
Advantages
1. More efficient than other algorithms as line intersection with boundaries
calculations are reduced.
2. Intersections of line are computed only once.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
int i,gd=DETECT,gm;
int x1,y1,x2,y2,xmin,xmax,ymin,ymax,xx1,xx2,yy1,yy2,dx,dy;
float t1,t2,p[4],q[4],temp;
x1=120;
y1=120;
x2=300;
y2=300;
xmin=100;
ymin=100;
xmax=250;
ymax=250;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
rectangle(xmin,ymin,xmax,ymax);
dx=x2-x1;
dy=y2-y1;
p[0]=-dx;
p[1]=dx;
p[2]=-dy;
p[3]=dy;
q[0]=x1-xmin;
q[1]=xmax-x1;
q[2]=y1-ymin;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{
if(p[i]==0)
{
printf("line is parallel to one of the clipping boundary");
if(q[i]>=0)
{
if(i<2)
{
if(y1<ymin)
{
y1=ymin;
}
if(y2>ymax)
{
y2=ymax;
}
line(x1,y1,x2,y2);
}
if(i>1)
{
if(x1<xmin)
{
x1=xmin;
}
if(x2>xmax)
{
x2=xmax;
}
line(x1,y1,x2,y2);
}
}
}
}
t1=0;
t2=1;
for(i=0;i<4;i++)
{
temp=q[i]/p[i];
if(p[i]<0)
{
if(t1<=temp)
t1=temp;
}
else
{
if(t2>temp)
t2=temp;
}
}
if(t1<t2)
{
xx1 = x1 + t1 * p[1];
xx2 = x1 + t2 * p[1];
yy1 = y1 + t1 * p[3];
yy2 = y1 + t2 * p[3];
line(xx1,yy1,xx2,yy2);
}
delay(5000);
getch();
closegraph();
}
Output: