Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Paints a sequence of numbers as a reference to Roman Opałka's painting
- # series 1965/1-∞, see https://en.wikipedia.org/wiki/Roman_Opalka
- # Copyright 2022, 2023 Roland Richter [Processing.py]
- # This sketch shows you how to ...
- # - [ ] test whether an item is in a list or not
- # - [ ] set a different mouse cursor in Processing
- # - [ ] fill an empty list within a while loop
- # - [ ] call a function for every item in a list
- # - [ ] define a function with default arguments
- from __future__ import division, print_function
- def setup():
- size(800, 560)
- # Use a handwritten font of size 24; either "Comic Sans" or "Ruji's"
- installed_fonts = PFont.list()
- if "Comic Sans MS" in installed_fonts:
- print("Hooray! Comic Sans is here!")
- comic_sans = createFont("Comic Sans MS", 24)
- textFont(comic_sans)
- else:
- print("Boo! Comic Sans is missing!")
- # Ruji's Handwriting Font v.2.0, by Ruji C., OFL (SIL Open Font License)
- # https://fontlibrary.org/en/font/ruji-s-handwriting-font-v-2-0
- rujis = createFont("rujis_handwriting.ttf", 24)
- textFont(rujis)
- # Set the mouse cursor to a different icon, shaped like a "+",
- # see https://py.processing.org/reference/cursor.html
- cursor(CROSS)
- def draw():
- background("#0F0F0F")
- if mousePressed:
- draw_grid()
- # Set the coordinates of start point
- # TRY different start points
- start_x, start_y = width/20, height/2
- # Later, we call that a vector from (start_x,start_y) to (mouseX,mouseY),
- # scaled by a factor of 0.4
- # TRY factors of 0.5, 1.0, or 0.2 -- can you see what happens?
- factor = 0.4
- step_x, step_y = factor*(mouseX - start_x), factor*(mouseY - start_y)
- # Make sure step is not (0,0), since that would cause an infinite loop below.
- if step_x == 0 and step_y == 0:
- step_x = random(5)
- step_y = random(-5, 5)
- # Generate a list of tuples of the form (number, x coordinate, y coordinate):
- # - number 1 is located at the starting point
- # - number 2 is moved one step away from the previous point (which
- # was the starting point)
- # - number 3 is moved one step away from the previous point (which
- # was the position of number 2)
- # ...
- numbers = []
- number = 1
- pos_x, pos_y = start_x, start_y
- # ...; generate tuples until their (x,y) position is no longer within
- # the display window.
- while is_displayable(pos_x, pos_y):
- numbers.append((number, pos_x, pos_y))
- number += 1
- pos_x += step_x
- pos_y += step_y
- # Show some debug info every second
- if frameCount % 60 == 0:
- print(len(numbers), "items in the list:")
- print(" first item:", numbers[0])
- print(" last item:", numbers[-1])
- # Finally, use the generated list to paint the numbers at their positions
- # TRY to use a different radius, and/or different colours here
- for num in numbers:
- paint_number_at(num[0], num[1], num[2])
- def draw_grid(horz_step = 40, vert_step = 40):
- """Draw a grid of vertical and horizontal grey lines.
- Arguments:
- horz_step -- vertical lines are drawn every `horz_step` pixels (default: 40)
- vert_step -- horizontal lines are drawn every `vert_step` pixels (default: 40)
- """
- stroke(92)
- fill(192)
- textSize(12)
- textAlign(CENTER, CENTER)
- # Draw vertical lines, and place a label at the top of each line
- for x in range(0, width, horz_step):
- line(x, 0, x, height)
- text(x, x, 12 - 0.15 * textAscent())
- # Draw horizontal lines, and place a label at the left of each line
- for y in range(0, height, vert_step):
- line(0, y, width, y)
- text(y, 12, y - 0.15 * textAscent())
- def is_displayable(x, y):
- """Return True if (x, y) is within the display window, False otherwise"""
- if x >= 0 and x < width and y >= 0 and y < height:
- return True
- else:
- return False
- def paint_number_at(number, x, y, r=42, fillcolor="#778899", textcolor="#F8F8FF"):
- """Paint a circled number at the given position, with given colors.
- Arguments:
- number -- the number to paint
- x -- the x position to paint at
- y -- the y position to paint at
- r -- the radius of the circle (default: 42)
- fillcolor -- the color to fill the circle (default: Light slate gray)
- textcolor -- the color to write the number (default: Ghost white)
- """
- # circle attributes: no stroke, in given color
- noStroke()
- fill(fillcolor)
- circle(x, y, r)
- # text attributes: size 24, centered, in given color
- fill(textcolor)
- textSize(24)
- textAlign(CENTER, CENTER)
- # https://py.processing.org/reference/textAlign.html:
- # "The vertical alignment is based on the value of textAscent(),
- # which many fonts do not specify correctly. It may be necessary
- # to use a hack and offset by a few pixels by hand so that the
- # offset looks correct. To do this as less of a hack, use some
- # percentage of textAscent() or textDescent() ..."
- text(number, x, y - 0.15 * textAscent())
- # ----------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement