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
(self, x, y=None)
| 1827 | self._goto(Vec2D(self._position[0], y)) |
| 1828 | |
| 1829 | def distance(self, x, y=None): |
| 1830 | """Return the distance from the turtle to (x,y) in turtle step units. |
| 1831 | |
| 1832 | Arguments: |
| 1833 | x -- a number or a pair/vector of numbers or a turtle instance |
| 1834 | y -- a number None None |
| 1835 | |
| 1836 | call: distance(x, y) # two coordinates |
| 1837 | --or: distance((x, y)) # a pair (tuple) of coordinates |
| 1838 | --or: distance(vec) # e.g. as returned by pos() |
| 1839 | --or: distance(mypen) # where mypen is another turtle |
| 1840 | |
| 1841 | Example (for a Turtle instance named turtle): |
| 1842 | >>> turtle.pos() |
| 1843 | (0.00, 0.00) |
| 1844 | >>> turtle.distance(30,40) |
| 1845 | 50.0 |
| 1846 | >>> pen = Turtle() |
| 1847 | >>> pen.forward(77) |
| 1848 | >>> turtle.distance(pen) |
| 1849 | 77.0 |
| 1850 | """ |
| 1851 | if y is not None: |
| 1852 | pos = Vec2D(x, y) |
| 1853 | if isinstance(x, Vec2D): |
| 1854 | pos = x |
| 1855 | elif isinstance(x, tuple): |
| 1856 | pos = Vec2D(*x) |
| 1857 | elif isinstance(x, TNavigator): |
| 1858 | pos = x._position |
| 1859 | return abs(pos - self._position) |
| 1860 | |
| 1861 | def towards(self, x, y=None): |
| 1862 | """Return the angle of the line from the turtle's position to (x, y). |