initial commit
This commit is contained in:
parent
d9d6a1dcf2
commit
8fe7a51009
82
rectangle.py
Normal file
82
rectangle.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
from random import randint
|
||||||
|
import turtle
|
||||||
|
|
||||||
|
class Point:
|
||||||
|
def __init__(self, x:int, y:int):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
||||||
|
|
||||||
|
def display_point(self):
|
||||||
|
return (self.x, self.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)
|
||||||
|
|
||||||
|
|
||||||
|
class Rectangle:
|
||||||
|
def __init__(self, point1:Point, point2:Point):
|
||||||
|
self.point1 = point1
|
||||||
|
self.point2 = point2
|
||||||
|
|
||||||
|
def point_in_rectangle(self, point):
|
||||||
|
if self.point1.x < point.x and \
|
||||||
|
point.y < self.point2.y:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def display_rectangle_coordinates(self):
|
||||||
|
print(f"Rectangle Coord: {self.point1.display_point()}, {self.point2.display_point()}")
|
||||||
|
|
||||||
|
def get_area(self):
|
||||||
|
return round(self.point2.x - self.point1.x) * (self.point2.y - self.point1.y)
|
||||||
|
|
||||||
|
class GuiRectangle(Rectangle):
|
||||||
|
|
||||||
|
def draw(self, canvas):
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
myturtle = turtle.Turtle()
|
||||||
|
|
||||||
|
gui_rectangle = GuiRectangle(Point(randint(0, 400), randint(0, 400)), \
|
||||||
|
Point(randint(10, 400), randint(10, 400))
|
||||||
|
)
|
||||||
|
gui_rectangle.display_rectangle_coordinates()
|
||||||
|
|
||||||
|
user_point = GuiPoint(float(input("Enter X: ")), \
|
||||||
|
float(input("Enter Y: "))
|
||||||
|
)
|
||||||
|
|
||||||
|
print(f"Your point is in the rectangle: {gui_rectangle.point_in_rectangle(user_point)}")
|
||||||
|
|
||||||
|
user_area = round(float(input("Guess the rectangle area: ")))
|
||||||
|
|
||||||
|
if gui_rectangle.get_area() > user_area:
|
||||||
|
print(f"Your guess of {user_area} was off by: {gui_rectangle.get_area() - user_area}")
|
||||||
|
else:
|
||||||
|
print(f"Your guess of {user_area} was off by: {user_area - gui_rectangle.get_area()}")
|
||||||
|
|
||||||
|
print(gui_rectangle.get_area())
|
||||||
|
|
||||||
|
gui_rectangle.draw(myturtle)
|
||||||
|
user_point.draw(myturtle)
|
||||||
|
turtle.done()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Reference in New Issue
Block a user