Sunday, December 4, 2022

Python Turtle Graphics Drawing - Lego® Logo

 Python Turtle Graphics Drawing - Lego® Logo

Youtube

Draw Lego® Logo using bézier curve in Python Turtle Graphics.


Source code:
from turtle import *
from math import atan2, degrees

SCL = 15 # scale
D = 0.05 # bezier step

RED ="#E3000B"

BLACK = [
    (-8.021, 5.702), (-6.187, 8.610),  (-0.952, 8.484),
    (0.279, 6.585),  (2.459, 8.425),  (7.088, 8.576),
    (8.547, 5.896),     (11.516, 9.181),
    (16.352, 8.030),
    (17.192, 5.266),
    (18.104, 1.807),
    (15.322, -4.754),
    (13.437, -6.111),
    (10.494, -8.383),
    (6.377, -7.500),
    (5.573, -4.997),
    (2.619, -8.360),
    (-1.321, -7.782),
    (-2.916, -5.956),
    (-5.379, -8.150),
    (-8.781, -7.604),
    (-10.161, -6.119),
    (-11.854, -7.789),
    (-16.040, -8.061),
    (-17.176, -5.390),
    (-18.144, -2.317),
    (-14.153, 6.068),
    (-12.892, 7.018),
    (-11.372, 8.329),
    (-7.892, 8.228),
    (-8.021, 5.702),
]
YELLOW = [
    (-7.607, 7.616),
    (-5.767, 9.203),
    (-1.112, 9.387),
    (0.394, 7.868),
    (2.755, 9.292),
    (6.670, 9.368),
    (8.718, 7.430),
    (13.184, 10.584),
    (17.470, 8.199),
    (18.132, 5.615),
    (18.961, 1.045),
    (16.348, -5.013),
    (14.556, -6.396),
    (11.114, -9.936),
    (6.342, -8.045),
    (5.371, -6.618),
    (2.263, -9.203),
    (-1.329, -8.591),
    (-2.927, -7.285),
    (-6.230, -9.537),
    (-9.681, -7.869),
    (-10.134, -7.409),
    (-12.192, -8.530),
    (-16.480, -9.380),
    (-18.131, -5.737),
    (-19.427, -2.469),
    (-14.982, 7.106),
    (-13.206, 8.014),
    (-11.782, 8.900),
    (-8.795, 9.450),
    (-7.607, 7.616),
]

C_L = [
    (-12.735, -2.699),
    (-11.428, -2.557),
    (-10.127, -2.461),
    (-10.333, -4.061),
    (-10.538, -5.660),
    (-12.689, -6.635),
    (-14.932, -6.004),
    (-17.176, -5.374),
    (-15.823, -1.896),
    (-15.386, -0.627),
    (-14.949, 0.642),
    (-12.849, 5.809),
    (-11.698, 6.372),
    (-10.673, 6.842),
    (-9.093, 6.834),
    (-9.379, 5.246),
    (-9.690, 3.271),
    (-11.630, -0.384),
    (-12.735, -2.699),
]

C_E = [
    (-5.223, 1.899),
    (-3.319, 2.169),
    (-2.318, 1.781),
    (-2.648, 0.598),
    (-2.996, -0.648),
    (-3.911, -1.304),
    (-6.248, -0.863),
    (-6.467, -1.488),
    (-6.696, -2.097),
    (-6.805, -2.749),
    (-4.598, -2.167),
    (-3.148, -2.592),
    (-3.249, -3.834),
    (-3.315, -4.980),
    (-4.218, -6.131),
    (-6.915, -6.223),
    (-9.532, -6.112),
    (-10.326, -4.355),
    (-9.711, -1.796),
    (-9.108, 1.001),
    (-7.509, 5.160),
    (-6.140, 5.942),
    (-4.625, 7.071),
    (-0.984, 7.112),
    (-0.662, 5.564),
    (-0.310, 4.069),
    (-2.093, 3.157),
    (-4.493, 3.451),
    (-4.863, 2.875),
    (-5.079, 2.502),
    (-5.223, 1.899),
]

C_G = [
    (3.061, -1.293),
    (1.210, -1.243),
    (2.186, 0.937),
    (3.125, 1.222),
    (4.408, 1.585),
    (6.328, 1.590),
    (6.227, -0.230),
    (6.080, -2.211),
    (4.455, -6.216),
    (0.646, -6.228),
    (-3.858, -6.117),
    (-2.708, -1.582),
    (-1.630, 1.224),
    (-0.568, 3.971),
    (0.837, 6.845),
    (4.527, 6.677),
    (7.382, 6.609),
    (8.144, 5.045),
    (7.325, 3.207),
    (6.604, 1.604),
    (4.466, 1.743),
    (4.183, 2.622),
    (4.058, 3.261),
    (4.616, 3.739),
    (4.268, 4.007),
    (4.018, 4.182),
    (3.350, 4.249),
    (2.736, 3.494),
    (1.953, 2.357),
    (0.881, -0.580),
    (0.605, -1.886),
    (0.444, -2.657),
    (0.528, -3.687),
    (1.849, -3.125),
    (2.406, -2.906),
    (2.901, -2.001),
    (3.061, -1.293),
]

