Measurement Toolkit (part 3)

Surface area calculator

In this tutorial we describe how to add functionality for calculating the surface area of various shapes to the Measurement Toolkit. The screenshot below shows the tool being used to calculate the surface area of a rectangular prism.

Surface Area Calculator

The tool is designed so that we can easily extend it with functionality for calculating the surface area for additional 3d shapes. The main steps involved in adding a new shape to the surface area calculator are:

  1. Research the shape to determine an appropriate formula for calculating its surface area
  2. Design and implement test cases that will be run to check the correctness of calculations in the tool
  3. Code a function to calculate the surface area of the chosen shape
  4. Create an image file to represent the shape in the tool
  5. Link the calculation function and image to the main interface

In the following sections we will look at the process used to add cubes to the surface area calculator.
Given a cube with sides of length a, then the surface area is given by the following formula:
SA=6a^2
The formula is derived by noting that the cube has six faces of equal size. Each face is a square with dimensions of a by a units.

Designing test cases

The next step is design and implement test cases for calculating the surface area of a cube. These should be designed initially on paper before being added to surface area tester file.
For the first test case we consider a cube with sides of length 5 units:

SA=6\times 5^2=6 \times 25 = 150\mathrm{units^2}

For the second test case a side length of 3.275 units is selected. The resulting surface area is rounded to two decimal places:

SA=6\times 3.275^2 = 64.35375 \approx 64.35 \mathrm{units^2}

These test cases were then coded in the surface_area_tester.py file as shown below. The tester file imports the surface area file on line 2, which contains the surface area calculation functions. It is assumed that the function for calculating the surface area of a cube is called calc_cube_sa, which takes a single input representing the length of the cube edges. Because the expected answer for the second test case was rounded to 2 decimal places we use assertAlmostEqual which in this case checks that the value returned by the calc_cube_sa after rounding to 2 decimal places is equal to the expected answer of 64.35.

import unittest
from surface_area import *

class TestSurfaceArea(unittest.TestCase):
    def test_calc_cube_sa(self):
        self.assertEqual(calc_cube_sa(5),150)
        self.assertAlmostEqual(calc_cube_sa(3.275),64.35,2)

if __name__ == '__main__':
    unittest.main()

Note that in the code above we have placed two test cases within the same test function allowing us to group the tests together based on the shape being tested.

Coding the surface area calculation functions

Functions for calculating the surface area of different 3d shapes are include in the surface_area.py file. The following code listing shows the code for the calc_cube_sa function. The file also includes a code stub for calc_rprism_sa, which will be used to calculate the surface area of a rectangular prism.
The file imports the math package which will be used later for doing calculations involving the constant pi.

import math

def calc_cube_sa(length):
    return 6*(length)**2

def calc_rprism_sa(length,width,height):
    return 0

Functions for additional surface area calculations should be added after the given functions.

After the functions have been written they should be tested by running the test cases in surface_area_tester.py.

Creating 3d shapes

Images representing the different 3d shapes used in the surface area calculator tool need to be created and added to the Images directory.
The following link explains how to
create 3d shapes in Illustrator. Another option is to create the 3d shapes using Efofex Draw.

Linking the components in the main interface

The final step in creating and adding to the surface area functionality of the Measurement Toolkit is to incorporate the calculation functions and images in the main mathtoolkit.py file.

##
## ADD NEW SHAPES HERE FOR SURFACE AREA
##
sa_shapes['Cube']=[['l'],"Images/cube.png","calc_cube_sa"]
sa_shapes['Rectangular Prism']=[['l','w','h'],"Images/rect_prism.png","calc_rprism_sa"]

Line 4 shows the configuration for cube. The string inside the first pair of square brackets is the name of the shape as presented in the tool. After the equal symbol, the list of variables, the name of the image file, and finally the name of the calculation function are given.
Line 5 shows a similar configuration for rectangular prisms. In this case the user will be required to enter values for three variables representing the length, width and height respectively.