EX .NO:2 LINE DRAWING USING BRESENHAMS ALGORITHM
Date:
AIM:
To write a C program to draw a line using Bresenhams algorithm.
ALGORITHM:
Step1: Start.
Step2:Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,
Mode.
Step3: Initialize the graphic mode with the path location in TC folder
Step4: Input the two line end-points and store the left end-points in (x1,y1).
Step5: Load (x1, y1) into the frame buffer; that is, plot the first point put x=x1,y=y1
Step6: Calculate dx=x2-x1 and dy=y2-y1, and obtain the initial value of decision
Parameter p as: a.p=(2dy-dx).
Step7: Starting from first point (x,y) perform the following test:
Step8: Repeat step 9 while(x<=x2).
Step9: If p<0, next point is (x+1,y) and p=(p+2dy).
Step10: Otherwise, the next point to plot is (x+1,y+1) and p=(p+2dy-2dx).
Step11: Place pixels using putpixel at points (x,y) in specified colour.
Step12: Close Graph
Step13: Stop.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy;
intgdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI:");
printf("\nEnter the x-coordinate of the first point ::");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d",&y2);
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=(dy-dx);
while(x<=x2)
{ if(p<0)
{ x=x+1;
p=2*x-dx;
}
{
x=x+1;
y=y+1;
p=p+2*dy;
}
putpixel(x,y,7);
}
getch();
closegraph();
}
OUTPUT:
Enter the x-coordinate of the first point: 200
Enter the y-coordinate of the first point: 300
Enter the x-coordinate of the second point: 300
Enter the y-coordinate of the second point: 300
RESULT:
Thus the program of line drawing using Bresenhams algorithm is executed and output is obtained.
0 comments:
Post a Comment