Learning Outcomes
On completion of this lesson you will be able to specify the coordinates of a quadrilateral and use techniques related to coordinate geometry to draw the actual shape
Go to topOn completion of this lesson you will be able to specify the coordinates of a quadrilateral and use techniques related to coordinate geometry to draw the actual shape
Go to topThis lesson follows directly on from the previous lesson. That lesson was devoted to drawing individual lines while the current one concentrates on drawing a closed quadrilateral shape by drawing the individual lines that make up that shape.
For a change we shall not be examining the entire code of the application. This is because most of the function are identical to their equivalent in the previous lesson. We shall instead concentrate on newly added functions as well as existing functions that we have modified.
Go to topThis version of the function main() is much shorter than its predecessor. This is because at line 76 the coordinates for our shape are passed to the function drawShape() as a two dimensional list. In the previous version eight different lines were used, one for each line we wished to draw.
On returnig from this function call line 78 saves the data to a file.
Go to topThe function drawLine() is esentially the same as its predecessor except that we have done a some tidying up.
At lines 13 and 14 the gradient of the line is calculated provided it is not a vertical line. (The gradient of a vertical line is an undefined number.
Lines 15 to 19 control the drawing of a horizontal line, while lines 21 to 24 control the drawing of a vertical line.
Lines 25 to 44 control the drawing of all lines apart from the vertical and the horizontal. Of those range, lines 25 to 35 control the lines whose gradients lie between -1 and 1 (excluding horizontal lines) while lines 37 to 44 cater for lines whose gradients are greater than 1 or less than -1.
Go to topLike its predecessor the function drawLine() can only process a single line using its x and y coordinates. To overcome this we have the intermediary function drawShape(). Its arguemt shape is passed to it from line 76 of the function main().
It is controlled by a for loop that spans lines 48 to 53. It reads the start and end coordinates of each line, and then passes those to drawLine() for actually drawing the line.
When all of the lines are drawn, line 54 passes control back to line 78 of main. Here the saveFile() is called to save the data.
The outut is shown below.
Go to top