- a*X+b*Y=c.
Let h and w be the width and height of your buffer:
X = repmat([0:w-1], h, 1)
Y = repmat([0:h-1]', 1, w)
For each pair of points (x1, y1) โ (x2, y2) a, b and c:
a = y2-y1
b = x1-x2
c = x1*y2-x2*y1
Now calculate straigt:
st = a*X+b*Y-c
st(abs(st)>1) = 1
st = 1 - abs(st)
A matrix stis a w * h matrix containing a smooth line passing through the points (x1, y1) and (x2, y2). Now release right from the line, masking the unnecessary parts:
[xs] = sort([x1 x2])
st = st .* [zeros(h, xs(1)) ones(h, xs(2)-xs(1)) zeros(h, w-xs(2))]
[ys] = sort([y1 y2])
st = st .* [zeros(ys(1), w) ; ones(ys(2)-ys(1), w) ; zeros(h-ys(2), w)]
We simply manually drew one line without an explicit loop. There are no guarantees for the effectiveness of the code :-)
Finally: add another dimension to each formula above (on the left as an exercise for the reader).
source
share