Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Line drawing algorithm
1. DDA line drawing algorithm
Stage 1: Read the contribution of the end purposes of the line as (x1,y1) and (x2,y2) with the end
goal that x1!=x2 and y1!=y2
Stage 2: Calculate dx=x2-x1 and dy=y2-y1
Stage 3:
if(dx>=dy)
step=dx
else
step=dy
Stage 4: xin=dx/step and yin=dy/step
Stage 5: x=x1+0.5 and y=y1+0.5
Stage 6:
for(k=0;k<step;k++)
{
X=x+xin;
Y=y+yin;
Putpixel(x,y);
}
C program for DDA
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i, gd=DETECT,gm;
initgraph(&gd,&gm,"c:tcbgi");
3. Bresenham’s line drawing algorithm
Step 1: Start.
Step 2: Now, we consider Starting point as (x1, y1) and ending point (x2, y2).
Step 3: Now, we have to calculate dx and dy.
dx = x2-x1
dy = y2-y1
m = dy/dx
Step 4: Now, we will calculate the decision parameter pk with following formula.
pk = 2dy-dx
Step 5: The initial coordinates of the line are (xk, yk), and the next coordinates are (xk+1,
yk+1). Now, we are going to calculate two cases for decision parameter pk
Case 1: If
pk < 0
Then
pk+1 =pk +2dy
xk+1 = xk +1
yk+1 = yk
Case 2: If
pk >= 0
Then
pk+1 =pk +2dy-2dx
xk+1 =xk +1
yk+1 =yk +1
Step 6: We will repeat step 5 until we found the ending point of the line and the total number of
iterations =dx-1.
Step 7: Stop.
4. C program to implement Bresenham’s line drawing
# include <stdio.h>
# include <conio.h>
# include <graphics.h>
int main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
printf("nntEnter the co-ordinates of first point : ");
scanf("%d %d",&x1,&y1);
printf("nntEnter the co-ordinates of second point : ");
scanf("%d %d",&x2,&y2);
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:tcbgi");
putpixel(x,y,WHITE);
while(x <= x2)
{
if(p < 0)
{
x=x+1;
y=y;
p = p + 2 * (dy);
}