【发布时间】:2017-10-04 09:55:53
【问题描述】:
我想用这些参数创建黑白斜条纹图案:
-
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
它完美地满足了我的需求,但我的问题在于它的性能。所以我发布了这个问题,看看是否有更好的方法。
【问题讨论】:
-
你有没有运行分析器(函数'profile')并识别“慢行”?
-
@G.J 大约 75% 的运行时间花费在
circshift行中。