简体   繁体   中英

A point class and a rectangle class without using inheritance

I am trying to create a point class that has the data attribute of floating point values 'x' and 'y' to therefore define a location of a point in 2D space. Additionally I want to have methods of init such as it being an initialization of default values x = 0 and y = 0. And then a move function which accepts the 'x' and 'y' as a new location of the point. And finally a function which tells the distance to a point. I want this to return the Euclidian distance from this point to another point at x, y. How would this be done?

Here is the code I have so far for the above description:

import math

class Point:

    def __init__(self):
        self.x = 0 # initialize to 0
        self.y = 0 # initialize to 0

    def move(self, x, y):
        self.x = x
        self.y = y 

Could use help on this and the Eucliean distance from this point to another point at x,y. Not sure if I have the right idea so far. New to python so don't know how to test the functionality of this code. Would appreciate the help!

After that part I'd to be able to define two points at two opposite corners of a rectangle and use the Points defined above without using inheritance. Any ideas on how to create this class?

You could do something like this (comments in the code):

example:

class Point:

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

    def move_by(self, dx: float, dy: float)-> None:   # move Point by dx, dy
        self.x += dx
        self.y += dy 

    def move_to(self, new_x: float, new_y: float)-> None:   # relocate Point to new x, y position
        self.x = new_x
        self.y = new_y 

    def distance(self, other: 'Point')-> float:   # calculates and returns the Euclidian distance between self and other
        if isinstance(other, Point):
            x0, y0 = self.x, self.y
            x1, y1 = other.x, other.y
            return ((x1 - x0)**2 + (y1 - y0)**2) ** 0.5
        return NotImplemented

    def __str__(self)-> str:    # add a nice string representation
        return f'Point({self.x}, {self.y})'

tests:

p = Point(1, 2)
q = Point()
print(p, q, p.distance(q) == 5**0.5)
p.move_by(.1, -.1)
print(p)

output:

Point(1, 2) Point(0.0, 0.0) True
Point(1.1, 1.9)

And the Rectangle class could be like this: [edited to add default Point values]

In Rectangle.__init__ , the min and max values of the x and y values of the Points provided as parameters are sorted in order to determine the top left and bottom right points defining the rectangle

class Rectangle:
        def __init__(self, p0: Point=Point(), p1: Point=Point(1.0, 1.0))-> None:  # <--- [edit]: now with default values
        x0, y0 = p0.x, p0.y
        x1, y1 = p1.x, p1.y
        self.topleft = Point(min(x0, x1), max(y0, y1))      # calculate the topleft and bottomright
        self.bottomright = Point(max(x0, x1), min(y0, y1))  # of the bounding box

    def __str__(self)-> str:
        return f'Rectangle defined by bbox at: {self.topleft}, {self.bottomright})'

tests:

p = Point(1, 2)
q = Point()
print(p, q, p.distance(q) == 5**0.5)
p.move_by(.1, -.1)
print(p)

r = Rectangle(p, q)
print(r)

output:

Point(1, 2) Point(0.0, 0.0) True
Point(1.1, 1.9)
Rectangle defined by bbox at: Point(0.0, 1.9), Point(1.1, 0.0))

[Edit:] with default values for Points in Rectangle:

s = Rectangle()
print(s)

output:

Rectangle defined by bbox at: Point(0.0, 1.0), Point(1.0, 0.0))

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