WAP To draw line using Bresenham’s Algorithm
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void draw_line(float x1,float y1,float x2,float y2);
int round(float);
void main()
{
int driver,mode,color=4;
// char msg[80];
int a,b;
float x1,y1,x2,y2;
clrscr();
printf("Enter first two end-points of the line:\n");
printf("x1=");
scanf("%f",&x1);
printf("y1 =");
scanf("%f",&y1);
printf("Enter first two end-points of the line:\n");
printf("x2 =");
scanf("%f",&x2);
printf("y2 =");
scanf("%f",&y2);
// settextjustify(CENTER_TEXT, CENTER_TEXT);
a = getmaxx() / 2;
b = getmaxy() / 2;
// clrscr();
driver = DETECT;
initgraph(&driver,&mode,"C:\\TC\\BGI");
// setbkcolor(15);
draw_line(x1,y1,x2,y2);
// sprintf(msg, "Color: %d",color);
outtextxy(a, b, "cfetch");
getch();
closegraph();
}
void draw_line(float x1,float y1,float x2,float y2)
{
// int color=4;
float m,c;
float x,y;
// char msg[80];
m = (y2-y1)/(x2-x1);
c = y1-m * x1;
for(x=x1;x<x2;x++)
{
y = m * x + c;
y = round(y);
putpixel(x,(int)y,WHITE);
}
}
int round(float x)
{
float temp;
int val = (x + 0.5);
return val;
}
#include<conio.h>
#include<graphics.h>
void draw_line(float x1,float y1,float x2,float y2);
int round(float);
void main()
{
int driver,mode,color=4;
// char msg[80];
int a,b;
float x1,y1,x2,y2;
clrscr();
printf("Enter first two end-points of the line:\n");
printf("x1=");
scanf("%f",&x1);
printf("y1 =");
scanf("%f",&y1);
printf("Enter first two end-points of the line:\n");
printf("x2 =");
scanf("%f",&x2);
printf("y2 =");
scanf("%f",&y2);
// settextjustify(CENTER_TEXT, CENTER_TEXT);
a = getmaxx() / 2;
b = getmaxy() / 2;
// clrscr();
driver = DETECT;
initgraph(&driver,&mode,"C:\\TC\\BGI");
// setbkcolor(15);
draw_line(x1,y1,x2,y2);
// sprintf(msg, "Color: %d",color);
outtextxy(a, b, "cfetch");
getch();
closegraph();
}
void draw_line(float x1,float y1,float x2,float y2)
{
// int color=4;
float m,c;
float x,y;
// char msg[80];
m = (y2-y1)/(x2-x1);
c = y1-m * x1;
for(x=x1;x<x2;x++)
{
y = m * x + c;
y = round(y);
putpixel(x,(int)y,WHITE);
}
}
int round(float x)
{
float temp;
int val = (x + 0.5);
return val;
}
Comments
Post a Comment