graph_magic.py

Created by jamac

Created on April 09, 2019

836 Bytes

graphs slope fields with color. change the string in the bottom line to the derivative to be displayed.


from math import *
from kandinsky import *

global x
global y

def hcolor(h):
  h/=360
  r=255*(1,2-6*h,0,6*h-4)[((0<=h and h<1/6) or (5/6<=h and h<=1),1/6<=h and h<1/3,1/3<=h and h<2/3, 2/3<=h and h<5/6).index(True)]
  g=255*(6*h,1,4-6*h,0)[(0<=h and h<1/6,1/6<=h and h<1/2,1/2<=h and h<2/3,2/3<=h and h<=1).index(True)]
  b=255*(0,6*h-2,1,6-6*h)[(0<=h and h<1/3,1/3<=h and h<1/2,1/2<=h and h<5/6,5/6<=h and h<=1).index(True)]
  return color(int(r),int(g),int(b))

def run(d):
  global x
  global y
  y=56
  for gy in range(0,111):
    x=-80
    for gx in range(0,160):
      try:
        h=(atan2(eval(d),1) + pi/2)/pi*360
      except ZeroDivisionError:
        h=0
      c=hcolor(h)
      for k in range(gy*2,gy*2+2):
        for h in range(gx*2,gx*2+2):
          set_pixel(h,k,c)
      x+=1
    y-=1

run("x/y")