Graphics programming using c
Home
Question Paperz
Java Projectz
ASP Projectz
8086
Data structures
c++
8085
Tuesday, August 3, 2010
Line Drawing
1. Write a program to draw line using DDA algorithm and Bresenham's algorithm.
#include<stdio.h> #include<graphics.h> #include<math.h> #define ROUND(a)((int)(a+0.5)) void DDA(int,int,int,int); void Bresenham(int,int,int,int); int main() { int xa,ya,xb,yb,choice,gd=DETECT,gm=VGAMAX; printf("\nEnter first cordinates "); scanf("%d%d",&xa,&ya); printf("\nEnter second cordinates "); scanf("%d%d",&xb,&yb); while(1) { printf("\nMENU\n-------\n1.DDA\n2.BRESENTHAM\n3.EXIT"); printf("\nEnter the choice "); scanf("%d",&choice); initgraph(&gd,&gm,NULL); switch(choice) { case 1: DDA(xa,ya,xb,yb); break; case 2: Bresenham(xa,ya,xb,yb); break; case 3: exit(0); break; default:printf("\nInvalid Choice"); } while (!kbhit()); closegraph(); } return 0; } void DDA(int xa,int ya,int xb,int yb) { int dx,dy,steps,k; dx=xb-xa; dy=yb-ya; float x=xa,y=ya,xinc,yinc; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float)steps; yinc=dy/(float)steps; putpixel(ROUND(x),ROUND(y),10); for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(ROUND(x),ROUND(y),10); } } void Bresenham(int xa,int ya,int xb,int yb) { int dx=abs(xa-xb),dy=abs(ya-yb),p,twody,twodydx,x,y,xEnd; p=2*dy-dx; twody=2*dy; twodydx=2*(dy-dx); if(xa>xb) { x=xb; y=yb; xEnd=xa; } else { x=xa; y=ya; xEnd=xb; } putpixel(x,y,10); while(x<xEnd) { x++; if(p<0) p+=twody; else { y++; p+=twodydx; } putpixel(x,y,10); } }
No comments:
Post a Comment
Newer Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment