draw_line.py

Created by tlake

Created on August 21, 2023

636 Bytes

Draws a line from (x0,y0) to (x1,y1) in user-chosen color


from math import *
from kandinsky import *
def draw_line(x0, y0, x1, y1, c=(0,0,0)):
  # Calculate "deltas" of the line (difference between two ending points)
  dx = x1 - x0
  dy = y1 - y0
  if abs(dx)>=abs(dy):
    step = abs(dx)
  else:
    step = abs(dy)
  dx = dx/step
  dy = dy/step
  x = x0
  y = y0
  i = 1
  # Draw the line based on arguments provided
  while i <= step:
    # Draw pixel at this location
    set_pixel(int(x), int(y), c)

    # Progress the line drawing algorithm parameters
    x += dx
    y += dy
    i += 1
#
def line():
  fill_rect(0,0,350,240,(0,0,0))
  draw_line(0,0,350,240,(0,255,0))
  

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.