Nothing Special   »   [go: up one dir, main page]

Cad

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

Line Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
void drawline(int x0,int y0,int x1,int y1)
{
int dx,dy,p,x,y;
dx=x1-x0;
dy=y1-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 i,gd=DETECT,gm,x0,y0,x1,y1;
initgraph(&gd,&gm, "C:/TURBOC3/BGI");
printf("enter coordinates of first point");
scanf("%d%d",&x0,&y0);
printf("enter cordinates of second point");
scanf("%d%d",&x1,&y1);
drawline(x0,y0,x1,y1);
return 0;
}
Diagonal Line :
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int xmax, ymax;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "C:/TurboC3/ BGI");
/* read result of initialization */
errorcode = graphresult();
/* an error occurred */
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}

setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();

/* draw a diagonal line */


line(0, 0, xmax, ymax);

/* clean up */
getch();
closegraph();
return 0;
}
Output of Diagonal line
Circle
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;
int midx, midy;
int radius = 50;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");

/* read result of initialization */


errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
left = getmaxx() / 2 - 125;
top = getmaxy() / 2 - 75;
right = getmaxx() / 2 + 125;
bottom = getmaxy() / 2 + 75;
midx = getmaxx() / 2 ;
midy = getmaxy() / 2 ;
putpixel(midx, midy, 4);

/* draw a rectangle */
rectangle(left,top,right,bottom);
/* draw a circle */
circle(midx, midy, radius);

/* clean up */
getch();
closegraph();
return 0;
}
Rectangle
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode, errorcode;
int left, top, right, bottom;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:/turboc3/BGI");

/* read result of initialization */


errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}

left = getmaxx() / 2 - 125;


top = getmaxy() / 2 - 125;
right = getmaxx() / 2 + 75;
bottom = getmaxy() / 2 + 75;

/* draw a rectangle */
rectangle(left,top,right,bottom);

/* clean up */
getch();
closegraph();
return 0;
}
Output of Rectangle
DDA Algorythm
#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main(void)
{
/* request auto detection */
int gdriver = DETECT, gmode;
int s, dx, dy, m, x1, y1, x2, y2;
float xi, yi, x, y;
clrscr();
printf("enter the starting piont x1 & y1\n");
scanf("%d%d",&x1, &y1);
printf("enter the end point x2 & y2\n");
scanf("%d%d",&x2, &y2);
/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "C:/TURBOC3/BGI");
cleardevice();
dx=x2-x1;
dy=y2-y1;

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


s=abs(dx);
else
s=abs(dy);
xi=dx/(float)s;
yi=dy/(float)s;
x=x1;
y=y1;

putpixel(x1,y1, 4);
for (m = 0; m < s; m++)
{
x +=xi;
y +=yi;
putpixel(x,y,4);
}
getch();
return(0);
}
Bresenham”s Algorythm
#include<graphics.h>
#include<stdio.h>
#include<conio.h>
int main(void)
{
int gdrive=DETECT,gmode,errorcode;
int x1,y1,x2,y2,z,i,p,q,r,t;
clrscr();
initgraph(&gdrive,&gmode,"c:/TURBOC3/BGI");
errorcode=graphresult();
if(errorcode != grOk)
{
printf("cheak initalisation");
getch();
exit(1);
}
printf("enter the value x1=");
scanf("%d",&x1);
printf("enter the value y1=");
scanf("%d",&y1);
printf("enter the value x2=");
scanf("%d",&x2);
printf("enter the value y2=");
scanf("%d",&y2);
q=(y2-y1);
r=(x2-x1);
putpixel(x1,y1,9);
if(q<r)
{
p= ((2*q)-r);
for(i=1;i<=1000;i++)
{
if(p<0)
{
x1=x1+1;
t=y1;
putpixel(x1,t,2);
}
else
{
x1=x1+1;
t=y1+1;
putpixel(x1,t,2);
}
z=p+(2*q)-(2*r*(t-y1));
y1=t;
p=z;
if((x1==x2) || (y1==y2))
{
break;
}
}
}
else
{
p=((2*r)-q);
for(i=1;i<=1000;i++)
{
if(p<0)
{
y1=y1+1;
t=x1;
putpixel(t,y1,2);
}
else
{
y1=y1+1;
t=x1+1;
}
z=p+(2*r)-(2*q*(t-x1));
x1=t;
p=z;
if((x1==x2) || (y1==y2))
{
break;
}
}
}
getch();
return(0);}
Transformation
clc;
a = input('Enter 3 column input matrix: a='); %input matrix for line
plot(a(:,1),a(:,2))

disp(' 1 = translation ');


disp(' 2 = scaling ');
disp(' 3 = rotating ');
disp(' 4 = mirror ');
Choice = input('');
hold on
%-------------------- for translate the entity ----------------------------%
if(Choice == 1)

tx = input(' translation in x cordinate = ');


ty = input(' translation in y cordinate = ');

e = [1 0 0 % translation matrix
0 1 0
tx ty 1];

d = a*e; % matrix multiplication


plot(d(:,1),d(:,2))
end
%----------------------------------------------------------------------------%

hold on
%-------------------- for scaling the entity ----------------------------%
if(Choice == 2)

sx = input(' scaling in x cordinate = ');


sy = input(' scaling in y cordinate = ');

e = [sx 0 0 % scaling matrix


0 sy 0
0 0 1];

d = a*e; % matrix multiplication


plot(d(:,1),d(:,2))
end
%----------------------------------------------------------------------------%

hold on

%-------------------- for rotating the entity ----------------------------%


if(Choice == 3)
b = input(' rotational angle theta = '); %rotational angle theta to rotate
the line

disp(' 1 = clock wise motion(-ve)');


disp(' 2 = anticlock wise motion(+ve)');
choice = input('');

if(choice == 1)
s = sin(-b*(pi/180));
c = cos(-b*(pi/180));

else
s = sin(b*(pi/180));
c = cos(b*(pi/180));
end

e = [c s 0 % rotational matrix
-s c 0
0 0 1];

d = a*e; % matrix multiplication


plot(d(:,1),d(:,2))
end
%----------------------------------------------------------------------------%

hold on

%-------------------- for mirror the entity ----------------------------%


if(Choice == 4)

disp(' 1 = mirror @ X axis');


disp(' 2 = mirror @ Y axis');
choice = input('');
if(choice == 1)
e = [1 0 0 % scaling matrix
0 -1 0
0 0 1];

else
e =[-1 0 0 % scaling matrix
0 1 0
0 0 1];
end

d = a*e; % matrix multiplication


plot(d(:,1),d(:,2))
end
%----------------------------------------------------------------------------%
Output of translation:
Rotation:
Scaling:
Mirror:
//Translation and rotation of triangle
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gm;
int gd=DETECT;
int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3;
int xt,yt,r;
float t;
clrscr();
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
printf("enter the first cordinate=");
scanf("%d %d",&x1,&y1);
printf("enter the second cordinate");
scanf("%d %d",&x2,&y2);
printf("enter the third cordinate");
scanf("%d %d",&x3,&y3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
printf("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);
printf("enter the angle of rotation");
scanf("%d",&r);
t=3.14*r/180;
nx1=abs(nx1*cos(t) - ny1*sin(t));
ny1=abs(nx1*sin(t) + ny1*cos(t));
nx2=abs(nx2*cos(t) - ny2*sin(t));
ny2=abs(nx2*sin(t) + ny2*cos(t));
nx3=abs(nx3*cos(t) - ny3*sin(t));
ny3=abs(nx3*sin(t) + ny3*cos(t));
line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);
line(nx3,ny3,nx1,ny1);
getch();
closegraph();
}

You might also like