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)
| 1894 | self._goto(Vec2D(self._position[0], y)) |
| 1895 | |
| 1896 | def distance(self, x, y=None): |
| 1897 | """Return the distance from the turtle to (x,y) in turtle step units. |
| 1898 | |
| 1899 | Arguments: |
| 1900 | x -- a number or a pair/vector of numbers or a turtle instance |
| 1901 | y -- a number None None |
| 1902 | |
| 1903 | call: distance(x, y) # two coordinates |
| 1904 | --or: distance((x, y)) # a pair (tuple) of coordinates |
| 1905 | --or: distance(vec) # e.g. as returned by pos() |
| 1906 | --or: distance(mypen) # where mypen is another turtle |
| 1907 | |
| 1908 | Example (for a Turtle instance named turtle): |
| 1909 | >>> turtle.pos() |
| 1910 | (0.00,0.00) |
| 1911 | >>> turtle.distance(30,40) |
| 1912 | 50.0 |
| 1913 | >>> pen = Turtle() |
| 1914 | >>> pen.forward(77) |
| 1915 | >>> turtle.distance(pen) |
| 1916 | 77.0 |
| 1917 | """ |
| 1918 | if y is not None: |
| 1919 | pos = Vec2D(x, y) |
| 1920 | if isinstance(x, Vec2D): |
| 1921 | pos = x |
| 1922 | elif isinstance(x, tuple): |
| 1923 | pos = Vec2D(*x) |
| 1924 | elif isinstance(x, TNavigator): |
| 1925 | pos = x._position |
| 1926 | return abs(pos - self._position) |
| 1927 | |
| 1928 | def towards(self, x, y=None): |
| 1929 | """Return the angle of the line from the turtle's position to (x, y). |