Wednesday, November 30, 2022

Python Turtle Graphics Drawing - Superman Logo

 Python Turtle Graphics Drawing - Superman Logo

Youtube


Bézier curve algorithm is from wikipedia


Source code:

from turtle import *
from math import atan2, degrees

D = 0.1  # bezier step
R = '#C80000'
Y = '#FFEB00'
K = '#000000'


def drawPt(ar): #not used
    p = pos()
    h = heading()
    up()
    color('lime')
    for a in ar:
        goto(a)
        seth(0)
        fd(5)
        lt(90)
        begin_fill()
        circle(5)
        end_fill()
    goto(p)
    seth(h)


def Bez(a, b, c, d):
    up()
    goto(a)
    down()
    t = 0.0
    while t <= 1.0:
        t3 = t**3
        t2 = t**2
        f1 = -t3+3*t2-3*t+1
        f2 = 3*t3-6*t2+3*t
        f3 = -3*t3+3*t2
        f4 = t3
        x = a[0]*f1 + b[0]*f2 + c[0]*f3 + d[0]*f4
        y = a[1]*f1 + b[1]*f2 + c[1]*f3 + d[1]*f4
        d1 = -3*t2+6*t-3
        d2 = 9*t2-12*t+3
        d3 = -9*t2+6*t
        d4 = 3*t2
        dx = a[0]*d1 + b[0]*d2 + c[0]*d3 + d[0]*d4
        dy = a[1]*d1 + b[1]*d2 + c[1]*d3 + d[1]*d4
        angr = atan2(dy, dx)
        seth(degrees(angr))
        goto(x, y)
        t += D
    goto(d)


