파이썬/파이썬(python) 중급

[21-2 파이썬] 터틀 distance()

Olivia-BlackCherry 2022. 8. 29. 22:04
distance()

distance 메서드는 뒤에 오는 argument에 따라 해석이 달라진다.

총 2가지 케이스가 있다.

 

 

첫 번째!

★.distance(♡) : 로부터 까지의 거리

ninja = Turtle(shape = "turtle")

circle = Turtle(shape = "circle")
circle.forward(100)

print(circle.distance(ninja))

 

 

 

 

 

.distance(x, y)  : 로부터 (x, y)까지의 거리

from turtle import Turtle, Screen

screen = Screen()
screen.setup(height=600, width=800)

#기준점
center= Turtle(shape="circle")
center.shapesize(0.2, 0.2)
center.color("red")
center.penup()
center.goto(100, 30)

#터틀
ninja = Turtle(shape = "turtle")
ninja.forward(-100)

#원
circle = Turtle(shape = "circle")
circle.forward(100)

print(circle.distance(100, 30))
print(ninja.distance(100, 30))
screen.exitonclick()

아래의 보이는 빨간 점은 기준점이며, 좌표가 (100, 30)이다. 

빨간 점에서부터 거리를 측정한 결과이다.