C_O1 = [
    (14.034, 6.607),
    (16.027, 6.151),
    (17.004, 4.472),
    (15.636, 0.449),
    (14.743, -2.555),
    (13.539, -6.378),
    (9.280, -6.179),
    (5.778, -5.983),
    (6.081, -2.847),
    (7.687, 1.564),
    (9.151, 6.000),
    (11.405, 7.083),
    (14.034, 6.607),
]

C_O2 = [
    (12.981, 4.036),
    (13.429, 3.909),
    (13.236, 2.704),
    (12.385, 0.323),
    (11.415, -2.196),
    (10.989, -3.489),
    (9.986, -3.277),
    (9.205, -3.041),
    (9.986, -0.819),
    (10.704, 0.997),
    (11.357, 2.713),
    (11.846, 4.366),
    (12.981, 4.036),
]


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 getpts(AR, i):
    return [
        tuple([c * SCL for c in AR[i * 3 + 0]]),
        tuple([c * SCL for c in AR[i * 3 + 1]]),
        tuple([c * SCL for c in AR[i * 3 + 2]]),
        tuple([c * SCL for c in AR[i * 3 + 3]]),
    ]


def draw(AR, col):
    n = len(AR) // 3
    begin_fill()
    color(col)
    for i in range(n):
        pts = getpts(AR, i)
        Bez(*pts)
    end_fill()

def circ(p,r,c):
    up()
    pt=tuple([c*SCL for c in p])
    rad = r*SCL
    goto(pt)
    seth(0)
    fd(rad)
    lt(90)
    begin_fill()
    color(c)
    circle(rad)
    end_fill()
    lt(90)
    fd(rad)



def Lego(sc):
    global SCL
    SCL = sc
    c1 = (18.2038,7.9373)
    c1r = 0.95

    c2 = (18.2081,7.93707)
    c2r= 0.75
    fh= 0.97765

    draw(YELLOW, "#FFED00")
    draw(BLACK, "black")
    draw(C_L, "white")
    draw(C_E, "white")
    draw(C_G, "white")
    draw(C_O1, "white")
    draw(C_O2, "black")
    circ(c1,c1r,'black')
    circ(c2,c2r,RED)
    color('black')
    lt(90)
    fd(round(fh*0.8*SCL))
    write("R",False,'center',('Arial',round(fh*SCL*0.95),'bold'))
setup(1000,500)
shape('turtle')
D=0.05
tracer(1)
bgcolor(RED)
Lego(24)
hideturtle()
goto(0,0)
done()


Friday, December 2, 2022

Python Turtle Graphics Drawing - Chrome Logo

 Python Turtle Graphics Drawing - Chrome Logo

Youtube


Source code:

import turtle as t
from math import sqrt
t.setup(640,640)
t.bgcolor('white')
t.tracer(1)
R = 310
# equilateral triangle
a = R/2
L = sqrt(R**2-a**2)
colors = ['#FBBF03', '#E4382A', '#269E49']
blu = '#1374E9'
for i in range(3):
    t.color(colors[i])
    t.up()
    t.lt(90)
    t.fd(a)
    t.down()
    t.begin_fill()
    t.rt(90)
    t.fd(L)
    t.rt(60)
    t.circle(-R,120)
    t.rt(120)
    t.fd(L)
    t.circle(a,120)
    t.end_fill()
    t.up()
    t.lt(90)
    t.fd(a)
    t.lt(90)
    t.lt(120)
r = R/2-R/10
t.fd(r)
t.lt(90)
t.down()
t.color(blu)
t.begin_fill()
t.circle(r)
t.end_fill()
t.hideturtle()
t.done()

Python Turtle Graphic Design #12

Python Turtle Graphic Design #12

 Youtube


Source code:

from turtle import *
import colorsys as cs
import time
setup(640,800)
bgcolor('black')
tracer(0,0)
pensize(2)
shape('turtle')
fnt=('Arial',45,'bold')
for n in range(3,21,1):
    clear()
    goto(0,100)
    down()
    color(cs.hsv_to_rgb((n-3)/20,1,1))
    N = n
    A = 360/N
    for i in range(N):
        for j in range(N):
            fd(100-n*3)
            rt(A)
        rt(A)
    up()
    seth(90)
    goto(0,-320)
    color('white')
    write(f'Sides: {n}',False,'center',fnt)
    goto(0,-250)
    update()
    time.sleep(1)