def Superman(sz):
    global D
    seth(270)
    up()
    fd(sz*0.5)
    down()
    color(K)
    begin_fill()  # 1
    rt(139.36)
    fd(sz*1.0245)
    rt(86.68)
    fd(sz*0.3208)
    rt(43.96)
    fd(sz*0.8727)
    rt(43.96)
    fd(sz*0.3208)
    rt(86.68)
    fd(sz*1.0245)
    end_fill()
    up()
    rt(139.36)
    fd(sz*0.0319)
    lt(40.645)
    down()
    begin_fill()  # 2
    color(R)
    fd(sz*0.9807)
    rt(86.68)
    fd(sz*0.2928)
    rt(43.96)
    fd(sz*0.8559)
    rt(43.96)
    fd(sz*0.2928)
    rt(86.68)
    fd(sz*0.9807)
    end_fill()
    up()
    rt(139.36)
    fd(sz*0.0759)
    lt(40.645)
    down()
    color('black')
    begin_fill()  # 3
    fd(sz*0.2833)
    rt(148.07)
    circle(sz*0.6521, 32.31)
    rt(145.63)
    fd(sz*0.2732)
    rt(139.36)
    end_fill()
    up()
    fd(sz*0.0329)
    down()
    begin_fill()  # 4
    color(Y)
    lt(40.645)
    fd(sz*0.1939)
    rt(144.16)
    circle(sz*0.6061, 23.52)
    rt(140.84)
    fd(sz*0.1845)
    lt(40.75)
    end_fill()
    up()
    fd(sz*0.0329)
    rt(139.36)
    fd(sz*0.7272)
    down()
    color(K)
    begin_fill()  # 5
    fd(sz*0.1478)
    rt(86.68)
    fd(sz*0.2315)
    rt(43.96)
    fd(sz*0.1107)
    lt(35.03)
    circle(-sz*0.4476, -42.87)
    end_fill()
    up()
    rt(127.21)
    fd(sz*0.7272)
    lt(139.25)
    fd(sz*0.0329)
    lt(40.75)
    fd(sz*0.7421)
    down()
    color(Y)
    begin_fill()  # 6
    fd(sz*0.0884)
    rt(86.51)
    fd(sz*0.2034)
    rt(44.19)
    fd(sz*0.0442)
    lt(42.32)
    circle(-sz*0.4684, -30.60)
    end_fill()
    up()
    color('black')
    rt(122.19)
    fd(sz*0.7421)
    rt(40.69)
    fd(sz*0.0329)
    rt(139.31)
    fd(sz*0.4045)
    down()
    begin_fill()  # 7
    fd(sz*0.2127)
    pt = pos()
    pts = [pt, pt+(sz*0.156, sz*-0.029), pt +
           (sz*0.269, sz*-0.037), pt+(sz*0.385, sz*-0.043)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*0.091, sz*-0.005), pt +
           (sz*0.249, sz*-0.008), pt+(sz*0.283, sz*-0.044)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*0.016, sz*-0.017), pt +
           (sz*0.016, sz*-0.038), pt+(sz*-0.006, sz*-0.066)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*-0.015, sz*-0.018), pt+(sz*-0.056,
           sz*-0.047), pt+(sz*-0.279, sz*-0.039)]
    Bez(*pts)
    seth(36.58+90)
    circle(sz*0.1705, 92.21)
    end_fill()
    up()
    lt(91.91)
    color(Y)
    fd(sz*0.4045)

    lt(139.25)
    fd(sz*0.0329)
    lt(40.75)
    fd(sz*0.5322)
    lt(180)

    down()
    begin_fill()  # 8
    fd(sz*0.1313)
    rt(99.58)
    circle(sz*0.1997, -80.15)
    rt(131.48)
    fd(sz*0.1140)
    pt = pos()
    pts = [pt, pt+(sz*0.0598, sz*0), pt+(sz*0.1222, sz*0.0099),
           pt+(sz*0.1441, sz*0.0418)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*0.0118, sz*0.0225), pt+(sz*0.0054,
                                              sz*0.0308), pt+(-sz*0.0348, sz*0.0423)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*-0.099, sz*0.029), pt+(sz*-0.311,
           sz*0.012), pt+(sz*-0.563, sz*0.056)]
    Bez(*pts)
    end_fill()
    goto(0, 0)
    color(K)
    seth(270)
    up()
    fd(sz*0.5-sz*0.1079)
    lt(139.355)
    fd(sz*0.6199)
    down()
    begin_fill()  # 9
    fd(sz*0.2542)
    lt(85.82)
    fd(sz*0.1851)
    lt(134.83)
    fd(sz*0.1337)
    rt(90)
    fd(sz*0.1994)
    pt = pos()
    pts = [pt, pt+(sz*-0.013, sz*0.132), pt+(sz*-0.163,
           sz*0.166), pt+(sz*-0.275, sz*0.162)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*-0.183, sz*-0.003), pt+(sz*-0.276,
           sz*-0.08), pt+(sz*-0.224, sz*-0.162)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*0.06, sz*-0.116), pt +
           (sz*0.555, sz*0.037), pt+(sz*0.664, sz*-0.19)]
    D = 0.05
    Bez(*pts)
    D = 0.1
    end_fill()
    up()
    seth(79.76)
    fd(sz*0.0362)
    color(Y)
    down()
    begin_fill()  # 10
    rt(29.69)
    fd(sz*0.2031)
    lt(85.18)
    fd(sz*0.1147)
    lt(134.74)
    fd(sz*0.1036)
    rt(90)
    fd(sz*0.2401)

    pt = pos()
    pts = [pt, pt+(sz*0.002, sz*0.159), pt+(sz*-0.208,
                                            sz*0.175), pt+(sz*-0.346, sz*0.154)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*-0.132, sz*-0.017), pt+(sz*-0.183,
           sz*-0.123), pt+(sz*-0.05, sz*-0.151)]
    Bez(*pts)
    pt = pos()
    pts = [pt, pt+(sz*0.14, sz*-0.039), pt+(sz*0.46, sz*0.041),
           pt+(sz*0.587, sz*-0.134)]
    D = 0.05
    Bez(*pts)
    end_fill()
    up()
    seth(119.09)
    fd(sz*0.2790)
    down()
    begin_fill()  # 11
    color(K)
    rt(81.13)
    circle(sz*0.0769, 62.33)
    lt(79.71)
    fd(sz*0.1135)
    rt(23.66)
    circle(sz*0.1918, -34.43)
    end_fill()
    up()
    color(Y)
    seth(87.68)
    fd(sz*0.0347)
    down()
    begin_fill()  # 12
    rt(30.36)
    circle(sz*0.0391, 29.17)
    lt(93.51)
    fd(sz*0.0242)
    rt(43.96)
    circle(sz*0.3790, -3.94)
    end_fill()
    up()
    goto(0, 0)


setup(600, 600)
bgcolor('white')
Superman(400)
hideturtle()
done()

No comments:

Post a Comment

Python Turtle Graphics Drawing - Gigabyte

 Python Turtle Graphics Drawing - Gigabyte Youtube Source code: from turtle import * setup ( 600 , 600 ) color ( '#2D68AE' ) up (...