• Bresenham Line Draw in Python

    Using Python Turtle, The Bresenham Line Draw Algorithm is used to draw a line between two points having their co-ordinates. This algorithm is converted into python program using python turtle. The user needs to enter co-ordinates for two points and line is graphically drawn into turtle window.

    The basic principle of Bresenham’s line algorithm is to select the optimum raster locations to represent a straight line. To accomplish this the algorithm always increments either x or y by one unit depending on the slope of line. The increment in the other variable is determined by examining the distance between the actual line location and the nearest pixel. This distance is called decision variable or the error. In mathematical terms error or decision variable is defined as

    e = DB – DA or DA – DB

    Where DA = Distance Above side

                 DB = Distance Below side


    import turtle
    import math
    import time

    # Prompt the user for inputting two points
    x1, y1 = eval(input(“Enter x1 and y1 for point 1: “))
    x2, y2 = eval(input(“Enter x2 and y2 for point 2: “))
    # Bresenham Line Draw Algorithm
    dx = abs(x2 – x1)
    dy = abs(y2 – y1)

    x = x1
    y = y1

    e = 2 * dy-dx

    turtle.goto(x1, y1)
    turtle.write(“Point 1”)

    i = 1
    while(i <= dx):
    turtle.goto(x, y)

    while(e >= 0):
    y = y + 1
    e = e – 2 * dx

    x = x + 1
    e = e + 2 * dy
    i = i + 1

    turtle.goto(x2, y2) # Draw a line to (x2, y2)
    turtle.write(“Point 2”)


    Appreciate my work :Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPin on PinterestShare on RedditShare on StumbleUponShare on TumblrDigg thisShare on YummlyShare on VKFlattr the authorBuffer this page

One Responseso far.

  1. Mehul Nakum says:

    Thak you for creating blog for 4gl… this is nice step toward learning 4gl.

Leave a Reply

Your email address will not be published. Required fields are marked *