简体   繁体   English

如何找到定义了两个点的矩形的面积和周长?

[英]How to find the area and perimter of a rectangle that has two points defined?

I have a Point class and Rectangle class set up, here is the code for that:我设置了一个 Point 类和 Rectangle 类,这是它的代码:

import math

class Point:
    """A point in two-dimensional space."""

    def __init__(self, x: float = 0.0, y: float = 0.0)->None:               
        self.x = x
        self.y = y    


    def moveIt(self, dx: float, dy: float)-> None:
        self.x = self.x + dx
        self.y = self.y + dy    

    def distance(self, otherPoint: float): 
        if isinstance(otherPoint, Point):
            x1 = self.x
            y1 = self.y
            x2 = otherPoint.x
            y2 = otherPoint.y

            return ( (x1 - x2)**2 + (y1 - y2)**2 )**0.5    


class Rectangle:
    def __init__(self, topLeft = Point(0,0), bottomRight = Point(1,1)):
        self.topLeft = topLeft
        self.bottomRight = bottomRight

The two points are the top left and the bottom right for the rectangle.两点是矩形的左上角和右下角。 How could I find the area and perimeter of this rectangle from two points?我怎么能从两点找到这个矩形的面积和周长? Would appreciate any and all help!将不胜感激任何和所有的帮助!

We can access the x and y values of each point and calculate the height and width, from there we can create methods that calculate area and perimeter我们可以访问每个点的 x 和 y 值并计算高度和宽度,从那里我们可以创建计算面积和周长的方法

class Rectangle():
    def __init__(self, topLeft = Point(0,0), bottomRight = Point(1,1)):
        self.topLeft = topLeft
        self.bottomRight = bottomRight
        self.height = topLeft.y - bottomRight.y
        self.width = bottomRight.x - topLeft.x
        self.perimeter = (self.height + self.width) * 2
        self.area = self.height * self.width

rect = Rectangle(Point(3,10),Point(4,8))
print(rect.height)
print(rect.width)
print(rect.perimeter)
print(rect.area)
 chrx@chrx:~/python/stackoverflow/9.24$ python3.7 rect.py 2 1 6 2

Or using methods或者使用方法

class Rectangle():
    def __init__(self, topLeft = Point(0,0), bottomRight = Point(1,1)):
        self.topLeft = topLeft
        self.bottomRight = bottomRight
        self.height = topLeft.y - bottomRight.y
        self.width = bottomRight.x - topLeft.x

    def make_perimeter(self):
        self.perimeter = (self.height + self.width) * 2
        return self.perimeter

    def make_area(self):
        self.area = self.height * self.width
        return self.area

rect = Rectangle(Point(3,10),Point(4,8))
print(rect.height)
print(rect.width)
print(rect.make_perimeter())
print(rect.make_area())

暂无
暂无

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

相关问题 如何找到包含一组点/多边形的最大面积矩形? - How to find the largest area rectangle inscribing a set of points/polygon? 如何在两点周围画一个矩形 - How to draw a rectangle around two points 找到给定点的最小面积矩形以计算长轴和短轴长度的算法 - Algorithm to find the minimum-area-rectangle for given points in order to compute the major and minor axis length 如何在 matplotlib 图表中找到矩形的“真实”区域? - How do I find the 'real' area of a rectangle in a matplotlib chart? 如何在轮廓区域内找到并绘制最大的矩形? - How to find and draw largest rectangle inside contour area? 如何使用 OpenCV python 绘制平行于最小面积矩形宽度的线连接相对的轮廓点 - How to draw lines parallel to the width of minimum area rectangle joing opposite contour points using OpenCV python 如何找到包含两个部分的 geojson 多边形的区域,例如一个离海岸有一个岛屿的县? - How can I find the area of a geojson polygon that has two parts, such as a county with an island off the coast? 用于选择绘图区域的矩形并找到最大值 - Rectangle to select area in plot and find maximum value 尝试使用矩形和坐标类的测试函数来查找矩形的面积 - Trying out a test function for rectangle and coordinate classes to find area of the rectangle 直方图的最大面积矩形。 查找最大矩形的尺寸 - Maximum Area Rectangle In Histogram. Find dimensions of the Max Rectangle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM