Computer Graphics -
Assignment 1
1 Introduction
In this assignment you will implement Bresenham’s line drawing algorithm. You will use the skeleton
program given to you. The functionality implemented handles the user drawing a line by first clicking on a
point in the canvas, then by moving the mouse to a second point and releasing the button triggers an event
that calls the Bresenham line drawing algorithm.
The first point of the line is where the user clicked, the second point is set to where the user released the
mouse. The drawing (setting a pixel) is done on the HTML canvas. Your implementation has to handle all
possible situations, including all octants and vertical and horizontal lines.
2 Implementation Details
2.1 Mathematics
The algorithm can be implemented in the first octant. For any other octant, your program should first
identify it, then transform the problem to the first octant. Such a mapping is given in Section 3.3 of the
following Wikipedia article: https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
In the first octant, the main idea of the algorithm is simple: as x increases by 1, making the decision
whether y increases is done via accumulating an error. The error can be thought of as a deviation (along the
y axis) from the true line. If the error is strictly positive, then y increases, otherwise remains unchanged. The
derivation of this algorithm is shown in the course slides.
A line can be written in slope-intercept form as follows:
y = f (x) = mx + b (1)
where m is the slope, or rise over run: ∆y . After some simple algebraic manipulation, one can write the
∆
equation of the line as follows:
f (x, y) = 0 = (∆y)x − (∆x)y + (∆x)b
(2)
or
f (x, y) = 0 = Ax + By + C
(3)
where A = ∆y, B = −∆x and C = (∆x)b are constants.
Given a line from P0 to P1, P0 = {x0, y0} and P1 = {x1, y1}, the starting is on the line, i.e., f (x0, y0) =
0. Given a slope ≤ 1, the idea is to evaluate which point should be chosen at the next step (x0 + 1, y0)
1
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller Knowledgekings. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $4.99. You're not tied to anything after your purchase.