Add a Margin

various: Up to 15 points


Objective

This exercise is meant to give you some practice adding a new feature to an existing program. The feature itself is simple: you’ll need to modify the VisualDataSet class to add the ability to define a margin between the edges of the image and the area in which the foreground bars can appear:

Example of a 640 x 440 display with a 40-pixel margin

For this exercise, the margin will always be the same on all sides of the image. In the example above, the display is 640 x 440 pixels and has a margin of 40 pixels, so the “data display area” shown is a 560 x 360 pixel rectangle in the center of the image.

Requirements

  • The size of the margin in pixels should be provided when the object is created.
  • It should be possible to create multiple instances of VisualDataSet with different margins.
  • The size of the dataset, as well as its maximum and minimum values should be based on the size of the data display area rather than the entire image.
  • Adding this feature should not change the behavior of main.py or any python files other than visualdataset.py.

Testing

To check that your margin feature is implemented correctly, modify test_visualdataset_class to test a margin of 20 using values of 640, 440 and 18 for width, height and bar_width, respectively. If working properly, running python3 visualdataset should produce terminal output similar to the following:

Expected number of values: 30
Actual number of values: 30
The lowest value should be no lower than 20.
Actual lowest value: 34
The highest value should be no higher than 400.
Actual highest value: 377
Generated values:
[94, 257, 241, 162, 145, 34, 75, 319, 46, 188, 275, 293, 377, 318, 70, 302, 35, 352, 242, 100, 278, 136, 66, 191, 259, 336, 35, 210, 329, 366]

Using the generated values listed above, the display would look like this:

Example of 640 x 440 display with 20-pixel margin

Hints

  • You’ll need to adjust other parts of your code to account for the new feature while preserving the program’s original behavior.
  • In particular, adding a parameter to the __init__ method will affect any code you have anywhere in your program that creates a VisualDataSet object. You will need to update this to avoid breaking your program.
  • When the margin is set to 0, the VisualDataSet class should behave exactly as it was supposed to before you added this feature.

Caution!

Watch out for unintended side-effects! Make sure to check all your code carefully to verify that it still works and its behavior has not changed.