Csc 308 graphics programming
Download
1 / 31

CSC 308 – Graphics Programming - PowerPoint PPT Presentation


  • 97 Views
  • Uploaded on

CSC 308 – Graphics Programming. Matrix Transformations for 2D Graphics Information modified from Ferguson’s “Computer Graphics via Java”. Dr. Paige H. Meeker Computer Science Presbyterian College, Clinton, SC. What are we doing today?.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' CSC 308 – Graphics Programming' - venedict-diego


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Csc 308 graphics programming

CSC 308 – Graphics Programming

Matrix Transformations for 2D Graphics

Information modified from Ferguson’s “Computer Graphics via Java”

Dr. Paige H. Meeker

Computer Science

Presbyterian College, Clinton, SC


What are we doing today
What are we doing today?

It is useful, for reasons to become obvious later, to be able to represent transformations as matrices. So, how does that work, exactly…


Matrix forms
Matrix Forms

The equations that represent transformations can be written as matrices – for example, rotation:

x2 = x1 cos q – y1 sin q

y2 = x1 sin q + y1 cos q


Rotation
Rotation

If p and p` represent the point P before and after rotation and R the rotation matrix, we can write the equation above more succinctly:

p` = R.p


Scaling
Scaling

x2=x1 * xsf

y2=y1 * ysf

p` = S.p


Translation
Translation

x2 = x1 + dx

y2 = y1 + dy

p` = T + p


Useful
Useful…

A useful consequence of using matrices is that transformations that are combinations of transformations can be represented by multiplying the matrices together. However…


Back to elementary school which one is different
Back to elementary school – Which one is different?

p` = R.p

p` = S.p

p` = T + p

  • Multiplying vs adding matrices

  • It would be nice (read ‘easier to code’) if they were all the same.

  • So…


Solution cheat homogenous coordinates
Solution – cheat - homogenous coordinates

Add an extra coordinate (!)

So, why does this help?

(We can now convert the necessary addition to multiplication!)


Rotation1
Rotation

Note how the top left of the matrix is the same as non-homogenous version


Scaling1
Scaling

Note how the top left of the matrix is the same as non-homogenous version


Translation1
Translation

  • How does this work? When multiplying matrices your adding as part of the operation anyway. Perform an addition by:

  • “1 times x add 1 times dx”

  • “1 time y add 1 times dy”


Matrices why 1
Matrices – why? (1)

Because we now have to write only one method to perform all possible transformations :

Matrix multiply(Matrix a){

.

.

}


Implementing matrices
Implementing Matrices

  • How can we do this in Java

    • Data representation

    • Set up

    • Multiplication

    • Integration with existing class set up


Data

Class Matrix {

Public int rows = 0;

Public int columns = 0;

Public double [] [] m;

.

.


Set up constructor
Set up - Constructor

Matrix(int in_rows, int in_columns, double val){

rows=in_rows;

columns=in_columns;

m = new double [rows][columns];

for (int i = 0; i < rows; i++ ) {

for (int j = 0; j < columns; j++ ) {

m[i][j] = val;

}//end for j

}//end for i

}//constructor


Other necessary methods for matrix
Other necessary methods for Matrix:

  • public int getRows()

  • public int getColumns()

  • public double getElement(int i, int j)

  • public double setElement(int i, int j, double value)

  • public Matrix mult(Matrix b)

  • public void transform(Matrix b)

  • public String toString()


Other necessary methods for matrix1
Other necessary methods for Matrix:

What’s the difference between the mult method and the transform method? mult will return a new Matrix object that holds the result of the multiplication. transform will return nothing, but the state of the original matrix object has been changed.


Multiplication
Multiplication

We can take the product of two matrices (A and B) only if the number of columns of A equals the number of rows of B (so that we can multiply the rows of A by the columns of B). The product AB is obtained as follows:


Multiplication1
Multiplication

  • To obtain the 1,1 entry of AB, multiply Row 1 of A by Column 1 of B

  • To obtain the 1,2 entry of AB, multiply Row 1 of A by Column 2 of B

  • To obtain the 1,3 entry of AB, multiply Row 1 of A by Column 3 of B

  • To obtain the 2,1 entry of AB, multiply Row 2 of A by Column 1 of B

  • To obtain the 2,2 entry of AB, multiply Row 2 of A by Column 2 of B

  • To obtain the i,j entry of AB, multiply Row i of A by Column j of B


Multiplication2
Multiplication

So, the product AB has as many rows of A and as many columns as B

Therefore, to code matrix multiplication, you must check to ensure you have the correct number of rows and columns before doing the multiplication.


Multiplication3
Multiplication

  • Check conformability

  • 3x3 . 3.1 – ok

  • 4x2 . 3.1 – not ok


Fit in with existing classes 1
Fit in with existing classes (1)

  • Make Point2d inherit Matrix

    • i.e. a point is a kind of matrix

  • Set up a Transformation class which inherits from Matrix

    • i.e. a transformation is a kind of matrix

      • (we can then multiply points by transformations which is what we’re after)

  • Adjust Line2d and Shape2d to match


  • Make point2d inherit matrix
    Make Point2d inherit Matrix

    Class Point2d extends Matrix {

    public Point2d(double in_x, double in_y) {

    super(3,1,1);

    m[0][0]=in_x;

    m[1][0]=in_y;

    }// end constructor

    public double x() {

    return (m[0][0]);

    }

    public double y() {

    return (m[1][0]);

    }


    Set up a transformation class
    Set up a Transformation class

    class Transformation2d extends Matrix{

    Transformation2d(){

    super(3,3,0);

    m[0][0]=1;

    m[1][1]=1;

    m[2][2]=1;

    }//constructor

    public void translate(double x, double y) {

    m[0][2]=x;

    m[1][2]=y;

    }//translate

    // Similar methods for rotate and scale needed


    Adjust line2d
    Adjust Line2d

    public void transform(Transformation2d trans) {

    src.transform(trans);

    dest.transform(trans);

    }//end transform

    Note that due to clever design earlier on, this is the only change.


    Adjust shape2d
    Adjust Shape2d

    public void transform(Transformation2d trans) {

    for (int i=0; i < numberOfLines; i = i+1) {

    ((Line2d)lines.get(i)).transform(trans);

    }//end for i

    localOrigin.transform(trans);

    }//end transform


    Combining transformations
    Combining transformations

    • …..by matrix multiplication

    • When two matrices that represent different transformations are multiplied together, the resulting matrix represents the combined transformation – thus the power of matrix multiplication – a single matrix can store the result of a sequence of transformations.


    Matrices why 2
    Matrices – why? (2)

    • We can now set up a complex transformation, store it, and use it whenever.

    • Instead of performing a whole series of transformations.

    • E.g….


    Undoing a transformation
    Undoing a transformation

    • Find a reverse transformation by finding the inverse of a matrix

    • (Create an inverse() method for the matrix class)

    • Create a transformation to “spin” an object around its local origin


    Homework
    Homework…

    • Create a Matrix class and a Transformation class

    • Modify the Point2d, Line2d, Shape2d and Drawing2d to use the homogenous coordinate matrix transformations.

    • Express a local rotation transformation as a matrix.


    ad