본문 바로가기
프로그램

[파이썬] 문제 : turtle 이름쓰기2

by 오디세이99 2024. 1. 11.
728x90
반응형
import turtle
import math

t = turtle.Turtle()
t.hideturtle()  
t.width(3)

def get_length(base, height):             # 빗변 길이. hypotenuse
    return math.sqrt(base**2 + height**2)

def get_angle(base, height):              # 각도 계산
    angle = math.atan(height / base)
    return angle * 180 / math.pi

def draw_ㅇ(x, y, width, height):
    t.penup()
    # t.goto(x+(int(width/2)), y-(int(height/2)))
    t.goto(x, y-(int(height/2)))
    t.pendown()
    t.circle(width / 2)

def draw_ㅣ(x, y, width, height):     # 한글 ㅣ (이)
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.setheading(0)
    
    t.right(90)
    t.forward(height)

def draw_ㅅ(x, y, width, height):
    t.penup()
    y1 = y-(height)      # ㅅ에서 좌측 하단으로 이동하기 위해 y계산(세로축)
    t.goto(x, y1)
    t.pendown()
    t.setheading(0)
    
    angle = get_angle(int(round(width/2,1)), height)   # 직각삼삭형에서 높이와 밑변 길이를 알때 각도 계산
    angle2 = 180 - (90 + angle)                        # ㅅ의 좌측 하단에서 중앙상단으로 이동 후 내려가는 각도 계산
    length = get_length(int(round(width/2,1)), height) # 긱각삼걱형에서 높이와 밑변 길이를 알때 빗변 길이 계산 
    
    t.left(angle)           # ㅅ의 / 의 각도 바꿈
    t.forward(length)       # ㅅ의 / 의 길이는 삼각형의 빗변 길이로 사용
    t.right(180)            # ㅅ의 / 이후 ∖ 각도 계산을 위해서 먼저 / 방향으로 회전
    t.left(angle2 * 2)      # 직각삼각형(ㅅ을 직각삼각형 2개로 봄)에서 왼쪽 직각삼각형에서 90, angle 그리고 하나는 180-(90+angle). 이 각도를 2번 하면 계산됨
    t.forward(length)       # ㅅ에서 ∖ 그림

def draw_ㅜ(x, y, width, height):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.setheading(0)
    
    # t.right(90)
    t.forward(int(width/2))
    t.right(90)
    t.forward(height/2)
    t.backward(height/2)
    t.left(90)
    t.forward(int(width / 2))
    
def draw_ㅂ(x, y, width, height):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.setheading(0)
    
    t.right(90)
    t.forward(height)
    t.backward(int(height/2))
    t.left(90)
    t.forward(width)
    t.left(90)
    t.forward(int(height / 2))
    t.backward(height)
    t.left(90)
    t.forward(width)
    
def draw_ㄴ(x, y, width, height):
    t.penup()
    t.goto(x, y)
    t.pendown()
    t.setheading(0)
    
    t.right(90)
    t.forward(height)
    t.left(90)
    t.forward(width)


# 글자 하나가 100x100 일때 이름이 3글자면 중간이 가운데 글자의 중간이 되어야 함;
# 중간 글자 중간에서 왼쪽이 x=-50, 간격 10, 첫글자는 -160이 됨
# 예시로 (50, 50) 위치에 폭 100, 높이 150 크기의 'ㄱ'을 그립니다.
# draw_ㄱ(-100, 0, 60, 60)

draw_ㅇ(-160, -50, 80, 80)
draw_ㅣ(-90, 0, 10, 100)

draw_ㅅ(-20, 0, 50, 50)
draw_ㅜ(-50, -60, 100, 80)

draw_ㅂ(80, 0, 50, 60)
draw_ㅣ(160, 0, 10, 60)
draw_ㄴ(80, -70, 80, 30)

    
turtle.done()

try:
    turtle.bye()
except:
    print("bye")

728x90
반응형

댓글