delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/05/27/20:15:26

From: "Michael Stewart" <mike AT reggin DOT freeserve DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
References: <8gp59t$h8n$1 AT lure DOT pipex DOT net>
Subject: Re: help a semi-newbie with some graphics....
Lines: 96
X-Newsreader: Microsoft Outlook Express 5.00.2014.211
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2014.211
X-Original-NNTP-Posting-Host: 212.188.150.76
Message-ID: <3930621b@news.server.worldonline.co.uk>
Date: Sun, 28 May 2000 01:04:33 +0100
NNTP-Posting-Host: 212.49.224.15
X-Trace: server12-lon1.london1.l3.net 959472156 212.49.224.15 (Sun, 28 May 2000 01:02:36 BST)
NNTP-Posting-Date: Sun, 28 May 2000 01:02:36 BST
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

DeepBlack (Murray Evans) <deepblack AT dial DOT pipex DOT com> wrote in message
news:8gp59t$h8n$1 AT lure DOT pipex DOT net...
<snip>

> 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_x == start_x) // bit pedantic, but I think neater :-)

>       {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_y == start_y) // ditto

>         {
>          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);
>          }
>         }
>
>       dx = sqrt((end_x - start_x)*(end_x - start_x));
dx = abs (end_x - start_x);

>       dy = sqrt((end_y - start_y)*(end_y - start_y));
dy = abs (end_y - start_y);

> any suggestions as to what is wrong?
I can't see any problems but the code you presented is very inefficient.
Perhaps you would be better using an implementation of the Bresenham line
routine (as used in practically every line drawing routine in practically
every graphics library, including Allegro).
You should be able to adapt the following code to fit your library.

void line (int x1, int y1, int x2, int y2, int col) {
   int i, steep = 0, sx, sy, dx, dy, e;

   dx = abs (x2 - x1);
   sx = ((x2 - x1) > 0) ? 1 : -1;
   dy = abs (y2 - y1);
   sy = ((y2 - y1) > 0) ? 1 : -1;

   if (dy > dx) {
      int temp = x1;
      x1 = y1;
      y1 = temp;
      temp = dx;
      dx = dy;
      dy = temp;
      temp = sx;
      sx = sy;
      sy = temp;
      steep = 1;
   }

   e = 2 * dy - dx;
   i = 0;
   while (i < dx) {
      if (steep) put_pixel (y1, x1, col);
      else put_pixel (x1, y1, col);

      while (e >= 0) {
         y1 += sy;
         e -= 2 * dx;
      }
      x1 += sx;
      e += 2 * dy;
      i++;
   }
   put_pixel (x2, y2, col);
}

Hope this helps

Mike


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019