kandinsky_better.py

Created by maelg0000

Created on March 11, 2026

1.26 KB

Add a draw line function to Kandinsky


from math import *
from kandinsky import *

def draw_line(start,end,thickness,col):
  curr_x=start[0]
  curr_y=start[1]
  
  difx=end[0]-start[0]
  dify=end[1]-start[1]
  if abs(difx)>abs(dify):
    totalsteps=abs(difx/thickness)
  else:
    totalsteps=abs(dify/thickness)

  if totalsteps>0:
    lenstep_x=(difx/totalsteps)
    lenstep_y=(dify/totalsteps)

  for i in range(totalsteps):
    fill_rect(int(curr_x),int(curr_y),thickness,thickness,col)  
    curr_x+=lenstep_x
    curr_y+=lenstep_y
    

# test part, to move in real time the pos
# of the beginning and the end of the line

speed=10
start=[10,10]
end=[1,1]

from ion import * 
from time import sleep
while True:
  if keydown(KEY_TOOLBOX):end[1]-=speed
  if keydown(KEY_COMMA):end[1]+=speed
  if keydown(23):end[0]+=speed
  if keydown(KEY_IMAGINARY):end[0]-=speed
  if keydown(KEY_ALPHA):start[1]-=speed
  if keydown(KEY_LN):start[1]+=speed
  if keydown(KEY_LOG):start[0]+=speed
  if keydown(KEY_EXP):start[0]-=speed
  
  
  fill_rect(0,0,320,222,(255,255,255))
  draw_line(start,end,10,(0,0,0))
  fill_rect(start[0],start[1],10,10,(255,0,0))
  fill_rect(end[0],end[1],10,10,(0,0,255))

  draw_string(str(start),0,222-18,(0,0,0),(255,0,0))
  draw_string(str(end),0,222-18*2,(0,0,0),(0,0,255))

  sleep(0.1)

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.