Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: DDA Algorithm of a Star
Date: Mon, 18 Feb 2008 20:13:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 73
Message-ID: <fpcose$1bq$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1203365582 1402 172.30.248.35 (18 Feb 2008 20:13:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 18 Feb 2008 20:13:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 892175
Xref: news.mathworks.com comp.soft-sys.matlab:452195



Hi, I'm new to Matlab and am in need of a little help. I
currently study a University module which involves a lot of
matlab. 

My task is to draw a rasterisation of a star using a DDA
algorithm. I'm told this is really simple, I am almost there
I  have managed to program half of the star but cant work
out how to program the other half. 

Below is my 2 M-files so far...I'm told the first one is
100% correct but I have slight errors in the second. 

1) Star.m

screen=ones(110,110);

coord=[40 70; 5 55; 40 40; 55 5; 70 40; 105 55; 70 70; 55 105];
for i=1:7
    first=coord(i,:);
    last=coord(i+1,:);
    
    screen=dda(screen,first,last,0);
end

clf
imagesc(flipud(screen'));
title('..');
xlabel('..');
ylabel('..');
axis square;
colormap(gray);



2) DDA.m

function raster=dda(raster,first,last,color)

%check whether line is drawn from left to right
if(last(1)<first(1))
temp=last;
last=first;
first=temp;
end

%calculate the gradient
grad=(last(2)-first(2))/(last(1)-first(1));

x=first(1);
y=first(2);


if (abs(grad)<1)
for x=first(1):last(1)
raster(x,round(y))=color;
y=y+grad;
end

  
elseif (abs(grad)>1)
for y=first(2):last(2)
raster(y,round(x))=color;
x=x+1/grad;
end



end
 

Any help is much appreciated,

Neo.