Given two 3-D co-ordinates we need to find the points on the line joining them. All points have integer co-ordinates.
Input : (-1, 1, 1), (5, 3, -1) Output : (-1, 1, 1), (0, 1, 1), (1, 2, 0), (2, 2, 0), (3, 2, 0), (4, 3, -1), (5, 3, -1) Input : (-7, 0, -3), (2, -5, -1) Output : (-7, 0, -3), (-6, -1, -3), (-5, -1, -3), (-4, -2, -2), (-3, -2, -2), (-2, -3, -2), (-1, -3, -2), (0, -4, -1), (1, -4, -1), (2, -5, -1)
Bresenham’s Algorithm is efficient as it avoids floating point arithmetic operations. As in the case of 2-D Line Drawing, we use a variable to store the slope-error i.e. the error in slope of the line being plotted from the actual geometric line. As soon as this slope-error exceeds the permissible value we modify the digital to negate the error.
The driving axis of the line to be plotted is the one along which the line travels the farthest i.e. the difference in axes co-ordinates is greatest. Thus the co-ordinate values increase linearly by 1 along the driving axis and the slope-error variable is used to determine the change in the co-ordinate values of the other axis.
In case of a 2-D line we use one slope-error variable but in case of a 3-D line we need two () of them for each of the non-driving axes. If current point is (x, y, z) and the driving axis is the positive X-axis, then the next point could be
- (x+1, y, z)
- (x+1, y+1, z)
- (x+1, y, z+1)
- (x+1, y+1, z+1)
The value of slope-error variables are determined according to the following equations:-
The initial value of slope-error variables are given by the following equations:-
Here denote the difference in co-ordinates of the two end points along the X, Y, Z axes.
- Input the two endpoints and store the initial point as
- Calculate constants and determine the driving axis by comparing
the absolute values of
If abs() is maximum, then X-axis is the driving axis
If abs() is maximum, then Y-axis is the driving axis
If abs() is maximum, then Z-axis is the driving axis
- Let’s suppose that X-axis is the driving axis, then
- At each along the line, starting at k = 0, check the following conditions
and determine the next point:-
- If AND , then
- Else If AND , then
- Else If , then
- Else then
- If AND , then
- Repeat step 5 times
[(-1, 1, 1), (0, 1, 1), (1, 2, 0), (2, 2, 0), (3, 2, 0), (4, 3, -1), (5, 3, -1)]
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Comparions between DDA and Bresenham Line Drawing algorithm
- Mid-Point Circle Drawing Algorithm
- Bresenham’s circle drawing algorithm
- Midpoint ellipse drawing algorithm
- Slope of the line parallel to the line with the given slope
- DDA Line generation Algorithm in Computer Graphics
- Klee's Algorithm (Length Of Union Of Segments of a line)
- Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
- Bresenham’s Line Generation Algorithm
- Mid-Point Line Generation Algorithm
- Anti-aliased Line | Xiaolin Wu's algorithm
- Line Clipping | Set 2 (Cyrus Beck Algorithm)
- How to check if two given line segments intersect?
- Given n line segments, find if any two segments intersect
- Count maximum points on same line
- Find an Integer point on a line segment with given two ends
- Scan-line Polygon filling using OPENGL in C
- Represent a given set of points by the best possible straight line
- Program to find line passing through 2 Points
- Reflection of a point about a line in C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.