MCPcopy Create free account
hub / github.com/codemistic/Data-Structures-and-Algorithms / distance

Method distance

Python/Turtle.py:1727–1757  ·  view source on GitHub ↗

Return the distance from the turtle to (x,y) in turtle step units. Arguments: x -- a number or a pair/vector of numbers or a turtle instance y -- a number None None call: distance(x, y) # two coordinates --or:

(self, x, y=None)

Source from the content-addressed store, hash-verified

1725 self._goto(Vec2D(self._position[0], y))
1726
1727 def distance(self, x, y=None):
1728 """Return the distance from the turtle to (x,y) in turtle step units.
1729
1730 Arguments:
1731 x -- a number or a pair/vector of numbers or a turtle instance
1732 y -- a number None None
1733
1734 call: distance(x, y) # two coordinates
1735 --or: distance((x, y)) # a pair (tuple) of coordinates
1736 --or: distance(vec) # e.g. as returned by pos()
1737 --or: distance(mypen) # where mypen is another turtle
1738
1739 Example (for a Turtle instance named turtle):
1740 >>> turtle.pos()
1741 (0.00, 0.00)
1742 >>> turtle.distance(30,40)
1743 50.0
1744 >>> pen = Turtle()
1745 >>> pen.forward(77)
1746 >>> turtle.distance(pen)
1747 77.0
1748 """
1749 if y is not None:
1750 pos = Vec2D(x, y)
1751 if isinstance(x, Vec2D):
1752 pos = x
1753 elif isinstance(x, tuple):
1754 pos = Vec2D(*x)
1755 elif isinstance(x, TNavigator):
1756 pos = x._position
1757 return abs(pos - self._position)
1758
1759 def towards(self, x, y=None):
1760 """Return the angle of the line from the turtle's position to (x, y).

Callers 1

demo2Function · 0.80

Calls 1

Vec2DClass · 0.85

Tested by

no test coverage detected