Path: news.mathworks.com!not-for-mail
From: "ken " <ken4aver@yahoo.com.tw>
Newsgroups: comp.soft-sys.matlab
Subject: Trouble - floor() BUG? or.... merely my faults?
Date: Tue, 17 Jun 2008 09:07:03 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 200
Message-ID: <g37urn$4eh$1@fred.mathworks.com>
Reply-To: "ken " <ken4aver@yahoo.com.tw>
NNTP-Posting-Host: webapp-03-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1213693623 4561 172.30.248.38 (17 Jun 2008 09:07:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 17 Jun 2008 09:07:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1223870
Xref: news.mathworks.com comp.soft-sys.matlab:474167




Hi, guys...
I'm new face here.

Is anybody could answer why the condition
 happened being so weird.

floor, a very simplex internal function which bothered me...

There are some marked lines below explain my problem.
I thought that if it's my problem, it'd be an "access
violation" when running my code and caused
such weird result.

Please take a look. (Trace the place I marked)
Thanks a lot.





function floor_bug

Line2Pixel([1 1], [-7 -7])


% Most of the time, this function works well, though,
% the following parameter set exactly causes problem.
% Please as well check the middle of the code where I 
highlighted.
% Problem there.
Line2Pixel([108 62], [-7 32])



function real_pixels=Line2Pixel(v1, v2)
% input: a line represented by end-point v1 to v2
% output: a sequence of pixels, may not in the order from 
v1 to v2, might
%           be in reversed order.
% There would be redundant pixels if points of (x, y) lie 
in the line
% where x and y are both integers, in which case we ignored 
to do clipping.

% % used for end-point verification
% s1=v1; s2=v2;

real_pixels=[];

% The followings deal with conditions on vert/hori line.
tmp0=v2-v1;                                             % a 
real vector
if tmp0==0                                              % a 
zero vector
    real_pixels=[floor(v1)];
    return;
elseif tmp0(1)==0                                       % a 
vertical line
    tmp00=floor(v1(1));
    for i=v1(2): sign(tmp0(2)): v2(2)
        real_pixels=[real_pixels; [tmp00 floor(i)]];
    end
    return;
elseif tmp0(2)==0                                       % a 
horizontal line
    tmp00=floor(v1(2));
    for i=v1(1): sign(tmp0(1)): v2(1)
        real_pixels=[real_pixels; [floor(i) tmp00]];
    end
    return;
end

m=(v2(2)-v1(2))/(v2(1)-v1(1));                          % 
the slope
param1x=v1(1)-v1(2)/m;                                  % a 
constant(a) for x=ny+a
param1y=v1(2)-m*v1(1);                                  % a 
constant(b) for y=mx+b

%CX=@(CY) CY/m+param1x;                                  % 
get x from y
%CY=@(CX) m*CX+param1y;                                  % 
get y from x

% Reorder v1 and v2 because we want to increasingly 
traverse along x-axis.
% After reordering, only two conditions for y are 
considered.
if v2(1)<v1(1); tmp0=v2; v2=v1; v1=tmp0; disorder=1; else 
disorder=0; end;

% The following code segment is a line-drawing algorithm 
which uses
% floating point calculation as to be inefficient.
pixels=floor([v1(1) v1(2)]);
x_prime=pixels(1);
x_end=floor(v2(1));

if v1(2)<v2(2)
    y_inc=1;
    while x_prime<=x_end
        y=m*(x_prime+1)+param1y;   %CY(x_prime+1);
        y_prime=floor(y);
        
        
        
        
        
        
        
        
        
        
        x=(y_prime+1)/m+param1x;   %CX(y_prime+1);
        
        
        
        
        
        
        
                  %BUG     HERE%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % CHECK THE NEXT LINE: x_prime=floor(x);
        % while iterating to x==16.0000,
        % some incredible problem occurs.
        % floor() was out of mind......
        % Anybody knows why?????????????????????
        % help me here.
        % Appreciate a lot.
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        
        x_prime=floor(x);
        pixels(end+1, :)=[x_prime y_prime]
    end
else
    y_inc=0;
    while x_prime<=x_end
        y=m*(x_prime+1)+param1y;   %CY(x_prime+1);
        y_prime=floor(y);
        x=(y_prime-1)/m+param1x;   %CX(y_prime-1);
        x_prime=floor(x);
        pixels(end+1, :)=[x_prime y_prime];
    end
end

% Transfer collected information to real pixels.
real_pixels=pixels(1, :);
if y_inc
    for i=2: size(pixels, 1)
        j=real_pixels(end, 2);
        while j<pixels(i, 2)
            j=j+1;
            if j>v2(2); break; end;
            real_pixels(end+1, :)=[real_pixels(end, 1) j];
        end
        j=real_pixels(end, 1);
        while j<pixels(i, 1)
            j=j+1;
            if j>v2(1); break; end;
            real_pixels(end+1, :)=[j real_pixels(end, 2)];
        end
    end
else
    for i=2: size(pixels, 1)
        j=real_pixels(end, 2);
        while j>pixels(i, 2)
            j=j-1;
            if j<v2(2); break; end;
            real_pixels(end+1, :)=[real_pixels(end, 1) j];
        end
        j=real_pixels(end, 1);
        while j<pixels(i, 1)
            j=j+1;
            if j>v2(1); break; end;
            real_pixels(end+1, :)=[j real_pixels(end, 2)];
        end
    end
end
if disorder; real_pixels=flipud(real_pixels); end; % reorder
% if length(real_pixels)>2 % clipping
%     tmp=abs(real_pixels(1: 2: end-2, :)-real_pixels(3: 2: 
end, :));
%     tmp=tmp<2;
%     real_pixels(2*find(and(tmp(:, 1), tmp(:, 2))), :)=[];
% end

% % used for end-point verification, fault if answer ~= 0
% s=floor([s1; s2]);
% prod([ ...
%     prod([sum(s(1, :)~=real_pixels(1, :)), sum(s(1, :)
~=real_pixels(end, :))]), ...
%     prod([sum(s(2, :)~=real_pixels(1, :)), sum(s(2, :)
~=real_pixels(end, :))]) ...
%     ])