Python uses DDA algorithm and midpoint Bresenham algorithm to draw straight line

Source: Internet
Author: User

Title: "Python uses DDA algorithm and midpoint Bresenham algorithm to draw straight line"
date:2018-06-11t19:28:02+08:00
Tags: ["Graphic science"]
Categories: ["Python"]

First on

Code
#!/usr/bin/env python# Coding=utf-8 fromPylabImport * fromMatplotlib.tickerImportMultiplelocatorImportMatplotlib.patches asPatches'''1. Input line ends Point x0,y0 xn,yn2. Calculate initial value delta_x, delta_y,k=delta_y/delta_x,d=0, X=x0,y=y03. Plot point x, y4. D Update to D+k, if d>0.5, then x, y update to X+1,y+1,d=d-1, otherwise x, y update to X+1,y5. Repeat 3,4 until the line is finished'''defInit (ax, width):# Set the length widthAx.axis ([0, Width,0, Width])# Set the position of the main tick label, the format of the label textMajorlocator=Multiplelocator (1) Minorlocator=Multiplelocator (0.5) Ax.xaxis.set_major_locator (Majorlocator) ax.yaxis.set_major_locator (majorlocator)# Ax.xaxis.set_minor_locator (minorlocator)    # Ax.yaxis.set_minor_locator (minorlocator)Ax.grid (True)# Grid of x axes using main scaledefAdd_pixel (x, y, Ax, c): X= round(x) Y= round(y)ifC== 1: Ax.add_patch (patches. Rectangle ((x- 0.5Y- 0.5),1,1, color=' B ')) Ax.plot (x, Y,' R. ')Else: Ax.add_patch (patches. Rectangle ((x- 0.5Y- 0.5),1,1)) Ax.plot (x, Y,' Y. ')if __name__ == ' __main__ ':# Splits a line of strings and converts them into numbersX0, y0, x1, y1, Width= Map(int,input("Enter the two points of the line and the side length of the canvas:"). Split (' '))ifX0>X1:x0,x1=X1,x0 y0,y1=Y1,y0 Ax=Subplot (121, Aspect=' equal ', title=' modified Bresenham ')# Improved BresenhamAx.plot ([x0, X1], [y0, Y1],'-K ') BX=Subplot (122, Aspect=' equal ', title=' DDA ')# DDABx.plot ([x0, X1], [y0, Y1],'-K ')# Graphical InitializationInit (ax, width) init (bx, width) delta_x=X1-X0 delta_y=Y1-Y0 D= 0    ifDelta_x== 0: K= 999999999    Else: K=Delta_y/Delta_x x= round(x0) Y= round(y0)'''DDA algorithm    '''    ifK> -1  andK< 1:# X Maximum Displacement         while True:ifX>X1: BreakAdd_pixel (x, y, BX,1) x=X+1Y=Y+KelifK>= 1:# Y Maximum Displacement         while True:ifY>Y1: BreakAdd_pixel (x, y, BX,1) y=Y+1X=X+1/KElse: while True:ifY<Y1: BreakAdd_pixel (x, y, BX,1) y=Y-1X=X-1/K'''range of K1. (0,1) x is maximum displacement, y positive increase2. (1,+inf) Y is the maximum displacement, x positive increase3. (0,-1) x is maximum displacement, Y negative increase4. ( -1,-inf) Y is the maximum displacement and y decreases. X Positive Increase    '''X=X0 y=Y0ifK> 1: while True:ifY>Y1: BreakAdd_pixel (x, y, Ax,0) y=Y+ 1D=D+ 1 /KifD> 0.5: X=X+ 1D=D- 1    elifK> 0: while True:ifX>X1: BreakAdd_pixel (x, y, Ax,0) x=X+ 1D=D+KifD> 0.5: Y=Y+ 1D=D- 1    elifK> -1: while True:ifX>X1: BreakAdd_pixel (x, y, Ax,0) x=X+ 1D=D-KifD> 0.5: Y=Y- 1D=D- 1    Else: while True:ifY<Y1: BreakAdd_pixel (x, y, Ax,0) y=Y- 1D=D- 1 /KifD> 0.5: X=X+ 1D=D- 1Show ()

Python uses DDA algorithm and midpoint Bresenham algorithm to draw straight line

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.