-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX4.37.py
46 lines (39 loc) · 1.09 KB
/
EX4.37.py
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
# 4.37 (Turtle: point in a rectangle?) Write a program that prompts the user to enter a
# point (x, y) and checks whether the point is within the rectangle centered at (0,
# 0) with width 100 and height 50. Display the point, the rectangle, and a message
# indicating whether the point is inside the rectangle in the window, as shown in
# Figure 4.14.
import math
import turtle
WIDTH = 100
HEIGHT = 50
x, y = eval(input("Enter Point's x,y : "))
# goto to appropriate drawing position
turtle.penup()
turtle.goto(0 - WIDTH / 2, 0 - HEIGHT / 2)
turtle.pendown()
# Draw rectangle
turtle.forward(WIDTH)
turtle.left(90)
turtle.forward(HEIGHT)
turtle.left(90)
turtle.forward(WIDTH)
turtle.left(90)
turtle.forward(HEIGHT)
# Draw dot
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.begin_fill()
turtle.color("red")
turtle.dot(6)
turtle.end_fill()
turtle.penup()
turtle.goto(-15, -60)
turtle.pendown()
if (WIDTH / 2 > x > -WIDTH / 2) and (HEIGHT / 2 > y > -HEIGHT / 2):
turtle.write("The Point is inside rectangle")
else:
turtle.write("The Point is outside rectangle")
turtle.hideturtle()
turtle.done()