Press enter to skip the top menu

Computer Graphics CS

Scaling

Download Program Code

Learning Outcomes

On completion of this lesson you will know to proportionally scale a quadrilateral.

Go to top

Introduction

This is possibly the easiest lesson in this group. We only need to modify the function drawShape(), which we have renamed scaleShape(). To modify it we simply replaced the trigonometry with addition.

We are also using a simple method of scaling: we are simply adding the same values to the width and height of the quadrilateral for each frame.

Go to top

Drawing a shape

Listing 1: the function scaleShape()
                        
                        def scaleShape(currentSize, maxRows, maxCols):
                        
                            x1 = currentSize
                            y1 = round(currentSize/8) 
                            x3 = -1*x1 
                            y3 = -1*y1 
                            x2 = x1 * -1
                            y2 = y1
                            x4 = -1*x2
                            y4 = -1*y2
                        
                            x1 += (maxRows)
                            y1 += (maxCols)
                            x2 += (maxRows)
                            y2 += (maxCols)
                            x3 += (maxRows)
                            y3 += (maxCols)
                            x4 += (maxRows)
                            y4 += (maxCols)
                        
                            shape = []
                            coord = []
                            coord.append(round(x1))
                            coord.append(round(y1))
                            shape.append(coord)
                            coord = []
                            coord.append(round(x2))
                            coord.append(round(y2))
                            shape.append(coord)
                            coord = []
                            coord.append(round(x3))
                            coord.append(round(y3))
                            shape.append(coord)
                            coord = []
                            coord.append(round(x4))
                            coord.append(round(y4))
                            shape.append(coord)
                            return shape
                    

As in the previous example we build our quadrilateral is built around the origin, (0, 0). We supply only the value of the one vertex. From that, using only addition and subtraction, we work out the other three vertices.

Also following the previous example we shall look at how the coordinates of the other three vertices are calculated once the first vertex is supplied. We shall use the starting value of 200 pixels

This value is supplied to the function through the argument currentSize at line 12. At line 14 this value is copied into the x coordinate of the first vertex, x1. The value of y1 is calculated at line 15 as one eighth of x1.

As before, at lines 16 and 17, the values of x3 and y3 are calculated as the negatives of x1 and y1.

At lines 18 and 19 the value of x2 is calculated as the negative of x1 while the value of y2 is calculated as the same as y1. At lines 20 and 21 the values of x4 and y4 are calculated as the negatives of x2 and y2.

At lines 23 to 30 all eight coordinates have their values adjusted so that now they are centered around the point (400, 400).

Fig 1

Fig 1 above shows how the coordinates of the vertices are positioned around (0, 0) and then adjusted so that they are centered around (400, 400).

Go to top

The main function

                        def main():
                            arrPage = []
                            strFileName="Scale"
                            intVersionNumber = 0
                            intMaxCols=800
                            intMaxRows=800
                            for size in range(20, 381,6):
                                arrPage = createBackground(intMaxCols,intMaxRows)
                                arrPage = fillShape(arrPage, scaleShape(size,intMaxCols/2,intMaxRows/2), intMaxRows, intMaxCols)
                                saveFile(arrPage,intMaxRows, intMaxCols,strFileName, intVersionNumber)
                                intVersionNumber+=1
                                arrPage=[]
                        
                        if __name__ == "__main__":
                            main()
                        
                        print("Programme finished")
                    

The for loop at line 102 controls the creations of our frames. The current value of the loop counter size is passed as the first argument to the function scaleShape() and determines the size of the quadrilateral that is to be drawn. The smallest size is 20 pixels and this increases by increments of 6 as far as 380. This will result in 61 frames where the quadrilateral increases in size in each successive frame.

Go to top

End product

Two sets of frames have been put back to back here to give the impression of a quadrilateral ever increasing and decreasing in size.

Animation of a rectangle increasing and decreasing
Fig 12
Go to top