graphics.py

Created by jamac

Created on April 09, 2019

921 Bytes

a simple graphics library


from kandinsky import *
from math import *

def rgb(c):
  r=int(c/2**11)
  g=int((c-(r*2**11))/2**5)
  b=int((c-(r*2**11)-(g*2**5)))
  return (r*8,g*4,b*8)

def grect(p,s,c=(0,0,0)):
  c=color(*c)
  for y in range(s[1]):
    for x in range(s[0]):
      set_pixel(int(p[0]+x),int(p[1]+y),c)

def gcircle(p,r,c=(0,0,0)):
  c=color(*c)
  for x in range(int(1-r),int(r)):
    h=int((r**2-x**2)**0.5)
    for y in range(1-h,h):
      set_pixel(int(p[0]+x),int(p[1]+y),c)

def aacircle(p,r,c=(0,0,0),g=1):
  for x in range(int(-r-g),int(r+g+1)):
    h=int(((r+g+1)**2-x**2)**0.5)
    for y in range(int(1-h),int(h)):
      gx=int(p[0]+x)
      gy=int(p[1]+y)
      d=(x**2+y**2)**0.5
      if(d<r-g-1):
        set_pixel(gx,gy,color(*c))
      else:
        f=min(max((d-r+g)/2/g,0),1)
        bc=rgb(get_pixel(gx,gy))
        nc=list(map(lambda a,b:int((a*(1-f)+b*f)),c,bc))
        set_pixel(gx,gy,color(*nc))

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.