1 / 31

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

Télécharger la présentation

CSC 308 – Graphics Programming

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

  5. Scaling x2=x1 * xsf y2=y1 * ysf p` = S.p

  6. Translation x2 = x1 + dx y2 = y1 + dy p` = T + p

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

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

  9. Solution – cheat - homogenous coordinates Add an extra coordinate (!) So, why does this help? (We can now convert the necessary addition to multiplication!)

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

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

  12. 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”

  13. Matrices – why? (1) Because we now have to write only one method to perform all possible transformations : Matrix multiply(Matrix a){ . . }

  14. Implementing Matrices • How can we do this in Java • Data representation • Set up • Multiplication • Integration with existing class set up

  15. Data Class Matrix { Public int rows = 0; Public int columns = 0; Public double [] [] m; . .

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

  17. 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()

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

  19. 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:

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

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

  22. Multiplication • Check conformability • 3x3 . 3.1 – ok • 4x2 . 3.1 – not ok

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

  24. 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]); }

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

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

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

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

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

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

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

More Related