简体   繁体   中英

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)

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.

eg length = abs(p1.x - p2.x)

From there you can compute the area and perimeter accordingly.

eg area = length * width

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM