This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
/* DDA Techinique to Draw a striaght Line */
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2,xinc,yinc;
int i,step;
float dx,dy;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter 1st End Points x1 & y1 : \n");
scanf("%d %d",&x1,&y1);
printf("\nEnter 2nd End points x2 & y2 : \n ");
scanf("%d %d",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
if(abs(dx) > abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
putpixel(x1,y1,BLUE);
for(i=1; i<=step; i++)
{
x1=x1+xinc;
y1=y1+yinc;
putpixel(x1,y1,BLUE);
}
getch();
}
/*MID POINT CIRCLE ALGORITHM IMPLEMENTED IN C*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm;
int x,y,r;
void Drawcircle(int,int,int);
printf("Enter the Mid points and Radious:");
scanf("%d%d%d",&x,&y,&r);
initgraph(&gd,&gm,"h:\\tc1\\bgi");
Drawcircle(x,y,r);
getch();
closegraph();
}
/* Function for MID-POINT CIRCLE algorithm */
void Drawcircle(int x1,int y1,int r)
{
int x=0,y=r,p=1-r;
cliplot(x1,y1,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cliplot(x1,y1,x,y);
}
}
/* function to plot point obtained from algorithm */
cliplot(int xctr,int yctr,int x,int y)
{
putpixel(xctr +x,yctr +y,7);
putpixel(xctr -x,yctr +y,7);
putpixel(xctr +x,yctr -y,7);
putpixel(xctr -x,yctr -y,7);
putpixel(xctr +y,yctr +x,7);
putpixel(xctr -y,yctr +x,7);
putpixel(xctr +y,yctr -x,7);
putpixel(xctr -y,yctr -x,7);
// getch();}
/* OUTPUT SCREEN FOR MID-POINT ALGORITHM FOR CIRCLE-DRAWING
ALGORITHM */
/*Bresenhams Techinique to Draw a striaght Line */
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <math.h>
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2;
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter 1st End Points x1 & y1 : \n");
scanf("%d %d",&x1,&y1);
printf("\nEnter 2nd End points x2 & y2 : \n");
scanf("%d %d",&x2,&y2);
bresenhams(x1,y1,x2,y2);
getch();
closegraph();
}
bresenhams(int x1,int y1,int x2,int y2)
{
int dx,dy,x,y;
int xc,c1,c2,p;
dx=abs(x2-x1);
dy=abs(y2-y1);
p=2*dy-dx;
c1=2*dy;
c2=2*(dy-dx);
if(x1 > x2)
{
x=x2;
y=y2;
xc=x1;
}
else
{
x=x1;
y=y1;
xc=x2;
}
putpixel(x,y,2);
while(x<xc)
{
x=x+1;
if(p<0)
p=p+c1;
else
{
y=y+1;
p=p+c2;
}
putpixel(x,y,2);
}
return;
}
/*PROGRAM FOR LIANG BASWKY ALGORITHM IMPLEMENTION */
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd, gm ;
int x1 , y1 , x2 , y2 ;
int wxmin,wymin,wxmax, wymax ;
float u1 = 0.0,u2 = 1.0 ;
int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ;
float r1 , r2 , r3 , r4 ;
int x11 , y11 , x22 , y22 ;
clrscr();
printf("Enter the windows left xmin , top boundry ymin\n");
scanf("%d%d",&wxmin,&wymin);
printf("Enter the windows right xmax ,bottom boundry ymax\n");
scanf("%d%d",&wxmax,&wymax);
printf("Enter line x1 , y1 co-ordinate\n");
scanf("%d%d",&x1,&y1);
printf("Enter line x2 , y2 co-ordinate\n");
scanf("%d%d",&x2,&y2);
printf("liang barsky express these 4 inequalities using lpk<=qpk\n");
p1 = -(x2 - x1 ); q1 = x1 - wxmin ;
p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ;
p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ;
p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;
printf("p1=0 line is parallel to left clipping\n");
printf("p2=0 line is parallel to right clipping\n");
printf("p3=0 line is parallel to bottom clipping\n");
printf("p4=0 line is parallel to top clipping\n");