Family Name:.............................. Other Names:............................. ID Number:............................... Signature.................................. Solution Notes COMP 151: Terms Test 17 th August 2016 Instructions Time allowed: 45 minutes. Answer all the questions. There are four questions and 45 marks in total. Write your answers in the boxes in this test paper and hand in pages 1 10. There is brief Processing documentation on page 11, which you may detach from the test paper. If you think some question is unclear, ask for clarification. This test contributes 10% of your final grade You may use paper translation dictionaries. No other materials are permitted. You may write notes and working on this paper, but make sure your answers are clear. Questions Marks 1. Straight line algorithm [15 2. Graphical output in Processing [10 3. Vectors and dot products [10 4. Specifying objects [10 TOTAL
SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP 151 (Terms Test) Page 2 of 11
1. Straight line algorithm (15 marks) Write an algorithm for drawing a straight line that uses only integer arithmetic. Assume that the line starts at (x 0, y 0 ) and ends at (x 1, y 1 ). Assume that x 0 < x 1. Assume that the slope of the line is between 0 and 45. Assume that x 0, y 0, x 1, y 1 are all integers. Assume that function drawpixel(x,y) turns on the pixel at integer location (x, y). You may write in Java code, Processing code, or pseudo-code. Minor syntax errors will not be penalised. void drawline( int x0, int y0, int x1, int y1 ) { int dx = (x1 - x0) ; int d2x = 2*dx ; int d2y = 2*(y1 - y0) ; int x = x0 ; int yf = 0 ; int y = y0 ; while (x <= x1) { drawpixel(x,y) x++ ; yf += d2y ; if( yf > dx ) { y++ ; yf -= d2x ; // This is Bresenham s algorithm. There are other ways to do this, // in particular the integer version of the Midpoint algorithm. COMP 151 (Terms Test) Page 3 of 11
SPARE PAGE FOR EXTRA ANSWERS Cross out rough working that you do not want marked. Specify the question number for work that you do want marked. COMP 151 (Terms Test) Page 4 of 11
2. Graphical output in Processing (10 marks) (a) (6 marks) Double loops Sketch what the following code will draw in the Processing window. size( 300, 300 ) ; background( 255 ) ; stroke( 0 ) ; fill( 255 ); for( int i = 0 ; i < 3 ; i++ ) { for( int j = 0 ; j < 3 ; j++ ) { if( i == j ) { ellipse( i*100 + 50, j * 100 + 50, 80, 40 ) ; else { rect( i*100 + 30, j * 100 + 10, 40, 80 ) ; (300 300 Processing window) (Question 2 continued on next page) COMP 151 (Terms Test) Page 5 of 11
(Question 2 continued) (b) (4 marks) Transformations Sketch what the following code will draw in the Processing window. size( 300, 300 ) ; background( 255 ) ; stroke( 0 ) ; fill( 255 ) ; translate( 150, 150 ) ; for( int i = 0 ; i < 8 ; i++ ) { rotate( PI / 4 ) ; ellipse( 100, 0, 20, 50 ) ; (300 300 Processing window) (Question 2 continued on next page) COMP 151 (Terms Test) Page 6 of 11
(Question 2 continued) Extra copies (in case you made a mistake) (spare copy of 300 300 Processing window) (spare copy of 300 300 Processing window) COMP 151 (Terms Test) Page 7 of 11
3. Vectors and dot products (10 marks) (a) (4 marks) Vector algebra You are given two vectors, p =. Evaluate the following expressions. [ 5 2 and q = [ 3 4 Vector addition p + q = [ 8 6 Vector subtraction p q = [ 2 2 Dot product p q = 5 3 + 2 4 = 23 Magnitude q = 32 + 4 2 = 5 (Question 3 continued on next page) COMP 151 (Terms Test) Page 8 of 11
(Question 3 continued) (b) (6 marks) Unit vectors The diagram shows a unit circle (a circle of radius 1) with four unit vectors (vectors of length 1): a, b, c and d. The pair of vectors a and b are at right angles (90 or π/2 radians) to one another, as are each of the pairs b and c, c and d, d and a. Vectors a and c are co-linear (i.e., they lie on the same infinite line). Vectors b and d are co-linear. From the definitions of cosine and sine, we know that the coordinates of a are [ cos θ a =. sin θ What are the coordinates of these other two vectors? b = [ sin θ cos θ c = [ cos θ sin θ What are these two dot products? a b = 0 a c = 1= cos 2 θ sin 2 θ COMP 151 (Terms Test) Page 9 of 11
4. Specifying objects (10 marks) A point in 2D is specified by two numbers: its x-coordinate and its y-coordinate. These are called its parameters. An axis-aligned rectangle, such as generated by Processing s rect() function, requires four parameters, for example, the x- and y-coordinates of its top left corner and its width and height. Give the minimum number of parameters required to specify each of the following graphical objects in 2D and list an example of those parameters. The first two are done for you. Graphical object point, such as generated by Processing s point() function Minimum number of parameters List of parameters 2 x, y (position of the point) axis-aligned rectangle, such as generated by Processing s rect() function 4 top, left, width, height (position of the top left corner and the dimensions [width and height of the rectangle) line segment, such as generated by Processing s line() function 4 (x 0, y 0 ), (x 1, y 1 ): the positions of the end points of the line vector 2 x, y infinitely long line 2* angle of line and perpendicular distance of line from origin* rectangle that is not aligned with the axes 5 centre position (x, y), width, height, rotation angle Bézier cubic spline 8 positions of the four control points: (x 0, y 0 ), (x 1, y 1 ), (x 2, y 2 ), (x 3, y 3 ) * Other plausible answers that get half credit: 2, m and c in y = mx + c (but cannot represent a vertical line) 3, a, b, c in the equation ax + by + c = 0 COMP 151 (Terms Test) Page 10 of 11
Brief Processing documentation void point( float x, float y ) // draw a point at location (x,y) void line( float x1, float y1, float x2, float y2) // draw a line segment from location (x1, y1) to location (x2, y2) void rect( float left, float top, float width, float height ) // draw a rectangle with its top left corner at the indicated position // of size width times height void ellipse( float centerx, float centery, float width, float height ) // draw an ellipse centred at the indicated position // of size width times height void triangle( float x1, float y1, float x2, float y2, float x3, float y3 ) // draw a triangle with vertices at the three points // (x1,y1 ), (x2,y2 ), (x3, y3) void background( float grey ) void stroke( float grey ) void fill( float grey ) // set the colour of the background, stroke or fill to be // a grey value between 0 ( black ) and 255 ( white ) void nostroke() void nofill() // turn off drawing strokes around objects and filling objects, respectively * * * * * * * * * * * * * * * COMP 151 (Terms Test) Page 11 of 11