draw_line.py

Created by gasmat2012

Created on January 18, 2026

2.09 KB

Draws a line using either Bresenham’s line algorithm (-> https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm) which is fast as it uses only two colors, when alias = True, or Xiaolin Wu’s line algorithm (-> https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm) which is slower as it uses opacity to improve line resolution, when alias = False. This script was made to be imported in my project_3d script via the descartes.py script.


from math import *
from kandinsky import *

def putPixel(x,y,opacity,col,bgcol):
  r,g,b=color(col)
  bgr,bgg,bgb=color(bgcol)
  r+=(bgr-r)*(1-opacity)
  g+=(bgg-g)*(1-opacity)
  b+=(bgb-b)*(1-opacity)
  set_pixel(x,y,(r,g,b))

def drawLine(x0,y0,x1,y1,col=(0,)*3,bgcol=(255,)*3,alias=False):
  if abs(y1-y0)<abs(x1-x0):
    if x1<x0:
      x0,x1=x1,x0
      y0,y1=y1,y0
    
    dx=x1-x0
    dy=y1-y0
    m=dy/dx if dx!=0 else 1
    
    if alias:
      putPixel(int(x0),int(y0),1.0,col,bgcol)
    else:
      overlap=1-((x0+0.5)-int(x0+0.5))
      distStart=y0-int(y0)
      putPixel(int(x0+0.5),int(y0),(1-distStart)*overlap,col,bgcol)
      putPixel(int(x0+0.5),int(y0)+1,distStart*overlap,col,bgcol)
    
    if alias:
      putPixel(int(x1),int(y1),1.0,col,bgcol)
    else:
      overlap=((x1-0.5)-int(x1-0.5))
      distEnd=y1-int(y1)
      putPixel(int(x1+0.5),int(y1),(1-distEnd)*overlap,col,bgcol)
      putPixel(int(x1+0.5),int(y1)+1,distEnd*overlap,col,bgcol)
    
    for i in range(int(dx)+1):
      x=x0+i
      y=y0+i*m
      ix,iy=int(x),int(y)
      if alias:
        putPixel(ix,iy,1,col,bgcol)
      else:
        dist=y-iy
        putPixel(ix,iy,1-dist,col,bgcol)
        putPixel(ix,iy+1,dist,col,bgcol)

  else:
    if y1<y0:
      x0,x1=x1,x0
      y0,y1=y1,y0
    
    dx=x1-x0
    dy=y1-y0
    m=dx/dy if dy!=0 else 1

    if alias:
      putPixel(int(x0),int(y0),1,col,bgcol)
    else:
      overlap=1-((y0+0.5)-int(y0+0.5))
      distStart=y0-int(y0)
      putPixel(int(x0+0.5),int(y0),(1-distStart)*overlap,col,bgcol)
      putPixel(int(x0+1.5),int(y0)+1,distStart*overlap,col,bgcol)

    if alias:
      putPixel(int(x1),int(y1),1.0,col,bgcol)
    else:
      overlap=((y1-0.5)-int(y1-0.5))
      distEnd=y1-int(y1)
      putPixel(int(x1),int(y1+0.5),(1-distEnd)*overlap,col,bgcol)
      putPixel(int(x1)+1,int(y1+0.5),distEnd*overlap,col,bgcol)

    for i in range(int(dy)+1):
      x=x0+i*m
      y=y0+i
      ix,iy=int(x),int(y)
      if alias:
        putPixel(ix,iy,1,col,bgcol)
      else:
        dist=x-ix
        putPixel(ix,iy,1-dist,col,bgcol)
        putPixel(ix+1,iy,dist,col,bgcol)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.