The Care and Usage of Vectors

Tom Kelliher, CS 320

Mar. 2, 2011

Administrivia

Announcements

Assignment

Carefully read collision.c and Sections 4.1 and B.1-B.5.

From Last Time

Experimentation with polygon.c.

Outline

  1. Finish-up polygon.c.

  2. Vectors. Data structures and code.

  3. Discussion of collision.c.

Coming Up

Continued study of collision.c.

polygon.c

20 minutes to tie up loose ends. Turn in to kelliher[at]goucher.edu by beginning of next class.

Vectors

Operating systems is the application of data structures and algorithms. Computer graphics is the application of data structures, algorithms, trigonometry, linear algebra, Calculus, and differential equations.

  1. Scalars.

  2. Correspondence between points and vectors.

  3. Forming a vector from two points.

  4. Vector length: $\vert u\vert$.

  5. Scaling vectors: $\alpha u$.

  6. Adding vectors.

  7. Forming a new point from a point and a vector.

  8. Normalizing a vector: $\hat{u} = \frac{1}{\vert u\vert} u$.

    Not the same as a normal vector.

  9. Dot product properties:
    1. $\vert u\vert^2 = u \cdot u$.

    2. $\cos \theta = \frac{u \cdot v}{\vert u\vert \vert v\vert}$, where $\theta$ is the angle between $u$ and $v$.

    3. The projection of $u$ onto $v$ is $\vert u\vert \cos \theta = \frac{u \cdot
v}{\vert v\vert}$

      Note that this is a scalar. What if I need a vector?

Data Structures and Code

This is all 2-D. It should be extended to 3-D.

  1. Basic data structure:
    typedef struct Vector2
    {
       GLdouble x, y;
    } Vector2;
    

  2. Vector length:
    double distanceSquared(double x, double y)
    {
       return x * x + y * y;
    }
    
    double vectorLength(Vector2 v)
    {
       return sqrt(distanceSquared(v.x, v.y));
    }
    
    Square root and division are expensive. Avoid where possible.

  3. Scalar, vector product:
    Vector2 scalarProduct(double s, Vector2 v)
    {
       v.x *= s;
       v.y *= s;
    
       return v;
    }
    

  4. Dot product:
    double dotProduct(Vector2 a, Vector2 b)
    {
       return a.x * b.x + a.y * b.y;
    }
    

  5. Vector normalization:
    Vector2 normalize(Vector2 v)
    {
       double length = vectorLength(v);
    
       v.x /= length;
       v.y /= length;
    
       return v;
    }
    

  6. Vector sum:
    Vector2 vectorSum(Vector2 a, Vector2 b)
    {
       a.x += b.x;
       a.y += b.y;
    
       return a;
    }
    

collision.c

  1. main(): Note use of srand().

  2. init(): Calls initBalls(), little else.

  3. initBalls:
    1. First off, we need a better object manager. More precisely, we need an object manager.

    2. The ``object'' definitions:
      typedef struct Color
      {
         GLdouble r, g, b;
      } Color;
      
      typedef struct Ball
      {
         Vector2 position; /* Ok, so it's not really a vector. Sue me. */
         Vector2 velocity;
         GLdouble radius;
         GLdouble mass;
         Color color;
         GLuint handle;
      } Ball;
      
      These should be proper classes.

    3. Note setting of basic object attributes. These should be kept in a file and handled by the object manager.

    4. Note compilation of object rendering primitives via individual display lists.

  4. placeBalls():
    1. Placement of the first ball along unit circle.

      Velocity computation. Target: origin. Scaling velocity.

      Translating position to circle of radius 40.

    2. Avoiding initial collision: Constrained placement of second ball; $\pi / 4$ or more away.

    3. Options: Aiming second ball at a point other than the origin. Computing and normalizing the velocity vector.

      Making the second ball stationary, at an arbitrary position.



Thomas P. Kelliher 2011-03-02
Tom Kelliher