Created
          November 27, 2018 19:44 
        
      - 
      
- 
        Save villares/af1f6b8e69d4c736224049c4f0c1e97b to your computer and use it in GitHub Desktop. 
    NDP2018
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day | |
| SKETCH_NAME = "ndp2018" # 20181127 | |
| OUTPUT = ".png" | |
| GRID_SIZE = 6 | |
| add_library('pdf') | |
| rule = lambda x, y: random(40) < 20 | |
| def setup(): | |
| global export | |
| export = True | |
| #strokeCap(SQUARE) | |
| size(700, 700) | |
| smooth(8) | |
| init_grid(GRID_SIZE) | |
| def draw(): | |
| background(200) | |
| stroke(0) | |
| # f = 0 #frameCount/10. | |
| # t = 0.5 + sin(f)/2 | |
| if export: | |
| beginRecord(PDF, "line1.pdf") | |
| for c in Cell.cells: | |
| c.plot(0) | |
| for c in Cell.cells: | |
| c.plot(1) | |
| if export: | |
| endRecord() | |
| global export | |
| export = False | |
| # if f < TWO_PI: | |
| # saveFrame("###.png") | |
| # else: | |
| # exit() | |
| def init_grid(grid_size): | |
| Cell.border = 75. | |
| Cell.spacing = (width - Cell.border *2) / grid_size | |
| Cell.cells = [] | |
| randomSeed(1) | |
| for x in range(0, grid_size, 2): | |
| for y in range(0, grid_size, 2): | |
| new_cell = Cell(x, y) | |
| Cell.cells.append(new_cell) | |
| Cell.grid[x, y] = new_cell | |
| randomSeed(frameCount) | |
| for x in range(-1, grid_size+1, 2): | |
| for y in range(-1, grid_size+1, 2): | |
| Node.grid0[x, y] = Node(x, y) # extrarir do dict | |
| Node.grid1[x, y] = Node(x, y) # extrarir do dict | |
| for c in Cell.cells: | |
| c.update_vers() | |
| class Node(): | |
| grid0 = dict() | |
| grid1 = dict() | |
| def __init__(self, x, y): | |
| self.ix = x | |
| self.iy = y | |
| self.px = Cell.border + Cell.spacing + x * Cell.spacing | |
| self.py = Cell.border + Cell.spacing + y * Cell.spacing | |
| if rule(x, y): | |
| mx, my = width/2, height/2 | |
| self.px += (self.px - mx) * 0.15 | |
| self.py += (self.py - my) * 0.15 | |
| self.x = self.px | |
| self.y = self.py | |
| class Cell(): | |
| cells = [] | |
| grid = dict() | |
| def __init__(self, x, y): | |
| self.ix = x | |
| self.iy = y | |
| self.px = Cell.border + Cell.spacing + x * Cell.spacing | |
| self.py = Cell.border + Cell.spacing + y * Cell.spacing | |
| self.vers = [] | |
| self.num_hatches = int(random(5, 12)) | |
| self.type_hatches = random(10) | |
| def plot(self, t): | |
| L0, V0 = self.lines, self.vers | |
| L1, V1 = self.lines_f, self.vers_f | |
| strokeWeight(1) | |
| for l0, l1 in zip(L0, L1): | |
| l = l0.lerp(l1, t) | |
| l.plot() | |
| beginShape() | |
| noFill() | |
| strokeWeight(1) | |
| for p0, p1 in zip(V0, V1): | |
| vertex(lerp(p0.x, p1.x, t), lerp(p0.y, p1.y, t) ) | |
| endShape(CLOSE) | |
| def update_vers(self): | |
| self.v0 = Node.grid0.get((self.ix-1, self.iy-1)) | |
| self.v1 = Node.grid0.get((self.ix-1, self.iy+1)) | |
| self.v3 = Node.grid0.get((self.ix+1, self.iy-1)) | |
| self.v2 = Node.grid0.get((self.ix+1, self.iy+1)) | |
| self.vers = [self.v0, self.v1, self.v2, self.v3] | |
| self.v0f = Node.grid1.get((self.ix-1, self.iy-1)) | |
| self.v1f = Node.grid1.get((self.ix-1, self.iy+1)) | |
| self.v3f = Node.grid1.get((self.ix+1, self.iy-1)) | |
| self.v2f = Node.grid1.get((self.ix+1, self.iy+1)) | |
| self.vers_f = [self.v0f, self.v1f, self.v2f, self.v3f] | |
| self.hatch() | |
| def hatch(self): | |
| self.lines = [] | |
| self.lines_f = [] | |
| n = self.num_hatches | |
| r = self.type_hatches | |
| if r > 2: | |
| self.lines.extend(par_hatch(self.vers, n, 0)) | |
| self.lines_f.extend(par_hatch(self.vers_f, n, 0)) | |
| if r < 8: | |
| self.lines.extend(par_hatch(self.vers, n, 1)) | |
| self.lines_f.extend(par_hatch(self.vers_f, n, 1)) | |
| def keyPressed(): | |
| if key == "n" or key == CODED: | |
| init_grid(GRID_SIZE) | |
| #saveFrame("###.png") | |
| if key == "s": saveFrame("###.png") | |
| # print text to add to the project's README.md | |
| def settings(): | |
| println( | |
| """ | |
|  | |
| {1}: [code](https://github.com/villares/sketch-a-day/tree/master/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)] | |
| """.format(SKETCH_NAME, SKETCH_NAME[1:], OUTPUT) | |
| ) | |
| class Line(): | |
| """ I should change this to a named tuple... """ | |
| def __init__(self, p1, p2): | |
| self.p1 = p1 | |
| self.p2 = p2 | |
| def plot(self): | |
| line(self.p1.x, self.p1.y, self.p2.x, self.p2.y) | |
| def lerp(self, other, t): | |
| p1 = PVector.lerp(self.p1, other.p1, t) | |
| p2 = PVector.lerp(self.p2, other.p2, t) | |
| return Line(p1, p2) | |
| def edges(poly_points): | |
| return pairwise(poly_points) + [(poly_points[-1], poly_points[0])] | |
| def pairwise(iterable): | |
| import itertools | |
| "s -> (s0,s1), (s1,s2), (s2, s3), ..." | |
| a, b = itertools.tee(iterable) | |
| next(b, None) | |
| return zip(a, b) | |
| def par_hatch(points, divisions, *sides): | |
| vectors = [PVector(p.x, p.y) for p in points] | |
| lines = [] | |
| if not sides: sides = [0] | |
| for s in sides: | |
| a, b = vectors[-1 + s], vectors[+0 + s] | |
| d, c = vectors[-2 + s], vectors[-3 + s] | |
| for i in range(1, divisions): | |
| s0 = PVector.lerp(a, b, i/float(divisions)) | |
| s1 = PVector.lerp(d, c, i/float(divisions)) | |
| lines.append(Line(s0, s1)) | |
| return lines | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment