Quantcast
Channel: MATLAB Central Newsreader - tag:"centerline"
Viewing all articles
Browse latest Browse all 15

Re: Polyline offset

$
0
0
for those interested in offset lines, I found some code that seems to work

function [X,Y]=LineOffset(x,y,width)
OFFSET=width/2;
XX0=[x(1)-diff(x(1:2)) x x(end)+diff(x(end-1:end))];
YY0=[y(1)-diff(y(1:2)) y y(end)+diff(y(end-1:end))];
XOffset=zeros(size(x));
YOffset=zeros(size(x));
IsPossible=1;
for i = 2: length(x)+1;
    YVal = -1*OFFSET;
    [TH, R]=cart2pol(XX0-XX0(i),YY0-YY0(i));
    TH_Spin=TH(i-1)-pi;
    [XNew, YNew]=pol2cart(TH-TH_Spin, R);
    dX=diff(XNew(i:i+1));
    dY=diff(YNew(i:i+1));
    % There are 9 conditions for dY & dX
    XVal = 0;
    if dX<=0 && dY==0
        IsPossible=0; % not a good definition
    else
        if dX == 0;
            if dY > 0;
                XVal = OFFSET;
            else %dY < 0
                XVal = -1*OFFSET;
            end
        elseif dX > 0;
            slope = dY/dX;
            if dY ~= 0;
                intercept = -1*OFFSET / (cos(atan(abs(slope))));
                XVal=(YVal-intercept)/slope;
            end
        else %dX < 0
            slope = dY/dX;
            intercept = OFFSET / (cos(atan(abs(dY/dX))));
            XVal=(YVal-intercept)/slope;
        end
    end
    [TH0, R0]=cart2pol(XVal,YVal);
    [XTemp,YTemp]=pol2cart(TH0+TH_Spin,R0);
    XOffset(i-1)=XTemp+XX0(i);
    YOffset(i-1)=YTemp+YY0(i);
end

XOffset2=(x-XOffset)+x;
YOffset2=(y-YOffset)+y;

X=[XOffset fliplr(XOffset2) XOffset(1)];
Y=[YOffset fliplr(YOffset2) YOffset(1)];
if ~IsPossible
    X=[];
    Y=[];
end


please post if you can figure out any improvements to this code.

Thanks
Jason

Viewing all articles
Browse latest Browse all 15

Trending Articles