Gameplay in HTML5: Homework #3 Solutions

Vectors

  1. u = [ -1, 3 ], v = [ 7, 2 ], w = [ 5, 0 ], e = [ 1, 0 ], and f = [ 0, 1 ].
    1. ux = -1.
    2. vy = 2.
    3. w1 = 0 if we use 0-based indices, but w1 = 5 if we use 1-based indices.
    4. |u| = sqrt( (-1)2 + 32 ) = sqrt( 1 + 9 ) = sqrt( 10 ) = 3.162 (approximately).
    5. |v| = sqrt( 72 + 22 ) = sqrt( 49 + 4 ) = sqrt( 53 ) = 7.280 (approximately).
    6. |w| = sqrt( 52 + 02 ) = sqrt( 25 ) = 5.
    7. |e| = sqrt( 12 + 02 ) = sqrt( 1 ) = 1.
    8. 3u = [ 3*(-1), 3*3 ] = [ -3, 9 ].
    9. 0v = [ 0*7, 0*2 ] = [ 0, 0 ] = 0.
    10. -3u = [ (-3)*(-1), (-3)*3 ] = [ 3, -9 ].
    11. -v = (-1)v = [ -7, -2 ].
    12. w/2 = [ 5/2, 0/2 ] = [ 2.5, 0 ].
    13. v/0 = [ 7/0, 2/0 ], which is undefined, a divide-by-zero error. (JavaScript will call this [ ∞, ∞ ].)
    14. When you normalize u, you get the vector [ -1, 3 ] / sqrt(10 ) = [ -0.316, 0.949 ] (approx.)
    15. When you normalize w you get the vector [ 5, 0 ] / 5 = [ 1, 0 ]
    16. u + v = [ (-1 + 7), (3 + 2) ] = [ 6, 5 ].
    17. v + w = [ (7 + 5), (2 + 0) ] = [ 12, 2 ].
    18. v + u = [ (7 + (-1)), (2 + 3) ] = [ 6, 5 ].
    19. 3(u + v) = 3 [ 6, 5 ] = [ 18, 15 ].
    20. 3u + 3v = 3 [ -1, 3 ] + 3 [ 7, 2 ] = [ -3, 9 ] + [ 21, 6 ] = [ 18, 15 ].
    21. (2 + 1)u = 3u = [ -3, 9 ].
    22. 2u + 1u = [ 2*(-1), 2*3 ] + [ 1*(-1) + 1*3 ] = [ -2, 6 ] + [ -1, 3 ] = [ -3, 9 ].
    23. 3e + 5f = [ 3*1, 3*0 ] + [ 5*0, 5*1 ] = [ 3, 0 ] + [ 0, 5 ] = [ (3 + 0) + (0 + 5) ] = [ 3, 5 ].
    24. u - v = [ (-1 - 7), (3 - 2) ] = [ -8, 1 ].
    25. v - u = [ (7 - (-1)), (2 - 3) ] = [ 8, -1 ].
    26. u·v = (-1)*7 + 3*2 = -7 + 6 = -1.
    27. v·w = 7*5 + 2*0 = 35 + 0 = 35.
    28. v·u = 7*(-1) + 2*3 = -7 + 6 = -1.
    29. 3(u·v) = 3*(-1) = -3.
    30. (3uv = [ -3, 9 ]·[ 7, 2 ] = (-3)*7 + 9*2 = -21 + 18 = -3.
    31. u·(3v) = [ -1, 3 ]·[ 21, 6 ] = (-1)*21 + 3*6 = -21 + 18 = -3.
    32. u·u = (-1)*(-1) + 3*3 = 10. This is |u|2.
    33. e·u = 1*(-1) + 0*3 = -1 = ux.
    34. f·u = 0*(-1) + 1*3 = 3 = uy.
    35. e·v = 1*7 + 0*2 = 7 = vx.
    36. f·v = 0*7 + 1*2 = 2 = vy.
    37. Let θ be the angle between u and v. Then u·v = |u||v|cos(θ), i.e. cos(θ) = u·v / |u||v| = -1 / (3.162 * 7.280) = -0.04344. Hence θ = acos( -0.04344 ) = 1.614. This is greater than π/2 (approx. 1.571), so the angle is obtuse.
    38. Let θ be the angle between v and w. Then cos(θ) = v·w / |v||w| = 35 / (7.280 * 5) = 0.9615, so θ = 0.2783. As this is less than π/2, the angle is acute.
    39. Let θ be the angle between e and w. Then cos(θ) = e·w / |e||w| = 5 / (1 * 5) = 1, so θ = 0. The angle is zero (extremely acute); the vectors e and w are parallel.
    40. Let θ be the angle between f and w. Then cos(θ) = f·w / |f||w| = 0 / (1 * 5) = 0, so θ = π/2, a right angle; the vectors f and w are perpendicular.
  2. My library is Vector.js. It begins by defining
    var app = app || { };
    app.vector = { };
    		
    1. app.vector.add = function( vec1, vec2 )
      {
          return {
              x: vec1.x + vec2.x,
              y: vec1.y + vec2.y
          };
      };
      			
    2. app.vector.subtract = function( vec1, vec2 )
      {
          return {
              x: vec1.x - vec2.x,
              y: vec1.y - vec2.y
          };
      };
      			
    3. app.vector.scalarMul = function( scalar, vec )
      {
          return {
              x: scalar * vec.x,
              y: scalar * vec.y
          };
      };
      			
    4. app.vector.length = function( vec )
      {
          return Math.sqrt( vec.x * vec.x  +  vec.y * vec.y );
      };
      			
    5. app.vector.normalize = function( vec )
      {
          var len = app.vector.length( vec ),
              invLen = (len > 0) ? (1.0 / len) : 0;
          return app.vector.scalarMul( invLen, vec );
      };
      			
    6. app.vector.distance = function( vec1, vec2 )
      {
          return app.vector.length( app.vector.subtract( vec1, vec2 ) );
      };
      			
    7. app.vector.dot = function( vec1, vec2 )
      {
          return  vec1.x * vec2.x  +  vec1.y * vec2.y;
      };
      			
    8. If v = [ vx, vy ], then perp(v) = [ -vy, vx ]. Their dot product is v·perp(v) = vx(-vy) + vyvx = 0, so v and perp(v) are perpendicular. Graphing a few examples should convince you that perp(v) is v rotated by the angle π/2 counterclockwise.