Untitled

 avatar
unknown
plain_text
10 months ago
5.9 kB
14
Indexable
import turtle

class Rectangle:
    def __init__(self, x, y, width, height):
            self.x = x
                    self.y = y
                            self.width = width
                                    self.height = height

                                        def getArea(self):
                                                return self.width * self.height

                                                    def getPerimeter(self):
                                                            return 2 * (self.width + self.height)

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

                                                                            def intersect(self, rec):
                                                                                    left   = max(self.x, rec.x)
                                                                                            right  = min(self.x + self.width,  rec.x + rec.width)
                                                                                                    bottom = max(self.y - self.height, rec.y - rec.height)
                                                                                                            top    = min(self.y, rec.y)
                                                                                                                    if right <= left or top <= bottom:
                                                                                                                                return None
                                                                                                                                        return Rectangle(left, top, right - left, top - bottom)

                                                                                                                                            def draw(self):
                                                                                                                                                    turtle.penup()
                                                                                                                                                            turtle.goto(self.x, self.y)
                                                                                                                                                                    turtle.pendown()
                                                                                                                                                                            turtle.begin_fill()
                                                                                                                                                                                    for d in [self.width, -self.height, -self.width, self.height]:
                                                                                                                                                                                                turtle.forward(d)
                                                                                                                                                                                                            turtle.right(90)
                                                                                                                                                                                                                    turtle.end_fill()


                                                                                                                                                                                                                    def main():
                                                                                                                                                                                                                        a = Rectangle(-150, 100, 140, 120)
                                                                                                                                                                                                                            b = Rectangle(-20, 60, 200, 180)
                                                                                                                                                                                                                                c = a.intersect(b)
                                                                                                                                                                                                                                    a.draw()
                                                                                                                                                                                                                                        b.draw()
                                                                                                                                                                                                                                            if c: c.draw()
                                                                                                                                                                                                                                                turtle.done()

                                                                                                                                                                                                                                                main()
Editor is loading...
Leave a Comment