Python Turtle Graphics Drawing - Lego® Logo
Draw Lego® Logo using bézier curve in Python Turtle Graphics.
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()
No comments:
Post a Comment