# draw_triangle.py

Created by fime

Created on January 29, 2023

713 Bytes

`draw_triangle` that draw a triangle.

Usage :

draw_triangle(

px = [x1, x2, x3], #the points x positions (must be a list)

py=[y1, y2, y3], # the points y positions (must be a list)

c = (r, v, b), # the color of the triangle

step = 1 ) # the step, must be higher or equal to 1 (for a full filling), if it is greater than one the triangle will be hatched.

```from math import *
from kandinsky import fill_rect

def rect(x,y,w,h,c):fill_rect(int(x),int(y),int(w),int(h),c)

def draw_triangle(px,py,c="red",step=1):
for i in range(3):# sorting the points
j=px.index(max(px[-i:]))
mx,my=px[j],py[j]
del px[j],py[j]
px,py=px+[mx],py+[my]
x1,x2,x3,y1,y2,y3=px+py
del px,py
if x1!=x2 and x2!=x3:# the triangle has no vertical side
y4=y1+(x2-x1)*(y3-y1)/(x3-x1)
draw_triangle([x1,x2,x2],[y1,y2,y4],c)
x1,y1=x2,y4
for x in range(0,x3-x1,step):
k=x/(x3-x1)
if x1==x2:ya,yb=y1+k*(y3-y1),y2+k*(y3-y2)
if x2==x3:ya,yb=y1+k*(y2-y1),y1+k*(y3-y1)
rect(x+x1,ya,1,yb-ya,c)

draw_triangle([10,50,100],[100,200,150])```