def tSquare(size, level): """ The T-Square fractal. http://en.wikipedia.org/wiki/T-Square_%28fractal%29 """ if level < 1: drawSquare(size, True) else: drawSquare(size, True) bk(size / 4.0) left(90) fd(size / 4.0) right(90) tSquare(size / 2.0, level - 1) up() fd(size) down() tSquare(size / 2.0, level - 1) right(90) fd(size) left(90) tSquare(size / 2.0, level - 1) bk(size) tSquare(size / 2.0, level - 1) left(90) up() fd(size * 3 / 4.0) down() right(90) fd(size / 4.0)
def kwiatek(): p = 0 while (p <= 7): platek() turtle.bk(50) turtle.lt(360/8) p = p + 1
def repeat(turtle, size, count): for i in range(count): random_color(turtle) drawSquareFromCenter(turtle, size) turtle.fd(size) distance = count * size turtle.bk(distance)
def ustNaStart(): turtle.pu() turtle.bk(250) turtle.lt(90) turtle.bk(250) turtle.rt(90) turtle.pd()
def draw_tree(l, level): """ 画出当前 level 的树枝 :param l: 当前层级树枝的长度 :param level: 当前层级 """ global r, g, b w = t.width() # 保存当前画笔宽度 t.width(w * 3.0 / 4.0) # 把画笔的宽度变小一点 r, g, b = r + 1, g + 2, b + 3 # 修改颜色 t.pencolor(r % 200, g % 200, b % 200) l = 3.0 / 4.0 * l # 修改当前树枝长度 t.lt(s) # 左转 s 度 t.fd(l) # 画出当前的第一个树枝,向左的那个树枝 if level < lv: # 如果当前的层级还没有达到最大的层级 draw_tree(l, level + 1) # 那就继续画下一个层级 t.bk(l) # 后退回到原来的位置 t.rt(2 * s) # 右转 2 倍的 s 度 t.fd(l) # 画出当前的第二个树枝,向右的那个树枝 if level < lv: # 如果当前的层级还没有达到最大的层级 draw_tree(l, level + 1) # 那就继续画下一个层级 # 画完了子树枝,恢复现场 t.bk(l) t.lt(s) t.width(w)
def tGraph(word='F', theta=0, alpha=0, step=10, x0=0, y0=0): #Run, turtle, RUN!!! #tr.clear() tr.radians() tr.speed(0) tr.pu() tr.setx(x0) tr.sety(y0) tr.seth(alpha) tr.pd() st = [] for c in word: if c == 'F': tr.fd(step) if c == 'b': tr.pu() tr.bk(step) tr.pd() if c == '[': st.append({'x': tr.xcor(), 'y': tr.ycor(), 'ang': tr.heading()}) if c == ']': pos = st.pop() tr.pu() tr.setx(pos['x']) tr.sety(pos['y']) tr.seth(pos['ang']) tr.pd() if c == '+': tr.lt(theta) if c == '-': tr.rt(theta)
def drawStar(N, R): turtle.reset() a = 360/N for i in range(N): turtle.fd(R) turtle.bk(R) turtle.left(a)
def draw_r(): """ Draw the letter R :pre: (relative) at the start of the letter, heading (east), up :post: (relative) 50 units from end of letter, heading (east), up :return: None """ t.down() t.left(90) t.fd(LETTER_LENGTH) t.bk(LETTER_LENGTH / 2) t.right(90) t.fd(LETTER_LENGTH / 4) t.circle(LETTER_LENGTH / 4, 180) t.fd(LETTER_LENGTH / 4) t.left(90) t.fd(LETTER_LENGTH / 2) t.left(45) t.fd(HYPOTENUSE) t.bk(HYPOTENUSE) t.right(45) t.up() t.fd(LETTER_LENGTH / 2) t.left(90) t.fd(LETTER_LENGTH / 2) add_space()
def korDuza(popr,ile): turtle.pu() turtle.bk(popr * ile) turtle.lt(90) turtle.bk(popr) turtle.rt(90) turtle.pd()
def locate_eye_2(): bob.pu() bob.bk(110) bob.rt(90) bob.fd(10) bob.lt(90) bob.pd()
def ustDoPOka(a): turtle.rt(60) turtle.bk(a * 1.5) turtle.lt(60) turtle.pu() turtle.rt(180) turtle.fd(4 * a) turtle.lt(60) turtle.fd(4 * a) turtle.lt(60) turtle.fd(4 * a) turtle.rt(60) turtle.fd(4 * a) turtle.lt(60) turtle.lt(360 / 12) turtle.fd(a * 8) turtle.lt(360 / 12) turtle.fd(a * 8) turtle.lt(360 / 12) turtle.fd(a * 8) turtle.lt(360 / 12) turtle.lt(60) turtle.fd(4 * a) turtle.lt(60) turtle.fd(a * 4) turtle.pd()
def ustNaStartKwiatki(rozmKanwy, segment): turtle.pu() turtle.bk(225) turtle.lt(90) turtle.bk(225) turtle.rt(90) turtle.pd()
def ustNaStart(a): turtle.pu() turtle.bk(a) turtle.lt(90) turtle.fd(a / 2) turtle.rt(90) turtle.pd()
def next(tamanho, colunas): t.pu() t.bk(tamanho * colunas) t.rt(90) t.fd(tamanho) t.lt(90) t.pd()
def draw_l(word): turtle.up() turtle.clear() turtle.setposition(0, 0) turtle.setheading(0) turtle.bk(INITIAL_POS[0]) turtle.down() turtle.st() stack = [] for char in word: if char == '0': turtle.fd(SIZE[0]) if char == '1': turtle.fd(SIZE[0]) if char == '[': stack.append((turtle.position(), turtle.heading())) turtle.lt(45) if char == ']': position, heading = stack.pop() turtle.up() turtle.setposition(position) turtle.setheading(heading) turtle.rt(45) turtle.down() turtle.ht()
def main(): """ 主函数 """ turtle.speed('slow') turtle.pensize(5) turtle.pu() turtle.fd(30) turtle.pd() turtle.fd(30) turtle.pu() turtle.bk(90) turtle.pd() turtle.bk(30) turtle.pu() turtle.setpos(-20, -60) turtle.pensize(7) turtle.color('red') turtle.pd() turtle.fd(40) turtle.color('black') turtle.pensize(3) turtle.pu() turtle.setpos(0, -130) turtle.pd() turtle.circle(100) turtle.pu() turtle.exitonclick()
def ustNaStart(dlBokKw, ile): turtle.pu() turtle.bk(dlBokKw * ile / 2 +5) turtle.lt(90) turtle.fd(dlBokKw * (ile - 2) / 2 - 5) turtle.rt(90) turtle.pd()
def koch_curve(d, lvl): if lvl == 0: t.fd(d) t.pu() t.bk(d) t.pd() return koch_curve(d / 3, lvl - 1) t.pu() t.fd(d / 3) t.lt(60) t.pd() koch_curve(d / 3, lvl - 1) t.pu() t.fd(d / 3) t.rt(120) t.pd() koch_curve(d / 3, lvl - 1) t.pu() t.fd(d / 3) t.lt(60) t.pd() koch_curve(d / 3, lvl - 1) t.pu() t.bk(2 * d / 3) t.pd()
def draw_circle(x,y,angle): """ draw_circle --------------------- x, y 小圆圆心坐标 angle 海龟的朝向 """ global first # 开始绘制外部的小圆 turtle.clear() turtle.up() turtle.seth(0) turtle.goto(x, y-r_small) turtle.down() turtle.color('black') turtle.circle(r_small, steps=200) # 圆心处打点 turtle.up() turtle.goto(x, y) turtle.dot(10, 'blue') turtle.down() # 移动到外圆上的定点 turtle.seth(angle) turtle.color('purple') turtle.bk(d) turtle.dot(10,'red') # 连接摆线 tt.goto(turtle.xcor(),turtle.ycor()) if first: tt.down() first = False
def drawdate(date): t.pencolor('red') for i in date: if i == '-': t.right(90) t.fd(50) t.write('年', font=('MS Gothic', 18, 'normal')) t.pencolor('green') t.bk(50) t.left(90) t.fd(40) elif i == '=': t.right(90) t.fd(50) t.write('月', font=('MS Gothic', 18, 'normal')) t.pencolor('blue') t.bk(50) t.left(90) t.fd(40) elif i == '+': t.right(90) t.fd(50) t.write('日', font=('MS Gothic', 18, 'normal')) else: drawdigit(eval(i))
def rysuj_cyfre(n): kolor = random.choice(kolory) for r in daj_cyfre(n): print() for i in range(len(r)): print(r[i],end='') if r[i] == '#': kwadrat(50, kolor) fd(50) else: pu() fd(50) pd() pu() bk(50 * len(r)) rt(90) fd(50) lt(90) pd() pu() fd(300) lt(90) fd(250) rt(90) pd()
def drawSootSprite(N, R): # reset direction turtle.reset() # draw star drawStar(N, R) # draw body turtle.dot(0.8 * 2 * R) # draw right eyeball turtle.fd(0.2 * R) turtle.dot(0.3 * R, 'white') # draw right pupil turtle.pu() turtle.bk(0.1 * R) turtle.pd() turtle.dot(0.05 * R) turtle.pu() # centre turtle.setpos(0, 0) # draw left eyeball turtle.bk(0.2 * R) turtle.pd() turtle.dot(0.3 * R, 'white') # draw left pupil turtle.pu() turtle.fd(0.1 * R) turtle.pd() turtle.dot(0.05 * R) turtle.hideturtle()
def drawStar(N, R): turtle.reset() a = 360 / N for i in range(N): turtle.fd(R) turtle.bk(R) turtle.left(a)
def box(size): # Setup t.pd() x = 90 t.fd(size * 60) # Draw the box for i in range(2): t.lt(x) t.fd(size * 120) t.lt(x) t.fd(size * 120) t.lt(x) # Draw each horizontal 40px t.fd(size * 40) t.lt(x) t.fd(size * 120) t.rt(x) t.fd(size * 40) t.rt(x) t.fd(size * 120) t.lt(x) t.bk(size * 80) t.rt(90) t.bk(size * 60) # Reset the pen t.pu()
def drawRaindropWithRipples(radius, x, y, numRipples=kNumRipples()): """If the ripples would go out of bounds, then re-roll""" if not (radius * (numRipples + 1) + abs(x) <= kPondSize() and radius * (numRipples + 1) + abs(y) <= kPondSize()): """Reroll the raindrop""" return drawRaindropWithRipples(randint(kMinRadius(), kMaxRadius()), randint(-kPondSize(), kPondSize()), randint(-kPondSize(), kPondSize()), numRipples) else: t.fd(x) t.lt(90) t.fd(y) t.rt(90) t.fillcolor((random(), random(), random())) t.fd(radius) t.lt(90) t.down() t.begin_fill() t.circle(radius) t.end_fill() t.up() t.rt(90) t.bk(radius) return 2 * pi * radius + drawRipple(numRipples, radius, 2 * radius) + goHome(x, y)
def drawA(size): # Draws an uppercase A a = 26.565 x = 134.164 # Setup t.pu() t.bk(size * 60) # Draw first leg t.pd() t.lt(90 - a) t.fd(size * x) # Draw second leg t.rt(180 - 2 * a) t.fd(size * x) # Draw center stem t.rt(180) t.fd(size * (x / 3)) t.lt(90 - a) t.fd(size * 80) # Reset from A t.pu() t.lt(180) t.fd(size * 40) t.rt(90) t.fd(size * 40) t.lt(90)
def zigzag(length, depth): """ Pre-Conditions: Default Starting location Post-Conditions: Back in the same place Description: Makes a zig-sag style fractal with red and green coloring, leg length and depth determined through user prompt """ depth = int(depth + .5) if depth <= 0: pass else: t.lt(90) t.fd(length / 2) t.rt(90) t.fd(length) zigzag(length / 2, depth - 1) t.bk(length) t.rt(90) t.fd(length) t.lt(90) t.bk(length) zigzag(length / 2, depth - 1) t.fd(length) t.lt(90) t.fd(length / 2) t.rt(90)
def draw_line(length, angle, state): turtle.pensize(1) turtle.pendown() turtle.setheading(angle) turtle.fd(length) turtle.bk(length) if state else turtle.penup() turtle.penup()
def draw_tree(l, level): global r, g, b # save the current pen width w = t.width() # narrow the pen width t.width(w * 3.0 / 4.0) # set color: r = r + 1 g = g + 2 b = b + 3 t.pencolor(r % 200, g % 200, b % 200) l = 3.0 / 4.0 * l t.lt(s) t.fd(l) if level < lv: draw_tree(l, level + 1) t.bk(l) t.rt(2 * s) t.fd(l) if level < lv: draw_tree(l, level + 1) t.bk(l) t.lt(s) # restore the previous pen width t.width(w)
def binary_tree(depth, length, origin = (0,0) ): turtle.setposition(origin) if length == 0: return True turtle.right(30) turtle.pendown() turtle.forward(depth) right = turtle.pos() turtle.penup() turtle.bk(depth) turtle.right(120) turtle.pendown() turtle.forward(depth) turtle.penup() left = turtle.pos() turtle.bk(depth) turtle.left(150) binary_tree(depth/2, length-1, left) binary_tree(depth/2, length-1, right) return True
def turtleXYZ(): turtle.goto(0, 0) turtle.pd() turtle.goto(0, 300) turtle.goto(6, 290) turtle.pu() turtle.goto(-6, 290) turtle.pd() turtle.goto(0, 300) turtle.goto(0, 0) turtle.goto(300, 0) turtle.goto(290, 6) turtle.pu() turtle.goto(290, -6) turtle.pd() turtle.goto(300, 0) turtle.goto(0, 0) turtle.rt(135) turtle.fd(212.1) turtle.lt(135) turtle.fd(5) turtle.bk(10) turtle.lt(90) turtle.fd(10) turtle.pu() turtle.goto(0, 0)
def drawBlockDiagram(lst, componentName): # deternmine number of inputs, outputs and inouts inp = 0 outp = 0 for sig in lst: if sig[1].upper() == "IN": inp += 1 else: outp += 1 largestType = max(inp, outp) height = 50 * (largestType + 1) drawSquare(COMPONENT_WIDTH, height, componentName) turtle.rt(90) for sig in lst: turtle.pu() if sig[1].upper() == "IN": turtle.fd(50) turtle.lt(90) turtle.bk(IO_LINE_LENGTH) drawIO(sig[0], sig[2]) turtle.fd(IO_LINE_LENGTH) turtle.rt(90) alignTurtletoKnown(COMPONENT_WIDTH, height) turtle.fd(75) turtle.rt(90) for sig in lst: if sig[1] != "in": turtle.fd(50) turtle.lt(90) drawIO(sig[0], sig[2]) turtle.rt(90) turtle.pu() turtle.home() turtle.setup(25 + COMPONENT_WIDTH + (IO_LINE_LENGTH * 2), height + 50)
def body(): ''' draws body ''' turtle.setheading(270) turtle.fd(50) turtle.bk(50)
def drawpoint(): turtle.pensize(5) turtle.pendown() turtle.fd(1) turtle.penup() turtle.bk(1) turtle.pensize(1)
def draw_tree(l, level): global r, g, b # save the current pen width w = turtle.width() # narrow the pen width 每次递归都是之前的宽度四分之三 turtle.width(w * 3.0 / 4.0) # set color: r = r + 1 g = g + 2 b = b + 3 turtle.pencolor(r % 200, g % 200, b % 200) #设置画笔颜色 l = 3.0 / 4.0 * l #前进长度 turtle.lt(s) #左转45度 left turtle.fd(l) #前进l个长度 forward if level < lv: #左边树叉递归 draw_tree(l, level + 1) turtle.bk(l) #回退l长度 backward turtle.rt(2 * s) #右转90度 right turtle.fd(l) #前进l个长度 forward if level < lv: #右边树叉递归 draw_tree(l, level + 1) turtle.bk(l) #backward 后退l个 turtle.lt(s) #左转45度 left # restore the previous pen width turtle.width(w) #完了之后将画笔宽度返回至原来的宽度
def jeden(): turtle.fd(50) turtle.fillcolor(randint(0,255),randint(0,255),randint(0,255)) turtle.begin_fill() kwadrat(50) turtle.end_fill() turtle.bk(50)
def ustDoNosa(a): turtle.pu() turtle.rt(60) turtle.bk(1.5 * a) turtle.fd(4 * a) turtle.lt(360 / 6) turtle.fd(4 * a) turtle.lt(360 / 6) turtle.fd(4 * a) turtle.lt(360 / 6) turtle.rt(180) turtle.lt(60) turtle.fd(a * 4) turtle.lt(120) turtle.fd(a * 8) turtle.lt(360 / 12) turtle.fd(a * 8) turtle.lt(360 / 12) turtle.fd(a * 8) turtle.lt(360 / 12) turtle.fd(a * 8) turtle.lt(360 / 12) turtle.lt(60) turtle.fd(8 * a) turtle.lt(30) turtle.lt(360 / 6) turtle.fd(2 * a) turtle.rt(360 / 6) turtle.fd(2 * a) turtle.rt(360/ 6) turtle.fd(a * 2) turtle.lt(360 / 6) turtle.pd()
def drawSootSprite(N, R): # reset direction turtle.reset() # draw star drawStar(N, R) # draw body turtle.dot(0.8*2*R) # draw right eyeball turtle.fd(0.2*R) turtle.dot(0.3*R, 'white') # draw right pupil turtle.pu() turtle.bk(0.1*R) turtle.pd() turtle.dot(0.05*R) turtle.pu() # centre turtle.setpos(0, 0) # draw left eyeball turtle.bk(0.2*R) turtle.pd() turtle.dot(0.3*R, 'white') # draw left pupil turtle.pu() turtle.fd(0.1*R) turtle.pd() turtle.dot(0.05*R) turtle.hideturtle()
def draw_people(x, y): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.pensize(2) turtle.color('black') turtle.setheading(0) turtle.circle(35, 360) turtle.penup() turtle.pensize(3) turtle.setheading(90) turtle.fd(45) turtle.setheading(180) turtle.fd(20) turtle.setheading(0) turtle.fd(35) turtle.pendown() turtle.circle(4, 360) turtle.penup() turtle.goto(x, y) turtle.pensize(2) turtle.setheading(0) turtle.fd(20) turtle.setheading(90) turtle.fd(20) turtle.setheading(-90) turtle.pendown() turtle.circle(5, 180) turtle.penup() turtle.goto(x, y) turtle.setheading(-90) turtle.pendown() turtle.fd(20) turtle.setheading(0) turtle.fd(35) turtle.setheading(60) turtle.fd(10) turtle.penup() turtle.goto(x, y) turtle.setheading(-90) turtle.pendown() turtle.fd(40) turtle.setheading(0) turtle.fd(35) turtle.setheading(-60) turtle.fd(10) turtle.penup() turtle.goto(x, y) turtle.setheading(-90) turtle.pendown() turtle.fd(60) turtle.setheading(-135) turtle.fd(60) turtle.bk(60) turtle.setheading(-45) turtle.fd(30) turtle.setheading(-135) turtle.fd(35) turtle.penup()
def tree(n): lt(90) pu() bk(300) pd() fd(100) branch(n) Lbranch(n)
def draw_turtle(): turtle.title('거북이 graphic') turtle.color('black', 'red') turtle.shape('turtle') turtle.fd(80) turtle.bk(100) turtle.exitonclick()
def recur(n): t.fd(n) t.rt(90) if n > 10: recur(n - 30) t.lt(180) if n > 10: recur(n - 30) t.rt(90) t.bk(n)
def createArm(amount, iteration, twig_amount): if (iteration < 1): return turtle.left(360 / amount) turtle.fd(twig_size) createTwig(twig_amount) turtle.bk(twig_size * (twig_amount + 1)) createArm(amount, iteration - 1, twig_amount)
def korDuza(a): dlBokKw = a / 5 turtle.pu() turtle.bk(a) turtle.rt(90) turtle.fd(dlBokKw) turtle.lt(90) turtle.pd()
def korDuzaLaczniki(segment, ileLacznikow): liczbaSegmentow = ileLacznikow * 3 turtle.pu() turtle.bk(liczbaSegmentow * segment) turtle.lt(90) turtle.pu() turtle.fd(segment * 3) turtle.rt(90) turtle.pd()
def korDuzaKwiatki(segment, ileKwiatkow): liczbaSegmentow = 5 + 6 * (ileKwiatkow - 1) turtle.pu() turtle.bk(liczbaSegmentow * segment + segment) turtle.lt(90) turtle.pu() turtle.fd(segment * 6) turtle.rt(90) turtle.pd()
def drawBranch(size, angle): # Recursive function to draw a part of the tree. turtle.fd(size) if size>5: turtle.rt(angle/2) drawBranch(size*0.75, angle) turtle.lt(angle) drawBranch(size*0.75, angle) turtle.rt(angle/2) turtle.bk(size)
def ustNaStart(a): bok = a * 8 ileBokow = 12 turtle.pu() turtle.bk(8*a + 4*a*math.sqrt(3)) turtle.lt(90) turtle.fd(bok / 2) turtle.rt(360 / ileBokow) turtle.pd()
def ustDoNastObr(bokKw=10): turtle.pu() turtle.bk(bokKw*2) turtle.lt(90) turtle.bk(bokKw*3) turtle.lt(90) turtle.fd(bokKw) turtle.rt(180) turtle.pd()
def elemKwiatka(bokStrzalki, beta): numPow = 1 ustNaStartElem(bokStrzalki, beta) while numPow <= 6: strzalka(bokStrzalki) ustDoNastStrzal(bokStrzalki, beta) numPow = numPow + 1 turtle.pu() turtle.lt(beta) turtle.bk(bokStrzalki/4) turtle.pd()
def ustNaStartLaczniki(rozmKanwy, segment): turtle.pu() turtle.goto(0, 0) turtle.bk(225) turtle.lt(90) turtle.bk(225) turtle.rt(90) turtle.fd(segment) turtle.lt(90) turtle.fd(segment * 2) turtle.rt(90) turtle.pd()
def kwadrOdSrodka(dlBokKw): polBokuKwadratu = dlBokKw / 2 turtle.pu() turtle.bk(polBokuKwadratu) turtle.rt(90) turtle.fd(polBokuKwadratu) turtle.pd() numPow = 1 while(numPow <= 4): turtle.lt(90) turtle.fd(dlBokKw) numPow = numPow + 1
def kwiatek(bok = 60): beta = 360 / 5 numPow = 1 turtle.lt(180 + 90) srodek(bok) turtle.lt(beta / 2) while(numPow <= 5): turtle.rt(beta) turtle.fd(60) rogi(bok) turtle.bk(60) numPow = numPow + 1
def wasL(a): numPow = 1 while(numPow <= 4): turtle.fd(2 * a) turtle.rt(360 / 8) numPow = numPow + 1 turtle.lt(360 / 8) numPow = 1 while(numPow <= 4): turtle.bk(2 * a) turtle.lt(360 / 8) numPow = numPow + 1 turtle.rt(360 / 8)
def accidentalButCool(size, angle, level): newAngle = angle / 2.0 obtuse = 180 - angle if level < 1: for i in range(3): fd(size) left(obtuse) fd(size / (2 * math.cos(math.radians(newAngle)))) bk(size / (2 * math.cos(math.radians(newAngle)))) right(newAngle) else: for i in range(3): accidentalButCool(size, newAngle, level - 1)
def snowflake(radius, iter, anti): n = 3 # number of sides side_length = radius * math.sqrt(3) angle = (360 / 2) - (60 / 2) t.fd(radius) # forward t.rt(angle) # right turn for i in range(n): f(side_length, iter, anti) t.rt(360 / n) t.rt(-angle) t.bk(radius) # backward
def pythagoras(size, level): """ The Pythagoras Tree fractal. http://en.wikipedia.org/wiki/Pythagoras_tree_%28fractal%29 """ if level < 1: drawSquare(size, True) else: newSize = size / 2.0 * math.sqrt(2) drawSquare(size, True) left(135) fd(newSize) right(90) pythagoras(newSize, level - 1) left(90) bk(newSize) right(135) fd(size) left(135) fd(newSize) right(90) fd(newSize) right(90) pythagoras(newSize, level - 1) left(90) bk(newSize) left(90) bk(newSize) right(135) bk(size)
def drawTreeFractal(height,AngularDisp, Length): turtle.bgcolor("#191970") a = (0,-200) Base(a) turtle.setheading(90) turtle.color("green") turtle.fd(1000) turtle.bk(2000) Base(a) star1() star2() sign() makeTree(height, Length,a,0,AngularDisp*2) turtle.setheading(180) makeTree(3, Length/2.00,a,0,AngularDisp*2) turtle.done()