简体   繁体   English

计算在graphics.py中绘制的矩形的面积和周长

[英]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. 我正在尝试使用getMouse绘制带有graphics的矩形,然后计算其面积和周长。

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. 给定两个点作为对角线,可以通过获取x差的绝对值和y差的绝对值来计算边的长度。 That will give you the length and width of the rectangle. 这将为您提供矩形的长度和宽度。

eg length = abs(p1.x - p2.x) 例如, length = abs(p1.x - p2.x)

From there you can compute the area and perimeter accordingly. 从那里您可以相应地计算面积和周长。

eg area = length * width 例如, area = length * width

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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