Advertisement
rric

loop_long_lists

Nov 20th, 2023 (edited)
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.97 KB | None | 0 0
  1. # Paints a sequence of numbers as a reference to Roman Opałka's painting
  2. # series 1965/1-∞, see https://en.wikipedia.org/wiki/Roman_Opalka
  3. # Copyright 2022, 2023 Roland Richter                    [Processing.py]
  4.  
  5. # This sketch shows you how to ...
  6. # - [ ] test whether an item is in a list or not
  7. # - [ ] set a different mouse cursor in Processing
  8. # - [ ] fill an empty list within a while loop
  9. # - [ ] call a function for every item in a list
  10. # - [ ] define a function with default arguments
  11.  
  12. from __future__ import division, print_function
  13.  
  14. def setup():
  15.     size(800, 560)
  16.  
  17.     # Use a handwritten font of size 24; either "Comic Sans" or "Ruji's"
  18.     installed_fonts = PFont.list()
  19.    
  20.     if "Comic Sans MS" in installed_fonts:
  21.         print("Hooray! Comic Sans is here!")
  22.         comic_sans = createFont("Comic Sans MS", 24)
  23.         textFont(comic_sans)
  24.     else:
  25.         print("Boo! Comic Sans is missing!")
  26.         # Ruji's Handwriting Font v.2.0, by Ruji C., OFL (SIL Open Font License)
  27.         # https://fontlibrary.org/en/font/ruji-s-handwriting-font-v-2-0
  28.         rujis = createFont("rujis_handwriting.ttf", 24)
  29.         textFont(rujis)
  30.  
  31.     # Set the mouse cursor to a different icon, shaped like a "+",
  32.     # see https://py.processing.org/reference/cursor.html
  33.     cursor(CROSS)
  34.  
  35.  
  36. def draw():
  37.     background("#0F0F0F")
  38.    
  39.     if mousePressed:
  40.         draw_grid()
  41.    
  42.     # Set the coordinates of start point
  43.     # TRY different start points
  44.     start_x, start_y = width/20, height/2
  45.    
  46.     # Later, we call that a vector from (start_x,start_y) to (mouseX,mouseY),
  47.     # scaled by a factor of 0.4
  48.     # TRY factors of 0.5, 1.0, or 0.2 -- can you see what happens?
  49.     factor = 0.4
  50.     step_x, step_y = factor*(mouseX - start_x), factor*(mouseY - start_y)
  51.    
  52.     # Make sure step is not (0,0), since that would cause an infinite loop below.
  53.     if step_x == 0 and step_y == 0:
  54.         step_x = random(5)
  55.         step_y = random(-5, 5)
  56.  
  57.     # Generate a list of tuples of the form (number, x coordinate, y coordinate):
  58.     #   - number 1 is located at the starting point
  59.     #   - number 2 is moved one step away from the previous point (which
  60.     #         was the starting point)
  61.     #   - number 3 is moved one step away from the previous point (which
  62.     #         was the position of number 2)
  63.     # ...
  64.     numbers = []
  65.    
  66.     number = 1
  67.     pos_x, pos_y = start_x, start_y
  68.    
  69.     # ...; generate tuples until their (x,y) position is no longer within
  70.     # the display window.
  71.     while is_displayable(pos_x, pos_y):
  72.         numbers.append((number, pos_x, pos_y))
  73.         number += 1
  74.         pos_x += step_x
  75.         pos_y += step_y
  76.  
  77.     # Show some debug info every second
  78.     if frameCount % 60 == 0:
  79.         print(len(numbers), "items in the list:")
  80.         print("  first item:", numbers[0])
  81.         print("  last item:", numbers[-1])
  82.    
  83.     # Finally, use the generated list to paint the numbers at their positions
  84.     # TRY to use a different radius, and/or different colours here
  85.     for num in numbers:
  86.         paint_number_at(num[0], num[1], num[2])
  87.    
  88.  
  89. def draw_grid(horz_step = 40, vert_step = 40):
  90.     """Draw a grid of vertical and horizontal grey lines.
  91.    
  92.    Arguments:
  93.    horz_step -- vertical lines are drawn every `horz_step` pixels (default: 40)
  94.    vert_step -- horizontal lines are drawn every `vert_step` pixels (default: 40)
  95.    """
  96.     stroke(92)
  97.     fill(192)
  98.     textSize(12)
  99.     textAlign(CENTER, CENTER)
  100.    
  101.     # Draw vertical lines, and place a label at the top of each line
  102.     for x in range(0, width, horz_step):  
  103.         line(x, 0, x, height)
  104.         text(x, x, 12 - 0.15 * textAscent())
  105.        
  106.     # Draw horizontal lines, and place a label at the left of each line
  107.     for y in range(0, height, vert_step):
  108.         line(0, y, width, y)
  109.         text(y, 12, y - 0.15 * textAscent())
  110.  
  111.  
  112. def is_displayable(x, y):
  113.     """Return True if (x, y) is within the display window, False otherwise"""
  114.     if x >= 0 and x < width and y >= 0 and y < height:
  115.         return True
  116.     else:
  117.         return False
  118.  
  119.  
  120. def paint_number_at(number, x, y, r=42, fillcolor="#778899", textcolor="#F8F8FF"):
  121.     """Paint a circled number at the given position, with given colors.
  122.    
  123.    Arguments:
  124.    number -- the number to paint
  125.    x -- the x position to paint at
  126.    y -- the y position to paint at
  127.    r -- the radius of the circle (default: 42)
  128.    fillcolor -- the color to fill the circle (default: Light slate gray)
  129.    textcolor -- the color to write the number (default: Ghost white)
  130.    """
  131.    
  132.     # circle attributes: no stroke, in given color
  133.     noStroke()
  134.     fill(fillcolor)    
  135.     circle(x, y, r)
  136.    
  137.     # text attributes: size 24, centered, in given color
  138.     fill(textcolor)
  139.     textSize(24)
  140.     textAlign(CENTER, CENTER)
  141.    
  142.     # https://py.processing.org/reference/textAlign.html:
  143.     # "The vertical alignment is based on the value of textAscent(),
  144.     # which many fonts do not specify correctly. It may be necessary
  145.     # to use a hack and offset by a few pixels by hand so that the
  146.     # offset looks correct. To do this as less of a hack, use some
  147.     # percentage of textAscent() or textDescent() ..."
  148.     text(number, x, y - 0.15 * textAscent())
  149.    
  150.  
  151. # ----------------------------------------------------------------------
  152. # This program is free software: you can redistribute it and/or modify
  153. # it under the terms of the GNU General Public License as published by
  154. # the Free Software Foundation, either version 3 of the License, or
  155. # (at your option) any later version.
  156. #
  157. # This program is distributed in the hope that it will be useful,
  158. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  159. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  160. # GNU General Public License for more details.
  161. #
  162. # You should have received a copy of the GNU General Public License
  163. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  164.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement