Tuesday, May 10, 2011

Moving a ship

Vector graphics refers to representing images by mathematical descriptions of geometric objects, rather than by a collection of pixels on the screen (raster graphics).


Manipulating a Ship


Let’s represent the ship by a triangle, starting with its vertices at (0, 0), (2, 0), (1, 3).









We could represent this as a matrix of vertices, with the understanding (stored as an additional piece of data) that they are connected in order and the last connects to the first.




A matrix of vertices allows us to represent rotation by matrix multiplication, but what about translation? If we wanted to move r units to the right and s units up, we’d have to add:




With a simple modification of the vertex matrix, though, we can represent translation by matrix multiplication using shear transformations (adds a multiple of one coordinate to each of the other coordinates). We just add a dummy coordinate that always holds value 1, and add multiples of that to the x and y coordinates.



Homogeneous Coordinates

In the new scheme, our triangle ship is represented by the matrix




Translation by (r, s) is now the product




Flying Forward


Suppose we want to fly our ship two units in the direction it is currently facing (up). We need to translate by (0, 2):




The new vertices, (0, 2), (2, 2), (1, 5), give us the new ship image:




Turning in Place


We’ll crash into an asteroid if we can’t change direction. Let’s find the images of the columns of I2 under counterclockwise rotation at an angle of .




The first two columns of I3 will do just the same thing, with a zero in the last place. Our extra coordinate will stay the same. Hence to obtain counterclockwise rotation by ' degrees, we multiply by the matrix




For example, for a 90_ turn to the left, we would compute:




That’s not the way ships turn, though! And if we rotate our ship after it drives up 2 units, it’s even worse:




Center of Ship vs. Center of Screen


We want our rotation to be about the center of the ship, say, the point halfway between the front and back ends on the line straight back from the tip. In our beginning position that is (1, 1.5).














One solution is to translate, rotate, and translate back: make the center of the triangle the origin, do the rotation as previously, and then move the center back where it belongs. If we move everything by the amount the center requires, everything will stay in its proper positions.


We multiply on the left by each operation’s matrix in order. Hence the new vertices are given by A1BAM where M is the matrix of homogeneous coordinates, A gives translation by (1,1.5), B rotation by 90_, and A1 translation by (1, 1.5).




The multiplication claims our vertices after rotation should be at (2.5, 0.5), (2.5, 2.5), and (0.5, 1.5). Let’s graph it.




Much better! The corresponding action for our ship after moving forward two units involves translating by (1, 3.5) and back with the rotation in between. The vertices obtained are (2.5, 2.5), (2.5, 4.5), and (0.5, 3.5) (the previous vertices with y + 2).



http://www.math.dartmouth.edu/~rweber/teaching/VectorSlides.pdf








7 comments:

  1. nice post dina it's very interestingow to know this i didnt have any idea befor about it

    ReplyDelete
  2. very smart example dina more specificly about "turning in place" without changing the direction we can get image of the columns

    ReplyDelete
  3. nice post, interesting informations and clear pictures but isn't so long ?

    ReplyDelete
  4. very interesting post dina,it seems that matrices is involved in every thing we do in our lives. thank you for this info's.

    ReplyDelete
  5. Dina, a great post indeed!!!!!! after all your frustration on posting, you finally did it and you did a great job!
    I like it when they said that the ship must change directions otherwise it might hit an asteroid! :)
    Thanks Dina!

    ReplyDelete
  6. I never thought that matrices can move a ship!!
    nice work Dina :)

    ReplyDelete