0% found this document useful (0 votes)
27 views9 pages

CG 5

The document describes the Bresenham's line algorithm (BLA) for scan converting a line in a digital display. It works by selecting pixels that are closest to the actual line to draw it efficiently using only integer operations. It calculates a decision variable to determine whether to use the top or bottom pixel at each step along the line from the starting to ending points. The algorithm and an example Java program implementing it are provided.

Uploaded by

019bim017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

CG 5

The document describes the Bresenham's line algorithm (BLA) for scan converting a line in a digital display. It works by selecting pixels that are closest to the actual line to draw it efficiently using only integer operations. It calculates a decision variable to determine whether to use the top or bottom pixel at each step along the line from the starting to ending points. The algorithm and an example Java program implementing it are provided.

Uploaded by

019bim017
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ST.

XAVIER’S COLLEGE
MAITIGHAR, KATHMANDU

Computer Graphics (CG)

Lab Report 5
Line using BLA algorithm

Submitted by
Jeshika Baniya
019BIM017

Submitted to
Mr. Ganesh Yogi
Lecturer
Department of Computer Science
St. Xavier’s College
BLA Algorithm
This algorithm is used for scan converting a line. It was developed by Bresenham. It is an
efficient method because it involves only integer addition, subtractions, and multiplication
operations. These operations can be performed very rapidly so lines can be generated quickly.
In this method, next pixel selected is that one who has the least distance from true line.
The method works as follows:
Assume a pixel P1'(x1',y1'),then select subsequent pixels as we work our may to the night, one
pixel position at a time in the horizontal direction toward P2'(x2',y2').

To chooses the next one between the bottom pixel S and top pixel T.
If S is chosen
We have xi+1=xi+1 and yi+1=yi
If T is chosen
We have xi+1=xi+1 and yi+1=yi+1
The actual y coordinates of the line at x = xi+1is
y=mxi+1+b

The distance from S to the actual line in y direction


s = y-yi
The distance from T to the actual line in y direction
t = (yi+1)-y
Now consider the difference between these 2 distance values
s-t
When (s-t) <0 ⟹ s < t
The closest pixel is S
When (s-t) ≥0 ⟹ s < t
The closest pixel is T
This difference is
s-t = (y-yi)-[(yi+1)-y]
= 2y - 2yi -1
Substituting m by and introducing decision variable
di=△x (s-t)

di=△x (2 (xi+1)+2b-2yi-1)
=2△xyi-2△y-1△x.2b-2yi△x-△x
di=2△y.xi-2△x.yi+c
Where c= 2△y+△x (2b-1)
We can write the decision variable di+1 for the next slip on
di+1=2△y.xi+1-2△x.yi+1+c
di+1-di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Since x_(i+1)=xi+1,we have
di+1+di=2△y.(xi+1-xi)- 2△x(yi+1-yi)
Special Cases
If chosen pixel is at the top pixel T (i.e., di≥0) ⟹ yi+1=yi+1
di+1=di+2△y-2△x
If chosen pixel is at the bottom pixel T (i.e., di<0) ⟹ yi+1=yi
di+1=di+2△y
Finally, we calculate d1
d1=△x[2m(x1+1)+2b-2y1-1]
d1=△x[2(mx1+b-y1)+2m-1]

Since mx1+b-yi=0 and m = , we have


d1=2△y-△x
Advantage:
1. It involves only integer arithmetic, so it is simple.
2. It avoids the generation of duplicate points.
3. It can be implemented using hardware because it does not use multiplication and division.
Algorithm
Step 1: Start Algorithm
Step 2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy
Step 3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point
Step 4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
Step 5: Consider (x, y) as starting point and xendas maximum possible value of x.
If dx < 0, then x = x2, y = y2, xend=x1
If dx > 0, then x = x1, y = y1, xend=x2
Step 6: Generate point at (x,y)coordinates.
Step 7: Check if whole line is generated.
If x > = xend
Step 8: Calculate co-ordinates of the next pixel
If d < 0
Then d = d + i1
If d ≥ 0
Then d = d + i2
Increment y = y + 1
Step 9: Increment x = x + 1
Step 10: Draw a point of latest (x, y) coordinates
Step 11: Go to step 7
Step 12: End of Algorithm
Program: Line using BLA algorithm

package jeshika;
import java.awt.*;
import javax.swing.*;
public class bla extends JFrame
{
bla()
{
setSize(700,700);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new FlowLayout(FlowLayout.CENTER));
}

public void paint(Graphics g)


{
int x1=50,y1=100,x2=300,y2=300;
int m = (y2-y1)/(x2-x1);
int dx = Math.abs(x2-x1);
int dy = Math.abs(y2-y1);
int p;
int x,y,c;
x = x1<=x2?x1:x2;
y = x==x1?y1:y2;
c = x==x1?x2:x1;
if(m>=0)
{
while(x<c)
{
if(Math.abs(m)<=1)
{
p = 2*dy - dx;
if(p<0){
x++;
p = p+2*dy;
}
else
{
x++;
y++;
p=p+2*dy-2*dx;
}
}
else
{
p = 2*dx - dy;
if(p<0)
{
y++;
p = p+2*dx;
}
else
{
x++;
y++;
p=p+2*dx-2*dy;
}
}
g.fillOval(x, y, 2, 2);
}
}else
{
while(x<c)
{
if(Math.abs(m)<=1)
{
p = 2*dy - dx;
if(p<0)
{
x--;
p = p+2*dy;
}
else
{
x--;
y++;
p=p+2*dy-2*dx;
}
}
else
{
p = 2*dx - dy;
if(p<0)
{
y++;
p = p+2*dx;
}
else
{
x--;
y++;
p=p+2*dx-2*dy;
}
}
g.fillOval(x, y, 2, 2);
}
}
}
public static void main(String[] args)
{
new bla();
}
}
Output:

You might also like