0% found this document useful (0 votes)
26 views4 pages

Computer Graphics Lab (CSP-305) Worksheet-3

This worksheet discusses Bresenham's line drawing algorithm for generating pixels along a line on a digital display. It provides steps to implement the algorithm to draw lines between two points with either positive or negative slope, including initializing variables, calculating increments, and generating pixels within the line segment. Source code is included that uses the algorithm to draw a line between two user-defined points using graphics functions in C++.

Uploaded by

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

Computer Graphics Lab (CSP-305) Worksheet-3

This worksheet discusses Bresenham's line drawing algorithm for generating pixels along a line on a digital display. It provides steps to implement the algorithm to draw lines between two points with either positive or negative slope, including initializing variables, calculating increments, and generating pixels within the line segment. Source code is included that uses the algorithm to draw a line between two user-defined points using graphics functions in C++.

Uploaded by

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

Computer Graphics Lab

(CSP-305)
Worksheet-3

Submitted to: Submitted By:


Er. Udit Jindal Samridh Garg
CSE-5(C)
19BCS1815
Q1. To draw a line using Bresenham’s line drawing Algorithm for positive and negative
line slop.
Answer:
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.
Bresenham's Line Algorithm:
Step1: Start Algorithm
Step2: Declare variable x1,x2,y1,y2,d,i1,i2,dx,dy
Step3: Enter value of x1,y1,x2,y2
Where x1,y1are coordinates of starting point
And x2,y2 are coordinates of Ending point
Step4: Calculate dx = x2-x1
Calculate dy = y2-y1
Calculate i1=2*dy
Calculate i2=2*(dy-dx)
Calculate d=i1-dx
Step5: 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
Step6: Generate point at (x,y)coordinates.
Step7: Check if whole line is generated.
If x > = xend
Stop.
Step8: 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
Step9: Increment x = x + 1
Step10: Draw a point of latest (x, y) coordinates
Step11: Go to step 7
Step12: End of Algorithm

Source Code:
#include<stdio.h>
#include<graphics.h>
#include<iostream>
using namespace std;
void drawline(int x0, int y0, int x1, int y1)
{
int dx, dy, p, x, y;
dx=x1-x0;
dy=y1-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, x1, y1;
initgraph(&gdriver, &gmode, "");
x0=100,y0=500,x1=200,y1=200;
drawline(x0, y0, x1, y1);
getch();
closegraph();
return 0;
}
Output:

You might also like