Reputation: 3
I am trying to draw a rectangle with graphics
using getMouse
and then calculate its area and perimeter.
I have no idea how to calculate the area or perimeter. This is what I have so far.
from graphics import *
win = GraphWin("rectangle",200,200)
text = Text(Point(100,50), "please click on two points.")
text.draw(win)
p1 = win.getMouse()
p2 = win.getMouse()
rectangle = Rectangle(p1,p2)
rectangle.draw(win)
Upvotes: 0
Views: 1840
Reputation: 6633
Given two points as the diagonals, you can compute the lengths of the sides by taking the absolute value of the difference in x and the absolute value of the difference in y. That will give you the length and width of the rectangle.
e.g. length = abs(p1.x - p2.x)
From there you can compute the area and perimeter accordingly.
e.g. area = length * width
Upvotes: 1