-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (53 loc) · 2.06 KB
/
main.py
File metadata and controls
74 lines (53 loc) · 2.06 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
64
65
66
67
68
69
70
71
72
73
74
import turtle
from random import randint
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def falls_in_rectangle(self, rectangle):
if rectangle.point1.x < self.x < rectangle.point2.x and rectangle.point1.y < self.y < rectangle.point2.y:
return True
else:
return False
def distance_from_point(self, point):
return ((self.x - point.x) ** 2 + (self.y - point.y) ** 2) ** 0.5
class Rectangle:
def __init__(self, point1, point2):
self.point1 = point1
self.point2 = point2
def area(self):
return (self.point2.x - self.point1.x) * (self.point2.y - self.point1.y)
class GuiRectangle(Rectangle):
def draw(self, canvas):
# go to a certain coordinates
canvas.penup()
canvas.goto(self.point1.x, self.point1.y)
canvas.pendown()
canvas.forward(self.point2.x - self.point1.x)
canvas.left(90)
canvas.forward(self.point2.y - self.point1.y)
canvas.left(90)
canvas.forward(self.point2.x - self.point1.x)
canvas.left(90)
canvas.forward(self.point2.y - self.point1.y)
class GuiPoint(Point):
def draw(self, canvas, size=5, color='red'):
canvas.penup()
canvas.goto(self.x, self.y)
canvas.pendown()
canvas.dot(size, color)
# create rectangle object
rectangle = GuiRectangle(Point(randint(0, 100), randint(0, 100)), Point(randint(100, 200), randint(100, 200)))
# print rectangle coordinates
print("Rectangle Coordinate: ", rectangle.point1.x, ",", rectangle.point1.y,
"and", rectangle.point2.x, ",", rectangle.point2.y)
# get point and area from user
user_point = GuiPoint(float(input("Guess X: ")), float(input("Guess Y: ")))
user_area = float(input("Guess rectangle area: "))
# Print out the game result
print("Your point was inside rectangle: ", user_point.falls_in_rectangle(rectangle))
print("Your area was off by: ", rectangle.area() - user_area)
myturtle = turtle.Turtle()
rectangle.draw(canvas=myturtle)
user_point.draw(canvas=myturtle)
turtle.done()