<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Silverlight &#124; WPF &#124; Microsoft.Net &#187; RenderTransform</title>
	<atom:link href="http://joel.neubeck.net/tag/rendertransform/feed/" rel="self" type="application/rss+xml" />
	<link>http://joel.neubeck.net</link>
	<description>Simplifing structure without changing results</description>
	<lastBuildDate>Fri, 01 Apr 2011 21:34:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Transformation Matrix in Silverlight 2</title>
		<link>http://joel.neubeck.net/2008/09/transformation-matrix-in-silverlight-2/</link>
		<comments>http://joel.neubeck.net/2008/09/transformation-matrix-in-silverlight-2/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 05:53:58 +0000</pubDate>
		<dc:creator>joel</dc:creator>
				<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[matrix]]></category>
		<category><![CDATA[MatrixTransform]]></category>
		<category><![CDATA[RenderTransform]]></category>
		<category><![CDATA[Rotate]]></category>
		<category><![CDATA[Scale]]></category>
		<category><![CDATA[Skew]]></category>
		<category><![CDATA[Translate]]></category>

		<guid isPermaLink="false">http://joel.neubeck.net/2008/09/transformation-matrix-in-silverlight-2/</guid>
		<description><![CDATA[In my exploration of Silverlight I have looked at a ton of code samples, and not once have I seen an example that chooses MatrixTransform as the class used to render a transformation.&#160; We all should be familiar with the four transform classes Silverlight provides for 2-D transformations.&#160; Each allows us to manipulate the underlying [...]]]></description>
			<content:encoded><![CDATA[<p>In my exploration of Silverlight I have looked at a ton of code samples, and not once have I seen an example that chooses MatrixTransform as the class used to render a transformation.&#160; We all should be familiar with the four transform classes Silverlight provides for 2-D transformations.&#160; Each allows us to manipulate the underlying matrix without having to understand how the matrix is structured.&#160; All of them can be grouped into a single TransformGroup to apply multiple transformations to a single object.</p>
<ul>
<li>TransformGroup
<ul>
<li>RotateTransform – Rotates by an angle </li>
<li>ScaleTransform – Scales by a a given X and Y </li>
<li>SkewTransform – Skews an element by an X and Y angle </li>
<li>TranslateTransform – Moves and object by a specified X and Y </li>
</ul>
</li>
</ul>
<p dir="ltr">As an alternative, you can use the MatrixTransform as a way to manipulate a matrix directly.</p>
<h2>What is a Matrix</h2>
<p>In Silverlight a matrix is a 3&#215;3 array of numbers used to define <strong>affine</strong> transformations.&#160; Here is a great definition of a Affine transformation taken from an excellent tutorial on using matrices in Flash.</p>
<p> <a href="http://www.senocular.com/flash/tutorials/transformmatrix/" target="blank"><br />
<blockquote>
<p>Affine transformations are transformations that preserve collinearity and relative distancing in a transformed coordinate space. This means points on a line will remain in a line after an affine transformation is applied to the coordinate space in which that line exists.</p>
</blockquote>
<p> </a>
<p>Silverlight uses row-major matrices, such that vectors are expressed as rows and not column vectors.&#160; Here is what the matrix structure looks like.</p>
<table cellspacing="0" cellpadding="2" width="230" border="1">
<tbody>
<tr>
<td valign="top" width="91">M11 (1.0)</td>
<td valign="top" width="99">M12 (0.0)</td>
<td valign="top" width="38">0</td>
</tr>
<tr>
<td valign="top" width="91">M21 (0.0)</td>
<td valign="top" width="99">M22 (1.0)</td>
<td valign="top" width="38">0</td>
</tr>
<tr>
<td valign="top" width="91">Offset X (0.0)</td>
<td valign="top" width="99">Offset Y (0.0)</td>
<td valign="top" width="38">1</td>
</tr>
</tbody>
</table>
<p>Since Silverlight only supports affine transforms, the values in the right column are always 0,0,1.&#160; Below is an example of a Silverlight UserControl that demonstrates how manipulating each of the matrices values, will transform a rectangle.</p>
<p> <iframe src="/wp-content/uploads/2008/09/Matrix/" width="500" height="200"></iframe>
<p>In my demonstration, using the appropriate TextBox&#160; you can rotate, scale, skew, and move the rectangle. For example, if you set M12 equal to 0.5 and M21 equal to -0.5 you will rotate the rectangle 45 deg.&#160; If you set M11 and M22 equal to 1.5 you will scale the rectangle proportionately to approximately one and half times its original size. </p>
<p>Procedurally, these values can be set by defining a Matrix struct and setting the appropriate property (M11, M12, M21, M22, OffsetX, OffsetY).&#160; The matrix can be applied to an objects RenderTransform by simply setting the MatrixTransform.Matrix property.</p>
</p>

<div class="wp_syntax"><div class="code"><pre class="c-sharp" style="font-family:monospace;">Matrix martix = new Matrix(1.5,0.5,-0.5,1.5,0,0);
MatrixTransform transform = new MatrixTransform();
transform.Matrix = matrix;
rectangle.RenderTransform = transform;</pre></div></div>

<p>Later in the week I will post an example that uses a MatrixTransform to demonstrate an animation that reacts to mouse movement.&#160; Nothing crazy, but a nice example of where a Matrix can create an interesting effect with very little code.</p>
<p>Code: <a onclick="javascript: pageTracker._trackPageview('/code/Matrix.zip');" href="/wp-content/uploads/2008/09/Matrix/MatrixExample.zip">MatrixExample.zip</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joel.neubeck.net/2008/09/transformation-matrix-in-silverlight-2/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

