View
244
Download
0
Category
Preview:
DESCRIPTION
new cg lab
Citation preview
BRESENHAMS LINE
DRAWING
ALGORITHM
AIM:
To write a C program to draw a line using Bresenham’s line drawing algorithm.
ALGORITHM:
Step1: Input the two line endpoints and store the left end point in (x0,y0).
Step2: Load (x0,y0) into the 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 the decision parameters as P0=2∆y-∆x.
Step4: At Xk along the line starting at k=0 perform the following test.
If Pk < 0, the next point to plot is (Xk+1,Xk) and
Pk+1=Pk+2∆y
Otherwise the next point is to plot is (xk+1,yk+1) and
Pk+1=Pk+2∆y-2∆x
Step5: Repeat the Step 4 for ∆x times.
§1
Ex. No.:
Date:
§1
PROGRAM:
#include<stdio.h>#include<graphics.h>#include<math.h>
void main(){
float x,y,x1,y1,x2,y2,dx,dy,e;int i,gd,gm;clrscr();printf("Enter the value of x1 & y1:");scanf("%f%f",&x1,&y1);printf("Enter the value of x2 & y2:");scanf("%f%f",&x2,&y2);detectgraph(&gd,&gm);initgraph(&gd,&gm,"");dx=abs(x2-x1);dy=abs(y2-y1);x=x1;y=y1;e=2*dy-dx;i=1;do{
putpixel(x,y,15);while(e>=0){
y=y+1;e=e-2*dx;
}x=x+1;e=e+2*dy;i=i+1;
}while(i<=dx);getch();closegraph();
}
§1
OUTPUT:
GIVE THE STARTING AND ENDING CO-ORDINATES:
ENTER X1 AND Y1: 100 100
ENTER X2 AND Y2: 200 300
§1
RESULT:
Thus the program for Bresenham’s line drawing algorithm is written and
executed and output is verified.
§1
BRESENHAMS CIRCLE DRAWING
ALGORITHM
AIM:
To write an C program to draw a circle using Bresenham’s circle drawing
algorithm.
ALGOROTHM:
Step1: Input radius r and circle center (xc,yc)and obtain the first point on the
circumference of circle centered on the origin as
(x0,y0) = (0,r)
Step2: Calculate the initial value of the 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 (xk+1,yk) and
Pk+1 = Pk+2xk+1+1
Otherwise, the next point along the circle centered on (0,0) is (xk+1,yk-1)
and
Pk+1 = Pk+2xk+1+1-2yk+1
where 2xk+1=2xk+2 and 2yk=1=2yk-2
Step4: Determine symmetry in the 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 Step3 through until x>=y.
§1
Ex. No.:
Date:
PROGRAM:
#include<stdio.h>#include<graphics.h>#include<math.h>
void main(){
float d;int gd,gm,x,y;int r;clrscr();detectgraph(&gd,&gm);initgraph(&gd,&gm,"");printf("Enter the radius of the circle:");scanf("%d",&r);x=0;y=r;d=3-2*r;do{
putpixel(200+x,200+y,15);putpixel(200+y,200+x,15);putpixel(200+y,200-x,15);putpixel(200+x,200-y,15);putpixel(200-x,200-y,15);putpixel(200-y,200-x,15);putpixel(200-y,200+x,15);putpixel(200-x,200+y,15);if(d<=1){
d=d+4*x+6;}else{
d=d+4*(x-y)+10;y=y-1;
}x=x+1;delay(10);
}while(x<y);getch();closegraph();
}
§1
OUTPUT:
ENTER THE CO-ORDINATES: 150 150
ENTER THE RADIUS: 50
§1
RESULT:
Thus the program for Bresenham’s circle drawing algorithm is executed
successfully and output is verified.
§1
BRESENHAMS ELLIPSE DRAWING
ALGRITHM
AIM:
To write a C program to draw the ellipse using Bresenham’s ellipse drawing
algorithm.
ALGORITHM:
Step1: Input rx,ry and the ellipse center (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.
P10 = ry2-rx
2ry+1/4rx2
Step3: At each x position in the region 1 starting at k=0, perform the following
test,
If P1k < 0, the next point along the ellipse centered on (0,0) is (xk+1,yk)
P1k+1 = P1k+2ry2xk+1+ry
2
continue until, 2ry2x >= 2rx
2y
Step4: Calculate the initial value of the decision parameter in region 2, using
the
last point (x0,y0) is calculated.
Step5: Calculate the initial value of the decision parameter in region 2, using
the
last point (x0,y0) is calculated.
Step6: 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
Step7: Repeat the steps for region 2 until, 2ry2x >= 2rx
2y.
§1
Ex. No.:
Date:
PROGRAM:
#include<stdio.h>#include<conio.h>#include<graphics.h>#include<math.h>
void main(){
long d1,d2;int i,gd,gm,x,y;long rx,ry,rx1,ry1,rx2,ry2,dx,dy;clrscr();printf("Enter the x & y radius of the Ellipse:");scanf("%ld%ld",&rx,&ry);detectgraph(&gd,&gm);initgraph(&gd,&gm,"");rx1=rx*rx;ry1=ry*ry;rx2=2*rx1;ry2=2*ry1;x=0;y=ry;d1=ry1-rx1*ry+(0.25*rx1);dx=ry2*x;dy=rx2*y;do{
putpixel(200+x,200+y,15);putpixel(200-x,200-y,15);putpixel(200+x,200-y,15);putpixel(200-x,200+y,15);if(d1<0){
x=x+1;y=y;dx=dx+ry2;d1=d1+dx+ry1;
}else{
x=x+1;y=y-1;dx=dx+ry2;dy=dy-rx2;d1=d1+dx-dy+ry1;
§1
}
§1
delay(20);}while(dx<dy);d2=ry1*(x+0.5)*(x+0.5)+rx1*(y-1)-rx1*ry1;do{
putpixel(200+x,200+y,15);putpixel(200-x,200+y,15);putpixel(200+x,200-y,15);putpixel(200-x,200-y,10);if(d2>0){
x=x;y=y-1;dy=dy-rx2;d2=d2-dy+rx1;
}else{
x=x+1;y=y-1;dy=dy-rx2;dx=dx+ry2;d2=d2+dx-dy+rx1;
}}while(y>0);getch();closegraph();
}
§1
OUTPUT:
ENTER THE CENTRE CO-ORDINATES : 70 70
ENTER THE LENGTH OF MAJOR AND MINOR AXIS: 30 10
§1
RESULT:
Thus the program for Bresenmham’s ellipse drawing algorithm is executed
successfully and output is verified.
§1
2D TRANSFORMATION FOR LINE
AIM:
To write a C++ algorithm to perform 2D transformations for line, such as
Transformation, Scaling, Rotation and Reflection.
ALGORITHM:
Step1: Initialize the position for basic x and y co-ordinates.
Step2: Declare the view condition for Translation, Rotation, Scaling and
Reflection.
Step3: Define the condition to display the different 2D transformation along x
and
y co-ordinates.
Step4: Display the line according to the input x and y co-ordinates.
§1
Ex. No.:
Date:
PROGRAM:
2D TRANSLATION:
#include<stdio.h>#include<conio.h>#include<iostream.h>#include<graphics.h>#include<math.h>
void rec(int,int,int,int);void rec1(int,int,int,int,int,int);void plot(int,int,int,int);void plot1(int,int,int,int,int,int);
void main(){
int x1,y1,x2,y2,tx,ty,c;int length,breadth,i,xinc,yinc;float x,y;int driver=DETECT,mode;initgraph(&driver,&mode,"");cout<<"Enter choice 1 for Line and 2 for Rectangle:";cin>>c;switch(c){
case 1:{
cout<<"Enter the Starting Points:";cin>>x1;cin>>y1;cout<<"Enter the Ending Points:";cin>>x2;cin>>y2;cout<<"Enter the Transaction Vectors:";cin>>tx>>ty;plot(x1,y1,x2,y2);getch();plot1(x1,y1,x2,y2,tx,ty);getch();break;
}case 2:{
cout<<"Enter the Staring Points:";cin>>x1;
§1
cin>>y1;cout<<"Enter the Ending Points:";cin>>x2;cin>>y2;cout<<"Enter the Transaction Vector:";cin>>tx;cin>>ty;rec(x1,y1,x2,y2);getch();rec1(x1,y1,x2,y2,tx,ty);getch();break;
}}getch();closegraph();
}
void plot(int x1,int y1,int x2,int y2){
int x,y,dx,dy,length,xinc,yinc,i;dx=abs(x2-x1);dy=abs(y2-y1);if(dx>dy)length=dx;elselength=dy;xinc=dx/length;yinc=dy/length;x=x1;y=y1;putpixel(x,y,15);x=x1+0.5;y=y1+0.5;for(i=1;i<=length;i++){
putpixel(x,y,15);x=xinc+x;y=yinc+y;
}}
void plot1(int x1,int y1,int x2,int y2,int tx,int ty){
int dx,dy,length,xinc,yinc,x,y,i;x1=x1*tx;
§1
x2=x2*tx;y1=y1*ty;y2=y2*ty;dx=abs(x2-x1);dy=abs(y2-y1);if(dx>dy){
length=dx;}else{
length=dy;}xinc=dx/length;yinc=dy/length;x=x1;y=y1;putpixel(x,y,15);x=x1+0.5;y=y1+0.5;for(i=1;i<=length;i++){
putpixel(x,y,15);x=xinc+x;y=yinc+y;
}}void rec(int x1,int y1,int x2,int y2){
int length,breadth,i,xinc,yinc;float x,y;length=abs(x2-x1);breadth=abs(y2-y1);xinc=(x2-x1)/length;yinc=(y2-y1)/length;x=x1+0.5;y=y1+0.5;for(i=1;i<=length;i++){
putpixel(x,y,15);putpixel(x,y+breadth,15);x=x+xinc;
}x=x1+0.5;y=y1+0.5;for(i=1;i<=breadth;i++)
§1
{putpixel(x,y,15);putpixel(x+length,y,15);y=y+yinc;
}}
void rec1(int x1,int y1,int x2,int y2,int tx,int ty){
int length,breadth,i,xinc,yinc;float x,y;x1+=tx;x2+=tx;y1+=ty;y2+=ty;length=abs(x2-x1);breadth=abs(y2-y1);xinc=(x2-x1)/length;yinc=(y2-y1)/length;x=x1+0.5;y=y1+0.5;for(i=1;i<=length;i++){
putpixel(x,y,15);putpixel(x,y+breadth,15);x=x+xinc;
}x=x1+0.5;y=y1+0.5;for(i=1;i<=breadth;i++){
putpixel(x,y,15);putpixel(x+length,y,15);y+=yinc;
}}
§1
OUTPUT:
ENTER THE CHOICE 1 FOR LINE AND 2 FOR RECTANGLE: 2
ENTER THE STARTING POINTS : 100 100
ENTER THE ENDING POINTS : 200 200
ENTER THE TRANSLATION VECTOR : 20 20
§1
2D ROTATION:
#include<iostream.h>#include<conio.h>#include<graphics.h>#include<math.h>#include<stdlib.h>#include<dos.h>#include<string.h>
void main(){
int x[10],y[10],z[10],a[10],b[10],theta=0,n,gm,gd=DETECT;int sx,sy,sz,not;initgraph(&gd,&gm,"");cout<<"Give the no of Sides:";cin>>n;for(int i=0;i<n;i++){
cout<<"Give the "<<i+1<<" Co-ordinate:";cin>>not;for(i=0;i<n;i++){
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);line(x[i]+z[i],y[i]+z[i],x[(i+1)%n]+z[(i+1)%n],y[(i+1)%n]+z[(i+1)%n]);line(x[i],y[i],x[i]+z[i],y[i]+z[i]);
}}getch();cleardevice();do{
theta=(theta+1)%360;float not=(theta*3.14)/180;for(i=0;i<n;i++){
a[i]=abs(x[0]+0.5+(x[0]-x[i])*cos(not)-(y[0]-y[i])*sin(not));b[i]=abs(y[0]+0.5+(x[0]-x[i])*sin(not)+(y[0]-y[i])*cos(not));
}for(i=0;i<n;i++){
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);line(x[i]+z[i],y[i]+z[i],x[(i+1)%n]+z[(i+1)%n],y[(i+1)%n]+z[(i+1)%n]);
§1
line(x[i],y[i],x[i]+z[i],y[i]+z[i]);}delay(85);putpixel(x[0],y[0],15);cleardevice();
}while(theta<=not);closegraph();getch();
}
§1
OUTPUT:
GIVE THE NUMBER OF SIDES: 3
GIVE THE 1ST CO-ORDINATE: 150 160
GIVE THE 2ND CO-ORDINATE: 100 250
GIVE THE 3RD CO-ORDINATE: 300 100
GIVE THE ANGLE : 45
§1
2D SCALING:
#include<stdio.h>#include<conio.h>#include<iostream.h>#include<graphics.h>#include<math.h>
void rec(int,int,int,int);void rec1(int,int,int,int,int,int);void plot(int,int,int,int);void plot1(int,int,int,int,int,int);
void main(){
int x1,y1,x2,y2,tx,ty,c;int length,breadth,i,xinc,yinc;float x,y;int driver=DETECT,mode;initgraph(&driver,&mode,"");cout<<"Enter choice 1 for Line and 2 for Rectangle:";cin>>c;switch(c){
case 1:{
cout<<"Enter the Starting Points:";cin>>x1;cin>>y1;cout<<"Enter the Ending Points:";cin>>x2;cin>>y2;cout<<"Enter the Transaction Vectors:";cin>>tx>>ty;plot(x1,y1,x2,y2);getch();plot1(x1,y1,x2,y2,tx,ty);getch();break;
}case 2:{
cout<<"Enter the Staring Points:";cin>>x1;cin>>y1;cout<<"Enter the Ending Points:";
§1
cin>>x2;cin>>y2;cout<<"Enter the Transaction Vector:";cin>>tx;cin>>ty;rec(x1,y1,x2,y2);getch();rec1(x1,y1,x2,y2,tx,ty);getch();break;
}}getch();closegraph();
}
void plot(int x1,int y1,int x2,int y2){
int x,y,dx,dy,length,xinc,yinc,i;dx=abs(x2-x1);dy=abs(y2-y1);if(dx>dy)length=dx;elselength=dy;xinc=dx/length;yinc=dy/length;x=x1;y=y1;putpixel(x,y,15);x=x1+0.5;y=y1+0.5;for(i=1;i<=length;i++){
putpixel(x,y,15);x=xinc+x;y=yinc+y;
}}
void plot1(int x1,int y1,int x2,int y2,int tx,int ty){
int dx,dy,length,xinc,yinc,x,y,i;x1=x1*tx;x2=x2*tx;y1=y1*ty;
§1
y2=y2*ty;dx=abs(x2-x1);dy=abs(y2-y1);if(dx>dy){
length=dx;}else{
length=dy;}xinc=dx/length;yinc=dy/length;x=x1;y=y1;putpixel(x,y,15);x=x1+0.5;y=y1+0.5;for(i=1;i<=length;i++){
putpixel(x,y,15);x=xinc+x;y=yinc+y;
}}
void rec(int x1,int y1,int x2,int y2){
int length,breadth,i,xinc,yinc;float x,y;length=abs(x2-x1);breadth=abs(y2-y1);xinc=(x2-x1)/length;yinc=(y2-y1)/length;x=x1+0.5;y=y1+0.5;for(i=1;i<=length;i++){
putpixel(x,y,15);putpixel(x,y+breadth,15);x=x+xinc;
}x=x1+0.5;y=y1+0.5;for(i=1;i<=breadth;i++){
§1
putpixel(x,y,15);putpixel(x+length,y,15);y=y+yinc;
}}
void rec1(int x1,int y1,int x2,int y2,int tx,int ty){
int length,breadth,i,xinc,yinc;float x,y;x1*=tx;x2*=tx;y1*=ty;y2*=ty;length=abs(x2-x1);breadth=abs(y2-y1);xinc=(x2-x1)/length;yinc=(y2-y1)/length;x=x1+0.5;y=y1+0.5;for(i=1;i<=length;i++){
putpixel(x,y,15);putpixel(x,y+breadth,15);x=x+xinc;
}x=x1+0.5;y=y1+0.5;for(i=1;i<=breadth;i++){
putpixel(x,y,15);putpixel(x+length,y,15);y+=yinc;
}}
§1
OUTPUT:
ENTER THE CHOICE 1 FOR LINE AND 2 FOR RECTANGLE: 2
ENTER THE STARTING POINTS : 100 100
ENTER THE ENDING POINTS : 200 200
ENTER THE SCALING POINTS : 2 2
§1
2D SHEARING:
#include<stdio.h>#include<conio.h>#include<iostream.h>#include<graphics.h>#include<math.h>
void rec(int,int,int,int);void rec1(int,int,int,int,int);void rec2(int,int,int,int,int);
void main(){
int x1,y1,x2,y2,ch,sx,xref;int driver,mode;driver=DETECT;initgraph(&driver,&mode,"");cout<<"Enter YOur Choice"<<endl;cout<<"X - Direction Shear ----> 1"<<endl;cout<<"Y - Direction Shear ----> 2"<<endl;cout<<"Both Direction Shear----> 3"<<endl;cin>>ch;switch(ch){
case 1:{
cout<<"Enter the Starting Point"<<endl;cin>>x1;cin>>y1;cout<<"Enter the Ending Point"<<endl;cin>>x2;cin>>y2;cout<<"Enther the SX value"<<endl;cin>>sx;cleardevice();line(0,320,640,320);line(320,110,320,640);rec(x1,y1,x2,y2);rec1(x1,y1,x2,y2,sx);getch();break;
}case 2:{
cout<<"Enter the Starting Point"<<endl;
§1
cin>>x1;cin>>y1;cout<<"Enter the Ending Point"<<endl;cin>>x2;cin>>y2;cout<<"Enther the SX value"<<endl;cin>>sx;cleardevice();line(0,320,640,320);line(320,0,320,640);rec(x1,y1,x2,y2);rec2(x1,y1,x2,y2,sx);getch();break;
}case 3:{
cout<<"Enter the Starting Point"<<endl;cin>>x1;cin>>y1;cout<<"Enter the Ending Point"<<endl;cin>>x2;cin>>y2;cout<<"Enther the SX value"<<endl;cin>>sx;cleardevice();line(0,320,640,320);line(320,0,320,640);rec(x1,y1,x2,y2);rec1(x1,y1,x2,y2,sx);getch();break;
}}
}
void rec(int x1,int y1,int x2, int y2){
int l,b,i,xinc,yinc;float x,y;l=abs(x2-x1);b=abs(y2-y1);xinc=(x2-x1)/l;yinc=(y2-y1)/l;x=x1+0.5;y=y1+0.5;
§1
for(i=1;i<=l;i++){
putpixel(x,y,15);putpixel(x1,y+l,15);x=x+xinc;
}x=x1+0.5;y=y1+0.5;for(i=1;i<=b;i++){
putpixel(x,y,15);putpixel(x1+l,y,15);y=y+yinc;
}}
void rec1(int x1,int y1,int x2,int y2,int sx){
int xr,yr,xr1,yr1,xr2,yr2,xr3,yr3;xr=x1+sx*y1;yr=y1;xr1=x2+sx*y1;yr1=y1;xr2=x1;yr2=y2;xr3=x2;yr3=x2;line(xr1,yr1,xr3,yr3);line(xr3,yr3,xr2,yr2);line(xr,yr,xr1,yr1);line(xr2,yr2,xr,yr);
}
void rec2(int x1,int y1,int x2,int y2,int sy){
int xr,yr,xr1,yr1,xr2,yr2,xr3,yr3;xr=x1;yr=y1+sy*x1;xr1=x1;yr1=y1+sy*x1;xr2=x2;yr2=y1;xr3=x2;yr3=y2;line(yr1,xr1,yr2,xr2);line(yr2,xr2,yr,xr);
§1
line(yr,xr,yr3,xr3);line(yr3,xr3,yr2,xr2);
}
§1
OUTPUT:
ENTER YOUR CHOICE
X – DIRECTION SHEAR 1
Y – DIRECTION SHEAR 2
BOTH DIRECTION SHEAR 3
3
ENTER THE STARTING POINTS : 100 100
ENTER THE ENDING POINTS : 200 200
ENTER THE SX VALUE : 2
ENTER THE XREF VALUE : 4
§1
RESULT:
Thus the program for 2D transformation for line is executed successfully and
output is verified.
§1
3D TRANSFORMATION
AIM:
To write a C++ program to perform the 3D transformations.
ALGORITHM:
Step1: Initialize basic position of object along top, bottom and depth
respectively.
Step2: Define view condition for 3D transformations such as Translation,
Rotation and Scaling.
Step3: Include the condition for size of top, bottom and depth of an object.
Step4: Display the object in 3D transformation view.
§1
Ex. No.:
Date:
PROGRAM:
3D TRANSLATION:
#include<iostream.h>#include<conio.h>#include<string.h>#include<stdio.h>#include<dos.h>#include<graphics.h>
void dev(int x[],int y[],int z[],int n,int tx,int ty,int tz){
cleardevice();for(int i=0;i<n;i++){
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);line(x[i]+z[i],y[i]+z[i],x[(i+1)%n]+z[(i+1)%n],y[(i+1)%n]+z[(i+1)%n]);line(x[i],y[i],x[i]+z[i],y[i]+z[i]);
}getch();for(i=0;i<n;i++){
x[i]=x[i]+tx;y[i]=y[i]+ty;z[i]=z[i]+tz;
}for(i=0;i<n;i++){
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);line(x[i]+z[i],y[i]+z[i],x[(i+1)%n]+z[(i+1)%n],y[(i+1)%n]+z[(i+1)%n]);line(x[i],y[i],x[i]+z[i],y[i]+z[i]);
}}
void main(){
int x[10],y[10],z[10],n,gm,gd=DETECT,tx,ty,tz;initgraph(&gd,&gm,"");cout<<"Give the no. of Sides:";cin>>n;for(int i=0;i<n;i++){
cout<<"\n Give the "<<i+1<<" co-ordinate: ";
§1
cin>>x[i]>>y[i]>>z[i];}
cout<<"\n Enter the Translation Vector:";cin>>tx>>ty>>tz;dev(x,y,z,n,tx,ty,tz);getch();closegraph();
}
§1
OUTPUT:
GIVE THE NUMBER OF SIDES : 4
GIVE THE 1 CO-ORDINATE : 100 100 130
GIVE THE 2 CO-ORDINATE : 100 100 130
GIVE THE 3 CO-ORDINATE : 100 100 130
GIVE THE 4 CO-ORDINATE : 100 100 130
ENTER THE TRANSLATION VECTORS : 90 20 40
§1
3D ROTATION:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
#include<string.h>
void main()
{
int x[10],y[10],z[10],a[10],b[10],theta=0,n,gm,gd=DETECT;
int sx,sy,sz,not;
initgraph(&gd,&gm,"");
cout<<"Give the no of Sides:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Give the "<<i+1<<" Co-ordinate:";
cin>>not;
for(i=0;i<n;i++)
{
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(x[i]+z[i],y[i]+z[i],x[(i+1)%n]+z[(i+1)%n],y[(i+1)%n]+z[(i+1)%n]);
line(x[i],y[i],x[i]+z[i],y[i]+z[i]);
§1
}
}
getch();
cleardevice();
do
{
theta=(theta+1)%360;
float not=(theta*3.14)/180;
for(i=0;i<n;i++)
{
a[i]=abs(x[0]+0.5+(x[0]-x[i])*cos(not)-(y[0]-y[i])*sin(not));
b[i]=abs(y[0]+0.5+(x[0]-x[i])*sin(not)+(y[0]-y[i])*cos(not));
}
for(i=0;i<n;i++)
{
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(x[i]+z[i],y[i]+z[i],x[(i+1)%n]+z[(i+1)%n],y[(i+1)%n]+z[(i+1)%n]);
line(x[i],y[i],x[i]+z[i],y[i]+z[i]);
}
delay(85);
putpixel(x[0],y[0],15);
cleardevice();
}while(theta<=not);
closegraph();
getch();
§1
}
OUTPUT:
GIVE THE NUMBER OF SIDES : 4
GIVE THE 1 CO-ORDINATE : 100 100
GIVE THE 2 CO-ORDINATE : 150 100
GIVE THE 3 CO-ORDINATE : 150 150
GIVE THE 4 CO-ORDINATE : 100 150
ENTER THE ANGLE : 60
§1
3D SCALING:
#include<iostream.h>#include<conio.h>#include<string.h>#include<stdio.h>#include<dos.h>#include<graphics.h>
void dev(int x[],int y[],int z[],int n,int tx,int ty,int tz){
cleardevice();for(int i=0;i<n;i++){
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);line(x[i]+z[i],y[i]+z[i],x[(i+1)%n]+z[(i+1)%n],y[(i+1)%n]+z[(i+1)%n]);line(x[i],y[i],x[i]+z[i],y[i]+z[i]);
}getch();
for(i=0;i<n;i++){
x[i]=x[i]*tx;y[i]=y[i]*ty;z[i]=z[i]*tz;
}
for(i=0;i<n;i++){
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);line(x[i]+z[i],y[i]+z[i],x[(i+1)%n]+z[(i+1)%n],y[(i+1)%n]+z[(i+1)%n]);line(x[i],y[i],x[i]+z[i],y[i]+z[i]);
}}
void main(){
int x[10],y[10],z[10],n,gm,gd=DETECT,tx,ty,tz;initgraph(&gd,&gm,"");cout<<"Give the no. of Sides:";cin>>n;
for(int i=0;i<n;i++){
§1
cout<<"\n Give the "<<i+1<<" co-ordinate: ";cin>>x[i]>>y[i]>>z[i];
}
cout<<"\n Enter the Translation Vector:";cin>>tx>>ty>>tz;dev(x,y,z,n,tx,ty,tz);getch();closegraph();
}
§1
OUTPUT:
GIVE THE NUMBER OF SIDES : 4
GIVE THE 1 CO-ORDINATE : 120 120 150
GIVE THE 2 CO-ORDINATE : 120 150 150
GIVE THE 3 CO-ORDINATE : 150 150 150
GIVE THE 4 CO-ORDINATE : 150 120 150
ENTER THE SCALING VECTORS : 2 1 1
§1
RESULT:
Thus the program for 3D transformations are executed successfully and the
output is verified.
§1
SUTHERLAND AND CHOSEN SUBDIVITION
2D LINE CLIPPING ALGORITHM
AIM:
To write a C++ program to clip the line using Sutherland clipping algorithm.
ALGORITHM:
Step1: Initialize position for end point of line.
Step2: Define the view actual size of the window to be displayed.
Step3: Declare the view condition to match the line clipping.
Step4: Using condition statement cheek for visibility before and after clipping.
Step5: Display the line within windows view point.
§1
Ex. No.:
Date:
PROGRAM:
#include<stdio.h>#include<conio.h>#include<iostream.h>#include<graphics.h>#include<math.h>#define TRUE 1#define FALSE 0
float x1,y1,x2,y2,dx,dy;float xwmin=100,xwmax=250,ywmin=100,ywmax=250;float u1=0.0,u2=1.0;
int cliptest(float p,float q,float *lu1,float *lu2){
float r;int result=TRUE;if(p<0.0){
r=q/p;if(r>*lu2)result=FALSE;elseif(r>*lu1)*lu1=r;
}else
if(p>0.0){
r=q/p;if(r<*lu1)result=FALSE;elseif(r<*lu2)*lu2=r;
}else
if(q<0)result=FALSE;
return result;}
void main(){
int gm,gd=DETECT;
§1
char a[10],b[10],c[10],d[10];cout<<"x1 value =>";cin>>a;cout<<"y1 value =>";cin>>b;cout<<"x2 value =>";cin>>c;cout<<"y2 value =>";cin>>d;initgraph(&gd,&gm,"");x1=atof(a);y1=atof(b);x2=atof(c);y2=atof(d);settextstyle(1,0,0);outtextxy(50,50,"Before Clipping");rectangle(xwmin,ywmin,xwmax,ywmax);line(x1,y1,x2,y2);dx=x2-x1;if(cliptest(-dx,x1-xwmin,&u1,&u2))if(cliptest(dx,xwmax-x1,&u1,&u2)){
dy=y2-y1;if(cliptest(-dy,y1-ywmin,&u1,&u2))if(cliptest(dy,ywmax-y1,&u1,&u2)){
if(u2<1.0){
x2=x1+u2*dx;y2=y1+u2*dy;
}if(u1<0.0){
x1=x1+u1*dx;y1=y1+y1*dy;
}}
}getch();settextstyle(1,0,0);outtextxy(375,50,"After Clipping");rectangle(xwmin+xwmax+50,ywmin,xwmin+xwmax+50+(xwmax-xwmin),250);line(xwmax+50+x1,y1,xwmax+50+x2,y2);getch();restorecrtmode();
§1
}
§1
OUTPUT:
X1 VALUE 100
Y1 VALUE 100
X2 VALUE 100
Y2 VALUE 100
BEFORE CLIPPING AFTER CLIPPING
§1
RESULT:
Thus the program for Sutherland clipping algorithm is executed successfully
and the output is verified.
§1
EX NO : GENERATING FRACTAL IMAGESDATE :Aim:
To generate fractal images.
Algorithm:
Step1: The Sierpinski Triangle is created by infinite removals
Step1: Each triangle is divided into 4 smaller upside down triangles
Step1: The center of the 4 triangles is removed
Step1: As the process is iterated infinite number of times, the total area of the
set goes to infinity as the size of the each new triangle goes to zero
Step1: After closer examination magnification factor is 2.
Step1: With each magnification there are 3 divisions of a triangle
Dimension D=ln(3)/ln(2)
D=1.5850
§1
#include <stdio.h>#include <conio.h>#include <stdlib.h>#include <math.h>#include <graphics.h>void DrawSierpinski(void);void main(void){int gd=VGA;int gm=VGAHI;initgraph(&gd,&gm, "\\tc\\bgi");DrawSierpinski();getch();}void DrawSierpinski(void){char Direct;int iterate;unsigned int x1, y1, x2, y2;x1 = x2 = 320;y1 = y2 = 0;for(iterate = 0; iterate < 10000; iterate++){Direct = random(3);if(Direct == 0){x1 = (x2 + 320) / 2;y1 = (y2 + 0) / 2;}else if(Direct == 1){x1 = (x2 + 0) / 2;y1 = (y2 + 480) / 2;}else if(Direct == 2){x1 = (x2 + 640) / 2;y1 = (y2 + 480) / 2;}putpixel(x1, y1, WHITE);x2 = x1;y2 = y1;
§1
}}
§1
Animation
#include<stdlib.h>#include<conio.h>#include<dos.h>#include<graphics.h>#include<ctype.h> void main(){ int gd=DETECT,gm; int newx=0,newy=0,inc_y=5;
initgraph(&gd,&gm,"c:/tc/bin/bgi/"); cleardevice(); while(!kbhit()) { ellipse(520-newx,200,30,330,90,30); circle(450-newx,193,3); line(430-newx,200,450-newx,200); line(597-newx,185,630-newx,170); line(597-newx,215,630-newx,227); line(630-newx,170,630-newx,227); line(597-newx,200,630-newx,200); line(597-newx,192,630-newx,187); line(597-newx,207,630-newx,213); line(500-newx,190,540-newx,150+newy); line(530-newx,190,540-newx,150+newy); if(newx>=500) newx=0; if(newy>=82) inc_y=-5; newx=newx+5; if(newy<=0) inc_y=5; newy=newy+inc_y; delay(50); cleardevice();
}getch();cleardevice();
}
§1
§1
§1
§1
§1
Recommended