done()

Wednesday, November 30, 2022

Python Turtle Graphics Drawing - Pepsi Logo

 Python Turtle Graphics Drawing - Pepsi 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 = '#ED1B38'
B = '#005993'
W = '#FFFFFF'
K = '#000000'
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 Pepsi(sz):
    color(W)
    up()
    seth(0)
    fd(sz)
    lt(90)
    down()
    begin_fill()
    circle(sz)
    end_fill()
    up()
    color(R)
    lt(90)
    fd(sz)
    rt(126.7)
    fd(sz*0.95)
    lt(90)
    down()
    begin_fill()
    circle(sz*0.95,163.1)
    p = pos()
    pts = [p,p+(sz*0.74275,sz*0.2308),p+(sz*1.14545,sz*0.74895),p+(sz*1.33245,sz*1.32555)]
    Bez(*pts)
    end_fill()
    up()
    color(B)
    seth(233.3)
    fd(sz*0.95)
    rt(13.3)
    fd(sz*0.95)
    lt(90)
    down()
    begin_fill()
    circle(sz*0.95,173)
    p = pos()
    pts = [p,p+(sz*0.10705,sz*-0.35325),p+(sz*-0.0774,sz*-0.6761),p+(sz*-0.38725,sz*-0.8211)]
    Bez(*pts)
    p = pos()
    pts = [p,p+(sz*-0.23155,sz*-0.10975),p+(sz*-0.70985,sz*-0.1699),p+(sz*-1.138,sz*-0.30585)]
    Bez(*pts)
    end_fill()
    up()


setup(640,650)
bgcolor(K)
Pepsi(300)
hideturtle()
done()

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()

Sunday, November 27, 2022

Python Turtle Graphics Drawing - Spotify Logo

 Python Turtle Graphics Drawing - Spotify Logo

Youtube


Source code:

from turtle import *
M = [ # radius, pensize, x,y, start angle, angle
    [1.804, 0.0935, -0.15023, -1.42915, 64, 40],
    [1.3955, 0.0775, -0.16654, -1.34771, 62, 44],
    [1.2295, 0.0615, -0.1843, -1.48902, 61, 44],
]


def spotify(sz):
    up()
    seth(0)
    goto(0, 0)
    fd(sz)
    lt(90)
    begin_fill()
    circle(sz)
    end_fill()
    up()
    color('white')
    for m in M:
        goto(sz*m[2], sz*m[3])
        seth(m[4])
        fd(sz*m[0])
        lt(90)
        pensize(sz*m[1]*2)
        down()
        circle(sz*m[0], m[5])
        up()


setup(800, 800)
bgcolor('white')
color('#1AB26B')
spotify(100)


done()

Friday, November 25, 2022

Python Turtle Graphics Drawing - Mitubishi Logo

 Python Turtle Graphics Drawing - Mitubishi Logo

Youtube




Source code:

from turtle import *
from math import cos,radians
setup(640,640)

bgcolor('white')
color('red')

R = 300 #Logo Radius
r = R*cos(radians(30))*2/3
for i in range(3):
    begin_fill()
    fd(r)
    rt(60)
    fd(r)
    lt(60)
    bk(r)
    rt(60)
    bk(r)
    rt(60)
    end_fill()
hideturtle()
done()

Tuesday, November 22, 2022

Python Turtle Graphics Drawing - Adobe Logo

 Python Turtle Graphics Drawing - Adobe Logo

Youtube


Source code:

from turtle import *

def adobe(s):
    up()
    goto(-20*s,17.5*s)
    down()
    begin_fill()
    goto(-5.2*s,17.5*s)
    goto(-20*s,-17.5*s)
    goto(-20*s,17.5*s)
    end_fill()
    up()
    goto(0,4.7*s)
    down()
    begin_fill()
    goto(9.2*s,-17.5*s)
    goto(3.1*s,-17.5*s)
    goto(0.3*s,-10.5*s)
    goto(-6.5*s,-10.5*s)
    goto(0,4.7*s)
    end_fill()
    up()
    goto(5.1*s,17.5*s)
    down()
    begin_fill()
    goto(20*s,17.5*s)
    goto(20*s,-17.5*s)
    goto(5.1*s,17.5*s)
    end_fill()
    up()

setup(640,640)
color('#FA0F00')
adobe(12)
hideturtle()
done()

Circle Animation with ImGui

 Circle Animation with ImGui for C++ Youtube: Source code: void CircFunc () {     static int nc = 360 ;     double radius = 300 ;     ...