#[Python] Help with encapsulation and inheritance (matplotlib stuff)

7 messages · Page 1 of 1 (latest)

blazing impBOT
#

@olive plinth

File Attachments Not Allowed

For safety reasons we do not allow files with certain file extensions.

shockerhead Said

For starters, yes, I am a noob (I'm not the best at catching up with uni classes).

What the output in matplotlib should look like the picture there (I'm sorry already for the quality lol).

We assume that the W12 file is correctly coded, we just have to alter the template file (ORIGINAL is obviously original, the one not named as such is my take, poorly made and obviously aided by the GPT). I'm assuming that the Square class should only be the one that is changed. Although, my lack of skills make me unable to comprehend what is wrong with my code (and its consequent output) and what should be changed so it would be correct.

Thanks in advance for the help!

Code Formatting

You can share your code using triple backticks like this:
```
YOUR CODE
```

Large Portions of Code

For longer scripts use a code hosting platform like GitHub Gists and share the link here

Ignored these files due to them having disallowed file extensions
  • ORIGINAL_Shapes2D_template.py
  • W12_main.py
  • Shapes2D_template.py
olive plinth
#

For starters, yes, I am a noob (I'm not the best at catching up with uni classes).

What the output in matplotlib should look like the picture there (I'm sorry already for the quality lol).

We assume that the W12 file is correctly coded, we just have to alter the template file (ORIGINAL is obviously original, the one not named as such is my take, poorly made and obviously aided by the GPT). I'm assuming that the Square class should only be the one that is changed. Although, my lack of skills make me unable to comprehend what is wrong with my code (and its consequent output) and what should be changed so it would be correct.

Thanks in advance for the help!

Here are the pastebin links (sorry mods):

ORIGINAL TEMPLATE: https://pastebin.com/saqQfP9G
MY TEMPLATE (sorry again lol): https://pastebin.com/FxfwV7sY
MAIN FILE (assumed as correct): https://pastebin.com/wW16CPZ1

#

Here is the expected output. (Task 1 at the topmost, 2 at the right, 3 at the left). I think I can do Task 4 by myself, I only have issues with 1-3.

light yoke
#

ight lets see

hoary gate
#

Might be easier if you explain what you’re having trouble with rather than us trying to dig through 300 lines of code trying to decipher where it’s broken

bright pewter
#

looks like you create 3 different plots,
with a few different plots drawnnon them

olive plinth
# hoary gate Might be easier if you explain what you’re having trouble with rather than us tr...

My bad. It's just the Square class from the template file that needs to be rectified so the output can be like the one I've sent.

    def __init__(self, side=1, center=(0,0)):
        """
        Square Constructor
            Defaults to Unit Square
            centered at the origin.
        """
        self._side = side
        self._center = Point2D(*center)
        self._update_points()

    def _update_points(self):
        half_side = self._side / 2
        center_x, center_y = self._center.getX(), self._center.getY()
        self._points = [Point2D(center_x + half_side, center_y + half_side), Point2D(center_x - half_side, center_y + half_side), Point2D(center_x - half_side, center_y - half_side), Point2D(center_x + half_side, center_y - half_side)]
        self._nsides = len(self._points)

    def translateX(self, dx):
        self._center.translateX(dx)
        self._update_points()

    def translateY(self, dy):
        self._center.translateY(dy)
        self._update_points()

    def rotate(self, th):
        center_x, center_y = self._center.getX(), self._center.getY()
        for point in self._points:
            point.translateX(-center_x)
            point.translateY(-center_y)
            point.rotate(th)
            point.translateX(center_x)
            point.translateY(center_y)

    def scale(self, r):
        """
        Increases square side length by a factor of r.
        Center of the square is maintained.
        """
        self._side *= r
        self._update_points()
    
    def get_center(self):
        return (self._center.getX(), self._center.getY())
    
    def set_center(self, c):
        dx = c[0] - self._center.getX()
        dy = c[1] - self._center.getY()
        self.translateX(dx)
        self.translateY(dy)

    def copy(self):
        return Square(self._side, (self._center.getX(), self._center.getY()))```