My shoelace broke the other day. I was too lazy to take it out and figure out how long it was, so I decided to calculate the length based on the number of eyelets and their separation.
The "horizontal" distance between the eyelets can increase or decrease from the bottom to the top of the shoe. The startWidth is how far apart they are at the bottom (near the toes) and the endWidth is how far apart they are at the top (near the tongue of the shoe). This distance increases (or decreases) linearly from bottom to top. Furthermore, the "vertical" distance, spread, between eyelets is constant.
Write a method, calculate, which calculates the length of the entire shoelace, given the initial distance between the eyelets (startWidth), the final distance between the eyelets (endWidth), distance between each pair of vertically adjacent eyelets (spread) and the number of pairs of eyelets (numPairs).
You should assume all laces follow this general pattern:
|----| <- startWidth
O----O -
\ / |
\/ | <- spread
/\ |
/ \ |
O O -
\ /
\/
/\
/ \
O O
|----| <- endWidth
In this illustration, startWidth and endWidth are identical, and numPairs is 3. |