Niall Mckenna
Niall Mckenna

Reputation: 3

calculating the area and perimeter of a rectangle drawn in graphics.py

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

Answers (1)

Chad S.
Chad S.

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

Related Questions