예제 #1
0
class Rectangle:
    """Klasa reprezentująca prostokąt na płaszczyźnie."""

    def __init__(self, x1, y1, x2, y2):
        self.pt1 = Point(x1, y1)
        self.pt2 = Point(x2, y2)

    def __str__(self):         # "[(x1, y1), (x2, y2)]"
        return "[" + self.pt1.__str__() + ", " + self.pt2.__str__() + "]"

    def __repr__(self):         # "Rectangle(x1, y1, x2, y2)"
        return "Rectangle[" + self.pt1.__str__() + ", " + self.pt2.__str__() + "]"

    def __eq__(self, other):   # obsługa rect1 == rect2
        return self.pt1.__eq__(other.pt1) and self.pt2.__eq__(other.pt2)

    def __ne__(self, other):        # obsługa rect1 != rect2
        return not self == other

    def center(self):          # zwraca środek prostokąta
        srodek = Point(float(self.pt1.x + self.pt2.x) / 2, float(self.pt1.y + self.pt2.y) /2)
        return srodek

    def area(self):            # pole powierzchni
        width = self.pt2.x - self.pt1.x
        hight = self.pt2.y - self.pt1.y
        return width * hight

    def move(self, x, y):      # przesunięcie o (x, y)
        self.pt1 = Point(self.pt1.x + x, self.pt1.y + y)
        self.pt2 = Point(self.pt2.x + x, self.pt2.y + y)
예제 #2
0
class Rectangle:
    """Klasa reprezentująca prostokąty na płaszczyźnie."""
    def __init__(self, x1, y1, x2, y2):
        try:
            if x1 >= x2 or y1 >= y2:
                raise ValueError
        except ValueError:
            print("x1 >= x2 or y1 >= y2")
        else:
            self.pt1 = Point(x1, y1)
            self.pt2 = Point(x2, y2)

    def __str__(self):  # "[(x1, y1), (x2, y2)]"
        return "[" + self.pt1.__str__() + ", " + self.pt2.__str__() + "]"

    def __repr__(self):  # "Rectangle(x1, y1, x2, y2)"
        return Rectangle.__name__ + "(" + str(self.pt1.x) + ", " + str(
            self.pt1.y) + ", " + str(self.pt2.x) + ", " + str(self.pt2.y) + ")"

    def __eq__(self, other):  # obsługa rect1 == rect2
        if (self.pt1 == other.pt1 and self.pt2 == other.pt2):
            return True
        else:
            return False

    def __ne__(self, other):  # obsługa rect1 != rect2
        return not self == other

    def center(self):  # zwraca środek prostokąta
        return Point(((self.pt2.x - self.pt1.x) / 2),
                     ((self.pt2.y - self.pt1.y) / 2))

    def area(self):  # pole powierzchni
        return ((self.pt2.x - self.pt1.x) * (self.pt2.y - self.pt1.y))

    def move(self, x, y):  # przesunięcie o (x, y)
        return Rectangle(self.pt1.x + x, self.pt1.y + y, self.pt2.x + x,
                         self.pt2.y + y)

    def intersection(self, other):  # część wspólna prostokątów
        return Rectangle(max(self.pt1.x, other.pt1.x),
                         max(self.pt1.y, other.pt1.y),
                         min(self.pt2.x, other.pt2.x),
                         min(self.pt2.y, other.pt2.y))

    def cover(self, other):  # prostąkąt nakrywający oba
        return Rectangle(min(self.pt1.x, other.pt1.x),
                         min(self.pt1.y, other.pt1.y),
                         max(self.pt2.x, other.pt2.x),
                         max(self.pt2.y, other.pt2.y))

    def make4(self):  # zwraca krotkę czterech mniejszych
        srodek = self.center()
        lewyDolny = Rectangle(self.pt1.x, self.pt1.y, srodek.x, srodek.y)
        prawyDolny = Rectangle(srodek.x, self.pt1.y, self.pt2.x, srodek.y)
        prawyGorny = Rectangle(srodek.x, srodek.y, self.pt2.x, self.pt2.y)
        lewyGorny = Rectangle(self.pt1.x, srodek.y, srodek.x, self.pt2.y)
        return (lewyDolny, prawyDolny, prawyGorny, lewyGorny)
예제 #3
0
class Rectangle:
    """Klasa reprezentująca prostokąt na płaszczyźnie."""

    def __init__(self, x1, y1, x2, y2):
        if x1 > x2 or y1 > y2:
            raise ValueError, "Zle wartości punktów"
        else:
            self.pt1 = Point(x1, y1)
            self.pt2 = Point(x2, y2)
        
    def __str__(self):         # "[(x1, y1), (x2, y2)]"
        return "[" + self.pt1.__str__() + ", " + self.pt2.__str__() + "]"

    def __repr__(self):         # "Rectangle(x1, y1, x2, y2)"
        return "Rectangle[" + self.pt1.__str__() + ", " + self.pt2.__str__() + "]"

    def __eq__(self, other):   # obsługa rect1 == rect2
        return self.pt1.__eq__(other.pt1) and self.pt2.__eq__(other.pt2)

    def __ne__(self, other):        # obsługa rect1 != rect2
        return not self == other

    def center(self):          # zwraca środek prostokąta
        srodek = Point(float(self.pt1.x + self.pt2.x) / 2, float(self.pt1.y + self.pt2.y) /2)
        return srodek

    def area(self):            # pole powierzchni
        width = self.pt2.x - self.pt1.x
        hight = self.pt2.y - self.pt1.y
        return width * hight

    def move(self, x, y):      # przesunięcie o (x, y)
        self.pt1 = Point(self.pt1.x + x, self.pt1.y + y)
        self.pt2 = Point(self.pt2.x + x, self.pt2.y + y)

    def intersection(self, other): # część wspólna prostokątów
        x1 = max(self.pt1.x, other.pt1.x)
        y1 = max(self.pt1.y, other.pt1.y)
        x2 = min(self.pt2.x, other.pt2.x)
        y2 = min(self.pt2.y, other.pt2.y)
        if x1 > x2 or y1 > y2:
            raise ValueError, "Brak czesci wspolnej"
        else:
            wspolna = Rectangle(x1, y1, x2, y2)
            return wspolna
            
    def cover(self, other):    # prostąkąt nakrywający oba
        x1 = min(self.pt1.x, other.pt1.x)
        x2 = max(self.pt2.x, other.pt2.x)
        y1 = min(self.pt1.y, other.pt1.y)
        y2 = max(self.pt2.y, other.pt2.y)
        covering = Rectangle(x1, y1, x2, y2)
        return covering

    def make4(self):           # zwraca listę czterech mniejszych
        srodek = self.center()
        rtgl1 = Rectangle(self.pt1.x, srodek.y, srodek.x, self.pt2.y)
        rtgl2 = Rectangle(srodek.x, srodek.y, self.pt2.x, self.pt2.y)
        rtgl3 = Rectangle(self.pt1.x, self.pt1.y, srodek.x, srodek.y)
        rtgl4 = Rectangle(srodek.x, self.pt1.y, self.pt2.x, srodek.y)
        rtgls = [rtgl1, rtgl2, rtgl3, rtgl4]
        return rtgls
예제 #4
0
파일: 6_2.py 프로젝트: MichalGk94/Python
from points import Point

point1 = Point(3, 7)
point2 = Point(-2, 1)
point1.__str__()
point2.__repr__()
print point1.eq(point2)
print point1.ne(point2)
print point1.add(point2)
print point1.sub(point2)
print point1.mul(point2)
print point2.mul(point1)
print point1.cross(point2)
print point2.cross(point1)
print point1.length()
print point2.length()