rric

multi_loop_grid

Nov 20th, 2023 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. # Draw a rectangular grid, and display a short description of Processing
  2. # Copyright 2022, 2023 Roland Richter                    [Processing.py]
  3.  
  4. # This sketch shows you how to ...
  5. # - [ ] draw a rectangular grid with two nested for loops
  6.  
  7. def setup():
  8.     # Create a 900×600 canvas (plus a small margin) with black background
  9.     size(904, 604)
  10.     ellipseMode(CENTER)
  11.  
  12.    
  13. def draw():
  14.     background(0)
  15.    
  16.     # This sentence is taken from https://py.processing.org/
  17.     words = ["Since", "2001", "Processing", "has", "promoted", "software",
  18.             "literacy", "within", "the", "visual", "arts", "and",
  19.             "visual", "literacy", "within", "technology"]
  20.  
  21.     # Set coordinates of the circles' centers (x and y separately)
  22.     # TRY to use list comprehension to compute these numbers;
  23.     #     best with the help of the width and height variables.
  24.     cxs = [112, 112+225, 112+2*225, 112+3*225]
  25.     cys = [75, 75+150, 75+2*150, 75+3*150]
  26.    
  27.     # TRY to  reverse y coordinates, or both x and y coordinates.
  28.     # TRY to "reverse" the text as well: "Since" -> "ecniS", "2001" -> "1002", ...
  29.     if mousePressed and mouseButton == LEFT:
  30.         cxs.reverse()
  31.  
  32.     # Draw circles with white lines and black interior
  33.     strokeWeight(4)
  34.     stroke(255)
  35.     fill(0)
  36.  
  37.     # First draw 4 × 4 circles, ...
  38.     for cx in cxs:
  39.         for cy in cys:
  40.             circle(cx, cy, 215)
  41.    
  42.     # ... (just for fun, add a little offset to the word positions) ...
  43.     off = 8.0 * sin(radians(4*frameCount))
  44.    
  45.     # ... then place one word within each circle.
  46.     textSize(36)
  47.     textAlign(CENTER, CENTER)
  48.     fill(255)
  49.    
  50.     i = 0
  51.     for cy in cys:
  52.         for cx in cxs:
  53.             sign = +1 if i % 2 == 0 else -1
  54.             text(words[i], cx, cy + sign*off)
  55.             i += 1
  56.  
  57.  
  58. # ----------------------------------------------------------------------
  59. # This program is free software: you can redistribute it and/or modify
  60. # it under the terms of the GNU General Public License as published by
  61. # the Free Software Foundation, either version 3 of the License, or
  62. # (at your option) any later version.
  63. #
  64. # This program is distributed in the hope that it will be useful,
  65. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  66. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  67. # GNU General Public License for more details.
  68. #
  69. # You should have received a copy of the GNU General Public License
  70. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment