Sunday, June 28, 2020

Numworks Python Scripts: Basic Graphics

Numworks Python Scripts:  Basic Graphics

Script:  atari.py

Draws the basic-eight color palette of the classic 1977 Atari 2600.

Atari Palette Python Script
Atari Palette Python Script


from math import *
from kandinsky import *
# 2020-05-28 atari 2600 colors
# kandinsky module

fill_rect(0,0,320,240,color(245,245,245))

fill_rect(15,15,55,55,color(0,0,0))
fill_rect(85,15,55,55,color(255,0,0))
fill_rect(155,15,55,55,color(255,255,0))
fill_rect(15,85,55,55,color(255,0,255))
fill_rect(155,85,55,55,color(0,255,0))
fill_rect(15,155,55,55,color(0,255,255))
fill_rect(85,155,55,55,color(0,0,255))
fill_rect(155,155,55,55,color(255,255,255))

draw_string("8",107,107)


Script:  firstdigit.py

The tenths digit from n random numbers is extracted and a bar chart is generated based on the results.  I recommend a sample size of at least 20.

firstdigit script example (sample = 100)
A Sample of 100 data points



from math import *
from random import *
from matplotlib.pyplot import *

# set up lists
x=[0,1,2,3,4,5,6,7,8,9]
y=[0,0,0,0,0,0,0,0,0,0]

# user iput
print("EWS 2020-05-29")
print("Bar Chart: First Digit")
print("Recommended at least 20")
n=int(input("n? "))

# generate list
for i in range(n):
  s=int(random()*10)
  y[s]=y[s]+1

# bar plot
h=int(n/2)
d=-int(h/4)
axis([-0.5,9.5,d,h])
bar(x,y)

# turn axis off
axis("off")

# labels at the bottom
# results at top
m=max(y)
for i in range(10):
  text(i-0.25,d+1,str(i))
  text(i-0.25,m+2,str(y[i]))
  
show()

Script:  colorfulrings.py

The script cycles through a set of nine colors, four times.   The Kandinsky module is used to generate the flowery circles as well as cycle through the colors.  This module works with integer pixels.

Color rings script in progress
Color rings script in progress


from math import *
from kandinsky import *
from time import *

# color lists
r=[255,255,255,0,0,0,51,128,255]
g=[0,102,255,128,255,0,102,128,255]
b=[0,0,0,0,0,255,255,128,255]

# angles
a=list(range(128))
for i in range(128):
  a[i]=i/128*2*pi

# draw circles
for k in range(36):
  n=int(fmod(k,9))
  for j in range(50):
    for i in range(128):
      x=int(160+(20+j)*cos(a[i]))
      y=int(120+(20+j)*sin(a[i]))
      set_pixel(x,y,color(r[n],g[n],b[n]))
  sleep(0.1)

Script:  modulusplot.py

Generate a pixel plot of the equation (x^n + y^n) mod m

Modulus Plot example, n = 3.9, m = 15.6
Input Screen  (n = 3.9, m = 15.6)

Modulus Plot result, n = 3.9, m = 15.6
Modulus Plot result, n = 3.9, m = 15.6


from math import *
from kandinsky import *
print("EWS 2020-05-28")
print("x**n + y**n mod m")
n=float(input("power? "))
m=float(input("modulus? "))

for x in range(320):
  for y in range(240):
    t=fmod(pow(x,n)+pow(y,n),m)  
    c=floor(t/m*255)
    set_pixel(x+1,y+1,color(c,c,c))


Eddie

BLOG UPDATE:
The HP Prime:  Conversion to Binary and IEEE-754 Binary blog entry that was posted on June 27, 2020 may contain errors.  In this case, I have taken that entry back to draft status and my intention is to repost the entry as soon as I can.  Apologies for any inconvenience. 

All original content copyright, © 2011-2020.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author.

Spotlight: Akron Brass FireCalc Pocket Computer

Spotlight: Akron Brass FireCalc Pocket Computer Welcome to a special Monday Edition of Eddie’s Math and Calculator blog. Thi...