gcircle0_1.py

Created by steveg1cmz

Created on July 02, 2023

5.97 KB

Draws filled circles (using turtle pensize technology). Parameters mimic those of SVG. Set scl=1 to use pixels, or scale drawings.


# Type your text  



""" Graphics library.
Currently just circles.

Functions: Moves
moveto 
Functions: Shapes
circle 
Functions: Text
texts (simplified text)
title

Functions suffixed X execute drawings in pixels. Functions prefixed g.

Eg Circle. The savage shape
  <circle cx="600" cy="200" r="100"
        fill="red" stroke="blue" stroke-width="10"  />
  </svg> can be mimiced by
  => gcircle(600,200,100,"red","blue",10)
 when scaling is used.

Color: syntax is one of
"red": native color ("": current turtle color)
[r,g,b]  (or red after red=...)
None or "none": transparent

Scaling: scl=1 => pixels, 0.5=>shrink
"""
__version__ = "0.1"

crid = "gcircle V"+__version__+" © 2023 SteveG1CMZ"

from turtle import *
from time import *

#half screensize common to most calculators
xscreen=int(318/2)
yscreen=int(186/2) #common 

#customise
showme=True #execute main
hasclock=True #inhibit if necessary
lineheight=18 #use in your code
#show turtle drawing (but moves omitted)
showtu=False
#default scaling (1=>pixels)
if showme:
  scl=xscreen/500
else:
  scl=1

#svg 
#opacity (affects drawing and gtext)
opacity=1 #0..1 (imp: Boolean)
#end custom

#common maths

#end common maths

#initialise constants
none="none"
rendered=0 #render timing
lastfc="black"
lastsc="black"

#svg mimic functions

#move functions

def gmoveto(x,y):
  """Mimic SVG move to.
  """
  xi=gscaler(x,scl)
  yi=gscaler(y,scl)
  gmovetox(x,y)

#shape functions

def gcircle( cx=0,cy=0,r=0,fill="black",stroke="black",width=1):
  """ Mimic SVG circle. 
    Draw a circle (mimic savage parameters).
   cx,cy: Centre position (None=0 TBD)
   r: radius 
   fill,stroke: None or "" or color
   width: of stroke 
   cx cy r width: In real

 Pixels (when scl=1).

   Imp: fill uses pensize.
   So radius > 70 will memory fail on Numworks.
   (Or even less)
  """
  if r>0:
    #print( cx,cy,r,fill,stroke,width)
    cxi=gscaler(cx,scl)
    cyi=gscaler(cy,scl)
    ri=gscaler(r,scl)
    widthi=gscaler(width,scl)
    gcirclex(cxi,cyi,ri,fill,stroke,widthi)


#text functions

def gtitle(text):
  """Title to appear outside graphics screen.
  Imp: print (assume it doesn't block graphics).
  """
  print(text)

def gtexts(x=0,y=0,text=""):
  """Simplified text. No color. No deltax.
  Use gtext instead if you need color TBD.
  Imp: The position of text is scaled, not it's size.
  """
  xi=gscaler(x,scl)
  yi=gscaler(y,scl)
  gtextsx(xi,-yi,text) #negated

#execute move functions

def gmovetox(x,y):
  """Mimic SVG moveto.
  Penup Hideturtle and goto.
  """
  penup()
  hideturtle()
  goto(x,y)

#execute shape functions

