1
我想創建與這些參數黑白斜條紋圖案:如何創建不同角度和厚度的斜條紋圖案?
h
:圖像w
的高度:所述圖像的寬度a
:與X條之間角度軸d
:條紋之間的距離t
:條紋的厚度
這可以使用正弦光柵完成,如我發佈here的答案。但它有兩個問題:
- 它不保證保持連接條紋
- 它不保證條紋的厚度保持保持相等
這是一個示例:
diagStripes([h h], pi/4, 20, 0.05);
diagStripes([h h], pi/6, 20, 0.03);
然後我實現了另一個福它受到了DDA line drawing algorithm的啓發。起初,它創建了一個垂直/水平條紋圖案,然後循環移位的行/列創建斜圖案:
function [ out ] = diagStripes(h, w, a, d, t)
% wrap a between pi/4 and -3*pi/4
a = -wrapTo2Pi(a);
if a<-7*pi/4
a=a+2*pi;
elseif a<-3*pi/4
a=a+pi;
end
if a>-(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
它完美地做什麼,我需要的,但我的問題是關於它的性能。所以我發佈了這個問題,看看是否有更好的方法。
你運行探查器(功能'配置文件'),並確定「慢行」? –
@ G.J約有75%的運行時間用於'circshift'行。 – saastn