Cad
Cad
Cad
#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;
setcolor(getmaxcolor());
xmax = getmaxx();
ymax = getmaxy();
/* 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;
/* 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;
/* 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;
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))
e = [1 0 0 % translation matrix
0 1 0
tx ty 1];
hold on
%-------------------- for scaling the entity ----------------------------%
if(Choice == 2)
hold on
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];
hold on
else
e =[-1 0 0 % scaling matrix
0 1 0
0 0 1];
end