%input:
%k:  number of rows, cols in w
%theta:  orientation of normal line from pixel center to edge of line
%rho:  distance from pixel center to line
%width:  line width in real space
%
%output:
%w:  graphics image of line

function [w] = synthwin(k, theta, rho, width)

mid = k / 2;
theta = posori(theta);
rotationFactor = 0;
if theta == pi %can't take integral of vertical lines!
    rotationFactor = 1;
    theta = pi / 2;
elseif theta == 2*pi || theta == 0 
    rotationFactor = 3;
    theta = pi / 2;
end
[closeX, closeY] = pol2cart(theta, rho);
closeX = mid + closeX;
closeY = mid + closeY;
[farX, farY] = pol2cart(theta, rho + width);
farX = mid + farX;
farY = mid + farY;
[x, y] = pol2cart(posori(theta + pi / 2), 1);
slope = y / x;
closeB = closeY - closeX * slope;
farB = farY - farX * slope;

w = zeros(k, k);
for c = 1:k
    syms x;
    farColArea = double(int(slope * x + farB, c - 1, c));
    closeColArea = double(int(slope * x + closeB, c - 1, c));
    farSum = 0;
    closeSum = 0;
    for r = 0:k
        baseArea = double(int(k-r, x, c - 1, c));
        farCurArea = farColArea - baseArea - farSum;
        closeCurArea = closeColArea - baseArea - closeSum;
        if farCurArea > 0
            if r > 0
                w(r, c) = farCurArea;
            end
            farSum = farSum + farCurArea;
        end
        if closeCurArea > 0
            if r > 0
                w(r, c) = abs(w(r, c) - closeCurArea);
            end
            closeSum = closeSum + closeCurArea;
        end
    end
end

if rotationFactor > 0 %if we rotated to avoid vertical lines, rotate back
    w = rot90(w, rotationFactor); 
end
end
