JMU CS488 - Computer Graphics Applications
Help Policies Solutions Study-Aids Syllabus Tools
Sample 2D Graphics Questions


  1. Answer each of the following:
    1. Convert the point \([5.0 \; 2.0 \; 1.0]^T\) from homogeneous coordinates to Cartesian coordinates.
    2. Convert the point \([4.0 \; 2.0 \; 0.5]^T\) from homogeneous coordinates to Cartesian coordinates.
    3. Convert the point \([-1.0 \; -7.0]^T\) from Cartesian coordinates to homogeneous coordinates.
  2. Given a line defined by the two Cartesian points \(\bs{a} = [1 \; 2]^T\), \(\bs{b} = [2 \; 4]^T\):
    1. Find the direction vector that is directed from \(\bs{a}\) to \(\bs{b}\).
    2. Use the direction vector from the previous question to find a direction vector that is perpendicular to the line at \(\bs{a}\).
  3. Consider the following transformation matrix:

    \(\bs{T} = \left[ \begin{array}{r r} 0 & -1 \\ 1 & 0\end{array}\right] \)

    1. Apply this transformation to the unit square below. Show all of your work.

      \(\bs{S} = \left[ \begin{array}{r r r r} 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1\end{array}\right] \)

    2. What kind of transformation is this?
  4. Concatenate the following two 2D (Cartesian) transformation matrices, assuming that the scaling, \(\bs{S}\) occurs before the rotation, \(\bs{R}\).

    \( \bs{R} = \left[ \begin{array}{r r} \cos \theta & -\sin \theta \\ \sin \theta & \cos \theta \end{array}\right] \)

    and

    \( \bs{S} = \left[ \begin{array}{r r} \alpha & 0 \\ 0 & \beta \end{array}\right] \)
  5. Answer both of the following:
    1. How many multiplications and additions would be required to scale and rotate 100 2D (Cartesian) points if the two transforms were applied sequentially?
    2. How many multiplications and additions would be required to scale and rotate 100 2D (Cartesian) points using the concatenated transform above?
  6. Given the following drawLine() method in the Rasterizer2D class (that uses the classes/structs from the programming assignments):
    void Rasterizer2D::drawLine(const Matrix<2,1>& p, const Matrix<2,1>& q,
                                const Color& color)
    {
       double    alpha, m;
    
       if (q.get(0,0) == p.get(0,0))
           m = INFINITY;
       else
          m = (q.get(1,0)-p.get(1,0))/(q.get(0,0)-p.get(0,0));
    
       if ((m >= -1.0) && (m <= 1.0))
       {
          double   y;          
          int      xEnd, xStart;
    
          if (p.get(0,0) <= q.get(0,0))
          {
             xStart = (int)round(p.get(0,0)); 
             xEnd   = (int)round(q.get(0,0)); 
          }
          else
          {
             xStart = (int)round(q.get(0,0)); 
             xEnd   = (int)round(p.get(0,0)); 
          }
          
          
          for (int x=xStart; x<=xEnd; x++)
          {
             if (q.get(0,0) == p.get(0,0))
             {
                y = p.get(1,0);                
             }
             else
             {
                alpha = (x - p.get(0,0))/(q.get(0,0) - p.get(0,0));
                y = p.get(1,0) + alpha * (q.get(1,0) - p.get(1,0));          
             }
             
             fb->setPixel(x, (int)round(y), color);          
          }
       }
       else
       {
          double   x;          
          int      yEnd, yStart;
           
          if (p.get(1,0) <= q.get(1,0))
          {
             yStart = (int)round(p.get(1,0)); 
             yEnd   = (int)round(q.get(1,0)); 
          }
          else
          {
             yStart = (int)round(q.get(1,0)); 
             yEnd   = (int)round(p.get(1,0)); 
          }
              
        
          for (int y=yStart; y<=yEnd; y++)
          {
             if (q.get(1,0) == p.get(1,0))
             {
                x = p.get(0,0);                
             }
             else
             {
                alpha = (y - p.get(1,0))/(q.get(1,0) - p.get(1,0));
                x = p.get(0,0) + alpha * (q.get(0,0) - p.get(0,0));          
             }
                 
             fb->setPixel((int)round(x), y, color);          
          }
       }
    }
        

    show (in the grid below) what will be drawn if this method is called as follows:

        Color       red = {255,0,0};
        Matrix<2,1> p = { 0, 0};
        Matrix<2,1> q = {10, 3};
        rasterizer->drawLine(red, p, q);
        

    You must assume that the lower-left pixel is the origin [i.e., (0,0)] and that the coordinates increase from left-to-right and bottom-to-top.

    grid.gif
  7. Write a function named onLine() that is passed a double containing the slope of the line, a double containing the vertical intercept of the line, and a Matrix<2,1> containing a point. This function must return true of the point is on the line and false otherwise.
  8. Write a function named sameSide() that is passed two points defining a line and two test points. The method must return true if the two points are on the same side of the line and false otherwise.
  9. Using the sameSide() function above, write a function that determines if a given point is inside a convex polygon.
  10. Using the determinant to calculate the signed areas, determine whether the point \(\bs{p}=[2 \; 3]^T\) is in the triangle formed by the points \(\bs{s}=[2 \; 2]^T\), \(\bs{t}=[5 \; 1]^T\) and \(\bs{r}=[3 \; 5]^T\). Show all of your work.

Copyright 2020