<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246026</link>
    <title>MATLAB Central Newsreader - How to rotate a 2D function</title>
    <description>Feed for thread: How to rotate a 2D function</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 06 Mar 2009 08:28:02 -0500</pubDate>
      <title>How to rotate a 2D function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246026#632957</link>
      <author>kees de Kapper</author>
      <description>Dear all,&lt;br&gt;
&lt;br&gt;
I have, probably, a stupid question with hopefully an easy answer.&lt;br&gt;
&lt;br&gt;
I have got a 2D function, say a simple Gaussian: &lt;br&gt;
&lt;br&gt;
x = 1:1:512;&lt;br&gt;
y = 1:1:512;&lt;br&gt;
z = (exp(-((x-256)/20).^2)'*(exp(-((y-256)/40).^2);&lt;br&gt;
%imagesc(z); colormap(gray); &lt;br&gt;
&lt;br&gt;
This gives a Gaussian with different length at the axes.&lt;br&gt;
&lt;br&gt;
Now I want to rotate this function around its center. So, I thought to transform the coordinates, thus:&lt;br&gt;
&lt;br&gt;
x0 = 256; y0 = 256;&lt;br&gt;
x2 = cos(Theta)*(x-x0)-sin(Theta)*(y-y0)+x0;&lt;br&gt;
y2 = sin(Theta)*(x-x0)+cos(Theta)*(x-x0)+y0;&lt;br&gt;
&lt;br&gt;
Then apply to the function:&lt;br&gt;
z2 = (exp(-((x2-256)/20).^2)'*(exp(-((y2-256)/40).^2);&lt;br&gt;
%imagesc(z); colormap(gray); &lt;br&gt;
&lt;br&gt;
However, this will not do the job. For example, if Theta = pi/2 then x2 = y and y2 = x, which represents the same values. Therefore no rotation is visible.&lt;br&gt;
&lt;br&gt;
What goes wrong?&lt;br&gt;
For computational speed and discretization errors I want to avoid &quot;imrotate&quot;.&lt;br&gt;
&lt;br&gt;
Thanks for your help.&lt;br&gt;
&lt;br&gt;
Kees</description>
    </item>
    <item>
      <pubDate>Fri, 06 Mar 2009 09:29:01 -0500</pubDate>
      <title>Re: How to rotate a 2D function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246026#632970</link>
      <author>nor ki</author>
      <description>&quot;kees de Kapper&quot; &amp;lt;kees_de_kapper@hotmail.com&amp;gt; wrote in message &amp;lt;goqmqi$7m5$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Dear all,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have, probably, a stupid question with hopefully an easy answer.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I have got a 2D function, say a simple Gaussian: &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x = 1:1:512;&lt;br&gt;
&amp;gt; y = 1:1:512;&lt;br&gt;
&amp;gt; z = (exp(-((x-256)/20).^2)'*(exp(-((y-256)/40).^2);&lt;br&gt;
&amp;gt; %imagesc(z); colormap(gray); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; This gives a Gaussian with different length at the axes.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Now I want to rotate this function around its center. So, I thought to transform the coordinates, thus:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; x0 = 256; y0 = 256;&lt;br&gt;
&amp;gt; x2 = cos(Theta)*(x-x0)-sin(Theta)*(y-y0)+x0;&lt;br&gt;
&amp;gt; y2 = sin(Theta)*(x-x0)+cos(Theta)*(x-x0)+y0;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Then apply to the function:&lt;br&gt;
&amp;gt; z2 = (exp(-((x2-256)/20).^2)'*(exp(-((y2-256)/40).^2);&lt;br&gt;
&amp;gt; %imagesc(z); colormap(gray); &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, this will not do the job. For example, if Theta = pi/2 then x2 = y and y2 = x, which represents the same values. Therefore no rotation is visible.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; What goes wrong?&lt;br&gt;
&amp;gt; For computational speed and discretization errors I want to avoid &quot;imrotate&quot;.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks for your help.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Kees&lt;br&gt;
&lt;br&gt;
Hi Kees,&lt;br&gt;
&lt;br&gt;
you make the 2D gaussian by multiplying the profile in y by the profile in x, this works only if the axes are parallel to your coordinate axes.&lt;br&gt;
When you rotate your gaussian you have to calculate all positions as no decomposition is possible.&lt;br&gt;
try&lt;br&gt;
&lt;br&gt;
Theta = pi/4&lt;br&gt;
x0 = 256; y0 = 256;&lt;br&gt;
[x,y] = meshgrid(1:512,1:512);&lt;br&gt;
x2 = cos(Theta)*(x-x0)-sin(Theta)*(y-y0)+x0;&lt;br&gt;
y2 = sin(Theta)*(x-x0)+cos(Theta)*(x-x0)+y0;&lt;br&gt;
z2 = (exp(-((x2-256)/20).^2).*(exp(-((y2-256)/40).^2)));&lt;br&gt;
imagesc(z2); colormap(gray);&lt;br&gt;
&lt;br&gt;
hth &lt;br&gt;
kinor</description>
    </item>
    <item>
      <pubDate>Fri, 06 Mar 2009 10:10:03 -0500</pubDate>
      <title>Re: How to rotate a 2D function</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/246026#632979</link>
      <author>kees de Kapper</author>
      <description>Great! this works.&lt;br&gt;
&lt;br&gt;
I've tried the solution you gave me earlier, but due to a mistypo it didn't work. You surely helped me to find the bug.&lt;br&gt;
&lt;br&gt;
thank you very much.</description>
    </item>
  </channel>
</rss>

