456.
cpp
AComputer program to draw a line of any start and endpoints coordinates using
Bresenham's line algorithm.
#include<stdio.h>
#include<graphics.h>
void drawl ine (int x0, int y0, int x1, int yl)
int dx, dy, p, X, y;
dx=xl-x0;
dy=yl-y0;
X=x0;
y=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 gdriver=DETECT, gmode, error, x0, y0,
initgraph (&gdriver, &gmode, "c:\iturboc3\bgi":
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
printf("Enter co-ordinates of second p0int: "):
scanf("%d%d", &x1, &y1);
drawline (x0, y0, x1, yl);
return 0;
Page l
345
coordinates and radius
A computer program to draw a circle with any centre
value.
#include<stdio.h>
#include<graphics.hs
#include <math.hs
#inclùde <dos. h>
void drawcircle(int x0, int y0, int radius)
int x = radius;
int y = 0;
int err 0;
while (x s= y)
putpixel(x0 X, y0 + y,
putpixel(x0 Y, yo +
putpixel(x0 y, yO +
putpiXel(x0 X, yO + y,
putpixel(«0 X, yo y,
putpixel(x0 y, yo X,
putpixel (x0 y, y0 x, 7)
putpixel(x0 X, y0- y, );
if (err <= 0)
y += 1;
err t= 2y + 1:
}
if (err > 0)
{
X -= 1;
err - 2*X + 1;
}
int main()
int gdriver=DETECT, gmode, error, x,
initgraph (&gdriver, agmode, ic:\iturboc3\bgi"):
printf("Enter radius of circle: ");
Scanf("%d", &r);
printf("Enter co-ordinates of center (x and y): "):
Scanf("%d%d", &x, &y);
drawcircle(x, y, );
delay (10000);
return 0;
}
Page 1
234
A Computer program to draw a 1ine with any
using DDA 1ine algorithm, start and end points
coordinates
#include <graphics. h>
#include <stdio.h>
#include <math.h>
#include <dos . h>
void main( )
float x, y, x1, yl, x2, y2,dx, dy, step;
int i, gd=DETECT, gm ;
initgraph (&gd. &gm, "c:\\turboc3\\bgi");
printf("Enter the value of x1 and yl : ");
Scanf("%f%f", &xl, &y1);
printf("Enter the value of x2 and y2: ");
Scanf("%f%f",&x2,&y2);
dx=abs (x2-x1) :
dy=abs (y2-yl):
if(dx>=dy)
step=dx;
else
step=dy:
dx=dx/step;
dy=dy/step;
X=X1;
y=yl;
i=l;
while(i<=step)
putpixel (x, y,5);
X=X+dx;
y=y+dy;
i=i+1;
delay(100);
}
dcDghd losegraph();