Computer Graphics Solution to Homework 0 CS417/CS418 Fall 1998 Due Wednesday, January 27, 1999 * This diagnostic homework will be weighted to count much less than other homeworks.* For this homework, NO PARTNERS.* Hand in each part (CS417, CS418, administrative) SEPARATELY.* As always in this class, you are graded on correctness and also clarity and conciseness. Administrative Part 0. Fill out the waiver AND e-mail the requested information AND write legibly & sign & date on your homework the following statement, filling in your own name and CUID# as appropriate: "I, hyour namei, CUID#hyour CUID#i, wrote up this assignment." You must complete this question to receive credit; e-mail will be accepted until Friday midnight. CS417 Part 1. Suppose r is a root of the polynomial x2 + bx + c, i.e. r2 + br + c = 0. Express the other root of the polynomial in terms of r, b, c using only subtraction or division -- square root and exponentiation (powers) are not allowed. HINT: Suppose the polynomial were factored into two linear terms; how are the terms related to the roots and coefficients of the polynomial? Answer: If s is the other root, then the polynomial factors as (x - r)(x - s) = x2 - (r + s)x + rs = x2 + bx + c, so b = -(r + s) and c = rs, so s = -b - r and if r 6= 0 then also s = c/r. 2. Use the following formulas: cos(` + OE) = cos(`) cos(OE) - sin(`) sin(OE) (1) sin(` + OE) = sin(`) cos(OE) + cos(`) sin(OE) (2) (x, y) = (r cos(`), r sin(`)), for r = px2 + y2 and some `. (3) to derive a formula for the result (x0, y0) of rotating (x, y) clockwise ae radians around the origin. Show your work. Your final answer should involve only x, y, ae -- neither r nor ` should appear. HINT: Does the radius (distance r from the origin) change? What's the new angle? Answer: If the original radius and angle are r and ` from (3), then the new radius and angle are r and ` + OE. Substituting ` + OE for ` into (3) yields (x0, y0) = (r cos(` + OE), r sin(` + OE)) = (r[cos(`) cos(OE) - sin(`) sin(OE)], r[sin(`) cos(OE) + cos(`) sin(OE)]) by (1), (2) = (r cos(`) cos(OE) - r sin(`) sin(OE), r sin(`) cos(OE) + r cos(`) sin(OE)) arithmetic = (x cos(OE) - y sin(OE), y cos(OE) + x sin(OE)) by (3). 1 3. Let a = (xa, ya) and b = (xb, yb) be two vectors, i.e. two points in the plane R2. (a) Give a formula for the projection c = (xc, yc) of vector a onto vector b, i.e. the point c obtained by projecting point a onto the line defined by the origin and point b. Answer: The answer from standard linear algebra is c = a * bb * b b, i.e. (xc, yc) = (txb, tyb) with t = xaxb + yaybx2 b + y2b . (b) Give a formula for the reflection d = (xd, yd) of vector a across vector b, i.e. the point d obtained by reflecting point a across the line defined by the origin and point b. HINT: You may use c in your answer even if you couldn't figure out its formula. Answer: Geometry tells us d-c = c-a, so d = 2c-a, i.e. (xd, yd) = (2xc-xa, 2yc-ya). CS418 Part 4. Enter type sphere at the Matlab prompt to see the code (including helpful comments) for sphere, which is useful for plotting a sphere with equally many latitude and longitude lines. Write a Matlab function mysphere so that [x,y,z] = mysphere(m,n); surf(x,y,z) plots a sphere with m latitude lines and n longitude lines. Demonstrate with m = 11 and n = 40. Turn in your code and a printout of the plot. (If you're uncomfortable writing a function, then instead write a script/sequence of commands. In this case, your code should start with m=11; n=40 and never again reference constants 11,40). HINT: Figure out the definitions of theta, phi, cosphi, and sintheta and alter these four lines of code appropriately (no other computations need to be changed). Answer: Observe that the comments have been updated. Thus, help mysphere will actually BE helpful, and also now we know which variables are for latitude and which for longitude. function [xx,yy,zz] = mysphere(m,n) %MYSPHERE Generate sphere. % [X,Y,Z] = MYSPHERE(M,N) generates three (m+1)-by-(n+1) % matrices so that SURF(X,Y,Z) produces a unit sphere % with m latitude and n longitude lines. % Modified from code by Clay M. Thompson 4-24-91, CBM 8-21-92. % -pi <= theta <= pi is a row vector -- longitude % -pi/2 <= phi <= pi/2 is a column vector -- latitude theta = (-n:2:n)/n*pi; phi = (-m:2:m)'/m*pi/2; cosphi = cos(phi); cosphi(1) = 0; cosphi(m+1) = 0; sintheta = sin(theta); sintheta(1) = 0; sintheta(n+1) = 0; xx = cosphi*cos(theta); yy = cosphi*sintheta; zz = sin(phi)*ones(1,n+1); Here's how we use it: [x,y,z]=mysphere(11,40); surf(x,y,z); % extra: fix aspect ratio, hide axes axis equal; axis off 2