def gcirclex(cx,cy,r,fill,stroke,width=1):
  """Mimic SVG circle.
  """
  global lastfc,lastsc
  
  if r>0:     
    #,cx cy None->0 tbd
    cxi=gscaler(cx,1)
    cyi=gscaler(cy,1)
    ri=gscaler(r,1)
    widthi=gscaler(width,1)
    #print("X",scl,cxi,cyi,ri,fill, stroke,width,widthi)
    oldpensize=pensize()
    oldcolor=color()
    wasdown=isdown()

    if not(fill==None or fill==none):
      #fill
      gmovetox(cxi,-cyi) # negated 
      pensize(round(ri*2)) #filler
      if not(fill==""):
        color(fill)
      gpendown()
      forward(1) # draw something
      lastfc=color()

    if not(stroke==None or stroke==none):
      #stroke (outline)
      #should begin at 3, but begin at 6 posn
      gmovetox(cxi,-cyi-ri) #negated 
      pensize(widthi)
      if not(stroke==""):
        color(stroke)
      if showtu:
        showturtle()
      gpendown()
      circle(ri)
      lastsc=color()
          
    #restore
    pensize(oldpensize) 
    color(oldcolor) 
    if not(wasdown):
      penup() 
    #turtlepos tbd

#execute text 

def gtextsx(x=0,y=0,text=""):
  """Simplified text. No color. No deltax.
  Use gtext instead of you need color TBD.
  Imp: The position of text is scaled, not it's size.
  """
  gmoveto(x,y)
  if opacity:
    write(text)

#supporting graphic routines

def gpendown():
  """Prepare to draw, unless not opaque.
  """
  if opacity:
    pendown()

def gscaler(num,scl):
  """Round and scale a number (but keep 1 pixel).
  """
  if num==0:
    return 0
  nums = num*scl
  if nums<1:
    return 1 #at least 1 pixel
  #return scaled and rounded number
  return round(nums)

#end SVG mimics

def svgexamples():
  scl=xscreen/float(600)
  gtextsx(0,yscreen,"SteveG1CMZ")
  #gtextw(0,yscreen-18,"Lin2")
  gtextsx(-xscreen,-yscreen,"examples")
  gcircle( 0,0,60,"blue","blue",6) 
  gcircle(50,50,50) #default colours
  
  gcircle(600,200,100,"red","blue",10) #example

  #gcircle(0,-120,60,"yellow","yellow",2)
  #gcircle(0,0,60,"blue",[255,0,0],1)

def circlestest():
  """Circle test is based on SVG test.
  """
  gtitle(">shapes-circle-01")
  lime="green" #approximate missing color
  #gtexts(100,100,"A") 
  gcircle (cx=100, cy=100, r=50, fill="none",stroke="black")
  #gtexts(220,100,"B")
  gcircle (cx=220, cy=100, r=35, fill="red",stroke="black")
  #gtexts(340,100,"C")
  gcircle (cx=340, cy=100, r=20, fill="black",stroke=lime, width=4)
  #gtexts(100,260,"D")
  gcircle ( cx=100, cy=260, r=20, stroke=lime,fill="yellow", width=4)
  #gtexts(220,260,"E")
  gcircle (cx=220, cy=260, r=35, stroke="none",fill="blue")
  #gtexts(340,260,"F")
  hideturtle()
  gcircle (cx=340, cy=260, r=50, stroke="red",fill="none", width=10)

  #return 
  gtitle(">shapes-circle-02")
  # color "green" stroke="#000000"
  fc = "green"
  sc = "#000000"
  #gtexts(0,0,"a")
  gcircle (r=50, fill=fc, stroke=sc)
  #gtexts(0,100,"b")
  gcircle (cy=100, r=50, fill=fc,stroke=sc)
  #gtexts(100,0,"c")
  gcircle (cx=100, r=50, fill=fc, stroke=sc)
  gcircle (cx=100, cy=100, r=0, fill="red",stroke=sc)
  #gtexts(100,100,"d")
  gcircle( cx=100, cy=100, r=50, fill=fc,stroke=sc)
    

  #print("tested")



def main():
  gtitle(crid)
  penup(); hideturtle()
  goto(-xscreen,yscreen) #-yscreen-20)
  write(crid)
  if hasclock:
    renderbegin=monotonic()
  #svgexamples()
  circlestest()
  
  if hasclock:
    rendered=monotonic()
    rendertakes=round(rendered-renderbegin,3)
    if True:
      print(" rendered in ",round(rendertakes),"s")

if showme or __name__ == "__main__":
  main()

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.