polar_graphing.py

Created by ferr0fluidmann

Created on January 21, 2020

1.7 KB


from math import *
import kandinsky as k
from turtle import *
#from my_functions import *

thetaMin = 0
thetaMax = 2*pi

def r_function(x):
  return x

def draw_polar_grid(it=500, xmin=-14.4,xmax=14.4,ymin=-10,ymax=10):
  blk=k.color(0,0,0)
  gry=k.color(220, 220, 220)
  x_map = 320.0/(xmax-xmin)
  y_map = 222.0/(ymax-ymin)
  y_0=int(ymax*222.0/(ymax-ymin))
  x_0=int(xmin*320.0/(xmin-xmax))
  grid = 1
  r_max = int(sqrt(xmax**2 + ymax**2))
  if (int(sqrt(xmin**2 + ymax**2)) > r_max):
    r_max = int(sqrt(xmin**2 + ymax**2))
  if (int(sqrt(xmin**2 + ymin**2)) > r_max):
    r_max = int(sqrt(xmin**2 + ymin**2))
  if (int(sqrt(xmax**2 + ymin**2)) > r_max):
    r_max = int(sqrt(xmax**2 + ymin**2))
  while grid <= r_max:
    for i in range(it):
      t=(2*pi*float(i))/float(it)
      x=int(y_map*float(grid)*cos(t))+x_0
      y=int(y_map*float(grid)*sin(t))+y_0
      k.set_pixel(x,y,gry)
    grid+=1
  for x in range(320):
    k.set_pixel(x,y_0,blk)
  for y in range(222):
    k.set_pixel(x_0,y,blk)

def polar_graph(it=1000,fr=r_function,tmax=thetaMax,tmin=thetaMin,xmin=-14.4,xmax=14.4,ymin=-10,ymax=10):
  reset()
  hideturtle()
  speed(10)
  x_map = 320/(xmax-xmin)
  y_map = 220/(ymax-ymin)
  x_0 = int(x_map*(xmax+xmin)/2.0)
  y_0 = int(y_map*(ymax+ymin)/2.0)
  draw_polar_grid(500, xmin,xmax,ymin,ymax)
  penup()
  color(0,0,255)
  for t in range(it+1):
    T = tmin+t*(tmax-tmin)/float(it)
    try:
      R=fr(T)
      turtle_x=x_0+int(R*x_map*cos(T))
      turtle_y=y_0+int(R*y_map*sin(T))
      if abs(turtle_x)<=(320.0/2.0) and abs(turtle_y)<=(220.0/2.0):
        goto(turtle_x, turtle_y)
        pendown()
      else:
        penup()
        goto(0,0)
    except:
      X=0
      Y=0
      penup()