1 / 31

CSC 308 – Graphics Programming - PowerPoint PPT Presentation

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?.

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

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

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

Matrix Transformations for 2D Graphics

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

Dr. Paige H. Meeker

Computer Science

Presbyterian College, Clinton, SC

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

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

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

x2=x1 * xsf

y2=y1 * ysf

p` = S.p

x2 = x1 + dx

y2 = y1 + dy

p` = T + p

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?

p` = R.p

p` = S.p

p` = T + p

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

• So…

So, why does this help?

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

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

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

• 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”

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

Matrix multiply(Matrix a){

.

.

}

• How can we do this in Java

• Data representation

• Set up

• Multiplication

• Integration with existing class set up

Class Matrix {

Public int rows = 0;

Public int columns = 0;

Public double [] [] m;

.

.

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

• 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()

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.

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:

• 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

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.

• Check conformability

• 3x3 . 3.1 – ok

• 4x2 . 3.1 – not ok

• 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

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

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

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.

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

• …..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.

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

• Instead of performing a whole series of transformations.

• E.g….

• 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

• 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.