Bresenham's line drawing algorithm
A line segment is defined by an infinite set of points which lie between two points; these points have no area. A problem occurs, however, when drawing lines on a computer screen, namely that we have to approximate the line with pixels which are not infinitely small, and which are limited to fixed positions based on the
screen resolution.
A simple line drawing algorithm can be implemented as,
filename dda.py
from Tkinter import *
import math
#defining main function
def main():
main()
This is the basic incremental algorithm,often referred as a digital differential analyser(DDA) algorithm.
This algorithm, of course, relies on some assumptions such as:
• Non-infinite slope
• x1 < x2
• y1 < y2
The problem about this is it is slow and can be solved by using mid-point algorithm.
The Bresenham / Midpoint Algorithm is a solution that not only correctly picks
these same points, it does it with integer math only, which results in a big speed
improvement.
Here is an implementation of Bresenham's/mid-point algorithm.The program is coded in python, using the Tkinter module.
filename:midpoint.py
from Tkinter import *
import math
#defining main function
def main():
main()
#the indentation of program is changed after posting,edit it while implementing.(python works based on indentation)
screen resolution.
A simple line drawing algorithm can be implemented as,
filename dda.py
from Tkinter import *
import math
#defining main function
def main():
#reading end points of line
x0 = int(raw_input("ENTER THE Xo : ")) y0 = int(raw_input("ENTER THE Yo : ")) x1 = int(raw_input("ENTER THE Xn : ")) y1 = int(raw_input("ENTER THE Yn : ")) root = Tk() pic = PhotoImage(width=800,height=800) lb = Label(root,image=pic) lb.pack() color = "blue" dx = x2 – x1; dy = y2 – y1; m = dy/dx for x in range(x1,x2) y = m*x + y1 + 0.5; pic.put(color(x, y)); #for loop end here root.mainloop() #main endmain()
This is the basic incremental algorithm,often referred as a digital differential analyser(DDA) algorithm.
This algorithm, of course, relies on some assumptions such as:
• Non-infinite slope
• x1 < x2
• y1 < y2
The problem about this is it is slow and can be solved by using mid-point algorithm.
The Bresenham / Midpoint Algorithm is a solution that not only correctly picks
these same points, it does it with integer math only, which results in a big speed
improvement.
Here is an implementation of Bresenham's/mid-point algorithm.The program is coded in python, using the Tkinter module.
filename:midpoint.py
from Tkinter import *
import math
#defining main function
def main():
#main starts #reading end points of line x0 = int(raw_input("ENTER THE Xo : ")) y0 = int(raw_input("ENTER THE Yo : ")) x1 = int(raw_input("ENTER THE Xn : ")) y1 = int(raw_input("ENTER THE Yn : ")) root = Tk() pic = PhotoImage(width=800,height=800) lb = Label(root,image=pic) lb.pack() color = "blue" #Now Drawing Line steep = abs(y1 - y0) > abs(x1 - x0) if steep: x0, y0 = y0, x0 x1, y1 = y1, x1 #if ends if x0 > x1: x0, x1 = x1, x0 y0, y1 = y1, y0 if y0 < y1: ystep = 1 else: ystep = -1 deltax = x1 - x0 deltay = abs(y1 - y0) error = -deltax / 2 y = y0 for x in range(x0, x1 + 1): if steep: pic.put(color,(y,x)) #if end else: pic.put(color,(x,y)) #else end error = error + deltay if error > 0: y = y + ystep error = error - deltax #if end root.mainloop() #main endsmain()
#the indentation of program is changed after posting,edit it while implementing.(python works based on indentation)
Comments
Post a Comment