forked from RonaldsonBellande/RobotLearningFinalProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDraw_and_Get_Coord.py
More file actions
63 lines (47 loc) · 1.57 KB
/
Draw_and_Get_Coord.py
File metadata and controls
63 lines (47 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
####################################################
# Copyright (C) 2019 Sam Pickell
# Last Updated December 9, 2019
# Draw_and_Get_Coord.py
####################################################
# The following Turtle Graphics implementation was obtained from this tutorial:
# https://techwithtim.net/tutorials/python-module-walk-throughs/turtle-module/drawing-with-mouse/
# It has been modified and added to so that it reports the mouse's coordinates when drawing
# Using Turtle Graphics
import turtle
from turtle import *
# Global variable that sets the number of datapoints to collect
LOOP_THRESHOLD = 20
# Create screen and turtle variables
out_screen = Screen()
my_turtle = Turtle("turtle")
my_turtle.speed(-1)
# Create two lists to store X and Y coordinates
x_coords = []
y_coords = []
# Draw function
def turtle_draw(x, y):
my_turtle.ondrag(None)
my_turtle.setheading(my_turtle.towards(x, y))
my_turtle.goto(x, y)
my_turtle.ondrag(turtle_draw)
# Ensure 0 is always positive
if(x == -0.0):
x = 0.0
# Append the x coordinate to the end of the list
x_coords.append(x)
# Ensure 0 is always positive
if(y == -0.0):
y = 0.0
# Append the y coordinate to the end of the list
y_coords.append(y)
# End drawing session after a certain threshold is reached
if(len(x_coords) >= LOOP_THRESHOLD):
turtle.bye()
# The main function
def main():
turtle.listen()
my_turtle.ondrag(turtle_draw)
out_screen.mainloop()
main()
print('X coordinates: ', len(x_coords))
print('Y coordinates: ', len(y_coords))