Mail Archives: djgpp/2000/05/27/15:00:38
From: | "DeepBlack \(Murray Evans\)" <deepblack AT dial DOT pipex DOT com>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | help a semi-newbie with some graphics....
|
Date: | Sat, 27 May 2000 19:48:06 +0100
|
Organization: | UUNET WorldCom server (post doesn't reflect views of UUNET WorldCom
|
Lines: | 106
|
Message-ID: | <8gp59t$h8n$1@lure.pipex.net>
|
NNTP-Posting-Host: | userb362.uk.uudial.com
|
X-Trace: | lure.pipex.net 959453309 17687 193.149.82.91 (27 May 2000 18:48:29 GMT)
|
X-Complaints-To: | abuse AT uk DOT uu DOT net
|
NNTP-Posting-Date: | 27 May 2000 18:48:29 GMT
|
X-Priority: | 3
|
X-MSMail-Priority: | Normal
|
X-Newsreader: | Microsoft Outlook Express 5.00.2919.6600
|
X-MimeOLE: | Produced By Microsoft MimeOLE V5.00.2919.6600
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Right, I've got a working pixel routine, a working rectangle drawing
routine, and working vertical and horizontal line routines, which can all
write to either the screen or a 'virtual' screen set up in memory. My
problem arrises with my line drawing routine to cope with lines that are not
horizontal/vertical.
here's the routine(it compiles without errors, and runs without them too,
but the lines it draws, if it even draws hem, are either horizontal or
vertical, even if I feed it information for a very diagonal line. I'm pretty
sure my maths is right, I can run through the thing in my head and with a
calculator and it'll work for me. Suggestions?):
(scr_buff is what I can feed into the routine to tell it to write to either
the screen or the virtual screen. it makes little difference to the actual
routine, col, is, surprisingly, colour.)
void line(int scr_buff, int start_x, int start_y, int end_x, int end_y, int
col)
{
int x,y,dx,dy;
float m;
/*this first part will draw straight lines, and take account of any
divide by zero's which may crop up.*/
if((end_x - start_x) == 0)
{if(end_y > start_y)
{
v_line(scr_buff,start_x,start_y,(end_y-start_y),col);
}
if(start_y > end_y)
{
v_line(scr_buff,start_x,start_y,(start_y-end_y),col);
}
}
if((end_y - start_y)==0)
{
if(end_x > start_x)
{
h_line(scr_buff,start_x, start_y, (end_y-start_y), col);
}
if(start_x > end_x)
{
h_line(scr_buff,start_x, start_y, (start_y-end_y), col);
}
}
/* this part will draw those awkward lines at angles. shiver. :{ */
/*the theory here, is that by squaring, and then taking a root, I have
to end up with a positive number for dy & dx. Why does this matter?
For the compare. If x is longer, but dx is negative, dy will likely be
a
larger number, thus, the compare will go the wrong way
maths is based on (y-b) = m*(x-a)
is there a better rounding function than floor or ceil? */
dx = sqrt((end_x - start_x)*(end_x - start_x));
dy = sqrt((end_y - start_y)*(end_y - start_y));
m = (end_y - start_y)/(end_x - start_x);
if(dx > dy)
{
if(end_x > start_x)
{
for(x=start_x;x < end_x; x++)
{
y = floor((m*(x-start_x) + start_y));
putpixel(scr_buff,x,y,col);
}
}
if(start_x > end_x)
{
for(x=end_x;x<start_x;x++)
{
y = floor((m*(x-start_x) + start_y));
putpixel(scr_buff,x,y,col);
}
}
}
if(dy > dx)
{
if(end_y > start_y)
{
for(y=start_y;y < end_y; y++)
{
x=floor(((y-start_y)/m)-start_x);
putpixel(scr_buff,x,y,col);
}
}
if(start_y > end_y)
{
for(y=end_y;y<start_y;y++)
{
x = floor(((y-start_y)/m)-start_x);
putpixel(scr_buff,x,y,col);;
}
}
}
any suggestions as to what is wrong?
- Raw text -