title: How to create diagonal stripes pattern of different angles and thicknesses?
body: <p>I want to create black and white diagonal stripes pattern with these parameters:</p>
  
  <ul>
  <li><code>h</code>: height of the image</li>
  <li><code>w</code>: width of the image</li>
  <li><code>a</code>: angle between strips and the X axis</li>
  <li><code>d</code>: distance between stripes</li>
  <li><code>t</code>: thickness of stripes</li>
  </ul>
  
  <p>This can be done using sinusoidal gratings like the answer I posted <a href="https://stackoverflow.com/a/43424340/2093077">here</a>. But it has two problems:</p>
  
  <ol>
  <li>It doesn't guarantee the stripes to remain connected</li>
  <li>It doesn't guarantee the thickness of stripes to remain remain equal</li>
  </ol>
  
  <p>This is a sample:</p>
  
  <pre><code>diagStripes( [h h], pi/4, 20, 0.05);
  diagStripes( [h h], pi/6, 20, 0.03);
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/HGWs6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/HGWs6.png" alt="stripes"></a></p>
  
  <p>Then I implemented another function which was inspired by <a href="https://en.wikipedia.org/wiki/Digital_differential_analyzer_(graphics_algorithm)" rel="nofollow noreferrer">DDA line drawing algorithm</a>. At first it creates a vertical/horizontal stripes pattern, then circularly shifts its rows/columns to create the diagonal pattern:</p>
  
  <pre><code>function [ out ] = diagStripes( h, w, a, d, t )
  % wrap a between pi/4 and -3*pi/4
  a = -wrapTo2Pi(a);
  if a&lt;-7*pi/4
      a=a+2*pi;
  elseif a&lt;-3*pi/4
      a=a+pi;
  end
  
  if a&gt;-(pi/4) % the acute angle between stripes and x axis is greater than pi/4
      dy = round(abs(d/cos(a))); % vertical distance between stripes
      ty = max(1, round(abs(t/cos(a)))); % vertical thickness of stripes
      n = ceil(h/dy); % maximum number of stripes
      out = repmat([false(ty, 1); true(dy-ty, 1)], n, w); % create horizontal stripes
      x = 1:w;
      y = round(tan(a)*x); % calculate shift amount of each column
      for ii=x
          out(:, ii) = circshift(out(:, ii), y(ii), 1);
      end
  else % the acute angle between stripes and x axis is less than pi/4
      dx = round(abs(d/sin(a))); % horizontal distance between stripes
      tx = max(1, round(abs(t/sin(a)))); % horizontal thickness of stripes
      n = ceil(w/dx); % maximum number of stripes
      out = repmat([false(1, tx), true(1, dx-tx)], h, n); % create vertical stripes
      y = 1:h;
      x = round(cot(a)*y); % calculate shift amount of each row
      for ii=y
          out(ii, :) = circshift(out(ii, :), x(ii), 2);
      end
  end
  out = out(1:h, 1:w); % crop the result, out may have more rows or columns than desired values (n*(dx or dy))
  end
  </code></pre>
  
  <p>It perfectly does what I need, but my problem is about its performance. So I posted this question to see if there is a better approach.</p>