简体   繁体   English

如何用python中的topLeft点(0,0)和bottomRight点(1,1)两点初始化矩形类?

[英]How does one initialize a rectangle class with two points, topLeft point (0,0) and bottomRight point (1,1) in python?

I want to be able to create a rectangle where it has the data attribute of two points defining two opposite corners of a rectangle as, use the Points defined above, not using inheritance. 我希望能够创建一个矩形,在该矩形中具有两个点的数据属性,它们定义一个矩形的两个相对角,使用上面定义的点,而不使用继承。 But I am having trouble with the initialization method in the rectangle class and a few methods and not sure if I am going about this the correct way. 但是我在矩形类中的初始化方法和一些方法上遇到了麻烦,不确定我是否要以正确的方式进行操作。 I want the init : initialize with default p1 = (0,0), p2 = (1,1) 我想要init :初始化为默认p1 =(0,0),p2 =(1,1)

Here is what I have so far for the Point class: 到目前为止,这是我为Point类准备的:

import math

class Point:

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


    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

This all appears to work as expecte when I create a point. 当我创建一个点时,所有这些似乎都可以正常工作。

p1 = Point()

print(p1.x, p1.y)

>>>> 0 0

But my Rectangle class is not working when I create a blank Rectangle object. 但是,当我创建一个空白的Rectangle对象时,我的Rectangle类无法正常工作。 Here is the code: 这是代码:

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

I can't seem to find a way to have like I had it in the Point class where the Point is initialized from the get go to x=0, and y=0. 我似乎无法找到一种方法,就像我在Point类中那样,将Point从get初始化为x = 0和y = 0。 Is there any way to do this in the Rectangle class? 在Rectangle类中有什么方法可以做到这一点? I tried the following but it wasn't allowed: 我尝试了以下操作,但不允许这样做:

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

When I run the code I get errors that I cannot initialize like this. 运行代码时,出现无法初始化的错误。

r1 = Rectangle()

print(r1.topLeft, r1.bottomRight)

After the initialization I want to be able to pass in Points I made. 初始化后,我希望能够传递我的得分。

Finally, from this I am trying to create two methods Get_area to return the area of the rectangle as a floating point value and Get_perimeter to return the perimeter as a floating point value. 最后,从这开始,我尝试创建两个方法Get_area来返回矩形的面积作为浮点值,并创建Get_perimeter来返回周长作为浮点值。

The problem is that you wrote Class instead of class . 问题是您写的是Class而不是class Your code works fine when it is rectified 您的代码在更正后可以正常工作

You wrote 写了

Class Rectangle:

You should write 应该

class Rectangle:

Complete: 完成:

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

r1 = Rectangle()
print(r1.topLeft, r1.bottomRight)    
> (0, 0) (1, 1)

Use the following way to override the default values 使用以下方式覆盖默认值

r1 = Rectangle(topLeft = (0,0.5), bottomRight = (1,1))

Edit 2: Overriding the defaults 编辑2:覆盖默认值

p1 = (3,5) 
p2 = (6,10)
r1 = Rectangle() 
print (r1.topLeft, r1.bottomRight)
> (0, 0) (1, 1)

r2 = Rectangle(p1, p2)
print (r2.topLeft, r2.bottomRight)
> (3, 5) (6, 10)

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

相关问题 计算A点类Python中两点之间的距离 - Calculating the distance between two points in A Point Class Python Python测试点是否在矩形中 - Python Test If Point is in Rectangle Python-点类未返回正确点 - Python - point class not returning correct points 如何缩放相对于特定点的点作为原点,而不是原点(0,0)? - How to scale a point relative to a specific point as origin, instead of origin (0,0)? Python - 在 Point 中调用方法来检查 Point 是否在矩形中 - Python - calling on method in Point to check if Point is in Rectangle 如何在 python 中生成一组二维点,其中任何点都在至少一个其他点的 R 单位内 - How to generate a set of 2D points in python where any point is within R units of at least one other point 如何在矩形的周长中找到一个点? - How to find a point in the perimeter of a rectangle? 我如何计算字符串中的浮点数,将浮点数识别为一个数字? -Python - How can I count number of floating points in a string, recognizing the floating point as one number? - Python 不使用继承的点类和矩形类 - A point class and a rectangle class without using inheritance Python(长度输入以绘制中心点为 0,0 的星形) - Python (Length input to draw star with a center point of 0,0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM