Unknown Pleasures Tutorial



  • Hey there I was playing trying to recreate the Unknown Pleasures cover art, and this is the result if you've ever done it before I would love to see how did you solve it.

    Cheers!!

    # Tribute for Joy Division's Unknown Pleasures album cover
    
    side = 1080
    
    def lsPts(num):
        points = []
        m = side*0.3
        for i in range(0, side, 12):
            if i <= side*0.2:
                x = i
                y = sin((i*(randint(1, 3))))
                points.append((x, y))
            if i > side*0.2 and i <= side*0.4:
                x = i
                y = i*sin(radians(i*(randint(-100, 100))))/(10**1)
                if y < 0:
                    points.append((x, y * -1))
                else:
                    points.append((x, y))
            if i > side*0.4 and i <= side*0.6:
                x = i
                y = m*sin(radians(m*(randint(-100, 100))))/(10**1)
                if y < 0:
                    points.append((x, y * -1))
                else:
                    points.append((x, y))
                m -= 1
            if i > side*0.6 and i <= side*0.8:
                x = i
                y = sin((i*(randint(1, 3))))
                points.append((x, y))
        return points
    
    def lineGrid(steps):
        for j in range(steps):
            newPage(side, side)
            fill(0)
            rect(0, 0, side, side)
            m = 0
            for i in range(int(side*0.8/13.5)):
                points = lsPts(1)
                fill(None)
                stroke(1)
                strokeWidth(1)
                path = BezierPath()
                with savedState():
                    translate(side*0.1, side*0.1 + m)
                    path.moveTo((0, 0))
                    path.curveTo(*points)
                    drawPath(path)
                m += 13.5
    
    for i in range(12):
        lineGrid(i)
    
    saveImage('~/Desktop/unknown_pleasures.gif')
    


  • hi @eduairet
    nice one! I don’t think I have ever tried that one myself. I had a look at your code and have some suggestions that might shorten it but hopefully keep it readable.

    Here are some comments on the lsPts function:

    The if statements could be written without the and:

    if min_limit < value <= max_limit: 
    

    Not sure why you are doing 10**1 since it is the same as 10

    All the ifs could probably be an if and some elifs and a final else so not every value gets checked four times.

    DRY! The line x = i is used four times but since you only change y you could write this line outside of your ifs just once. Also the points.append() could happen just at the end of the ifs.

    The lsPts() function takes the parameter num but it never gets used. Maybe this could be the gap between the points?

    Maybe some of these comments are helpful — Have fun!



  • Thank you @jo I've worked with your suggestions, which I really appreciate. That 10¹, haha, I was playing with the power of ten and I ended up that 10¹ was the best but I never erased it 😂 I've used the num in another exercise (https://github.com/eduairet/eat36daysOfType2020/blob/master/17_36_DAYS_OF_TYPE_2020.py) but there's no point in this one. This is the code after suggestions, way much better:

    # Tribute for Joy Division's Unknown Pleasures album cover
    
    side = 1080
    
    def lsPts():
        points = []
        m = side*0.3
        for i in range(0, int(side*0.8), 12):
            x = i
            if side*0.2 >= i < side*0.4 or side*0.6 < i <= side*0.8:
                y = sin((i*(randint(1, 3))))
            elif side*0.2 < i <= side*0.4:
                y = i*sin(radians(i*(randint(-100, 100))))/10
            elif side*0.4 < i <= side*0.6:
                y = m*sin(radians(m*(randint(-100, 100))))/10
                m -= 1
            if y < 0:
                points.append((x, y * -1))
            else:
                points.append((x, y))
        return points
    
    print(lsPts())
    
    def lineGrid(steps):
        for j in range(steps):
            newPage(side, side)
            fill(0)
            rect(0, 0, side, side)
            m = 0
            for i in range(int(side*0.8/13.5)):
                points = lsPts()
                fill(None)
                stroke(1)
                strokeWidth(1)
                path = BezierPath()
                with savedState():
                    translate(side*0.1, side*0.1 + m)
                    path.moveTo((0, 0))
                    path.curveTo(*points)
                    drawPath(path)
                m += 13.5
    
    for i in range(12):
        lineGrid(i)
    
    saveImage('~/Desktop/unknown_pleasures.gif')
    


  • @eduairet nice!
    I am happy you found the comments helpful! So I will mention one more about the page count. The 66 pages confused me at first since I did not see them assigned anywhere. Until I realized that you are generating the Triangular Number since you are looping twice. Not sure if you are doing this on purpose but maybe the last loop

    for i in range(12):
        lineGrid(i)
    

    could just be.

    lineGrid(12)
    

    or

    lineGrid(66)
    

    if you really wanted exactly that amount of frames.



  • @jo Thank you, I was disconnected from Drawbot so I haven't seen this, I'll try your suggestions on the code


Log in to reply