September 29, 2015

This notebook demonstrates using homogeneous coordinates to perform linear transformations and translations in (ℝ^2)

(* This function plots two lists of points in R^2 and draws connecting lines.  
The first list is labeled as the list of original points, and the second list is labeled as the list of transformed points. *)
 
pointsPlot[origPts_,transformedPts_] := Module[ {prange},
	
	prange = Max[ {Abs[origPts],Abs[transformedPts]}]+0.5;
	
ListLinePlot[{origPts, transformedPts}, AspectRatio->1,  PlotRange->{{-prange,prange},{-prange,prange}}, PlotStyle->{Blue, Red}, PlotMarkers -> "●",PlotLegends->{"Original", "Transformed"}]
]


(* This function takes a list of points in R^2 and returns a list of points in R^3 where '1' has been added as the third component. 
This effectively converts a list of points in R^2 into the corresponding list in homogeneous coordinates in R^3. *)

homogeneous[L_] := Table[ Append[L[[i]],1], {i, 1,Length[L]}]


(* This function takes a list of points in R^3 and returns a list of points in R^2 where the third component has been dropped. 
This effectively converts a list of points in homogeneous coordinates in R^3 into the corresponding list of points in R^2. 
 If you want to try to unravel this procedure, I'm using that (((BC))^T)=(C^T)(B^T)*)

dehomogeneous[L_] :=L.{{1,0},{0,1},{0,0}}

An example of translating a polygon by (3,2)

These are the same polygons we’ve used before.

Square1 = { {0,0}, {1,0}, {1,1}, {0,1}, {0,0}};
Square2 = { {-0.5,-0.5},{0.5,-0.5},{0.5,0.5},{-0.5,0.5},{-0.5,-0.5}};
Square3 = { {1,1},{2,1},{2,2},{1,2},{1,1}};
Triangle1 = { {0,0},{1,0},{Cos[Pi/3],Sin[Pi/3]},{0,0}};
Triangle2 = { {1,0},{2,0},{Cos[Pi/3]+1,Sin[Pi/3]},{1,0}};
Rm={ {Cos[2Pi/5],-Sin[2Pi/5]},{Sin[2Pi/5],Cos[2Pi/5]}};Pentagon1 = { {0,1}, Rm.{0,1}, Rm.Rm.{0,1},Rm.Rm.Rm.{0,1},Rm.Rm.Rm.Rm.{0,1},{0,1}};

Let’s see what happens to Square2. 
We use the homogeneous[ ] function to convert the points to homogeneous coordinates.

hSquare2 =homogeneous[Square2]

A is the matrix that will translate by 3 units in the positive x direction and by 2 units in the positive y direction.

A = { {1, 0, 3}, {0,1,2},{0,0,1}}
A//MatrixForm

We would like to multiply A by each point.

A.hSquare2

What happened here?

hSquare2 //MatrixForm

Oh!  The points in hSquare2 are in the rows, not the columns!
This means that we need to multiply A by the transpose of hSquare2

A.Transpose[hSquare2] //MatrixForm

The pointsPlot function wants a list of points, but A.Transpose[hSquare2] has the points in the columns.
Therefore, we need to take the transpose of this product.

trans = Transpose[A.Transpose[hSquare2]]

Now, let’s move back to R^2 from our homogeneous coordinates

transSquare2 = dehomogeneous[trans]

And we’re finally ready to plot the output

pointsPlot[Square2,transSquare2]

Or, we could do this entire example in two lines!

A = { {1, 0, 3}, {0,1,2},{0,0,1}}

pointsPlot[Square2,dehomogeneous[Transpose[A.Transpose[homogeneous[Square2]]]]]

We can remove one of the Transpose[ ] commands in the second line by using that (((BC))^T)=(C^T)(B^T)

A = { {1, 0, 3}, {0,1,2},{0,0,1}}

pointsPlot[Square2,dehomogeneous[homogeneous[Square2].Transpose[A] ]]

This is another set of points that you might find interesting to use

bart = {{3,2},{4,3},{5,4},{7,4},{8,3},{8,2},{9,2.5},{10,2},{11,3},{12,4},{14,4},{15,3},{16,2},{15.5`,9},{16,16},{14,15},{14,17}, {12,16},{11,18},{9,17},{8,18},{6,16},{5,18},{4,16},{3,17},{2,15},{1,16},{2.5,9},{3,2}}