home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / os / msdos / programm / 10734 < prev    next >
Encoding:
Internet Message Format  |  1992-11-18  |  1.4 KB

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!uwm.edu!linac!uchinews!machine!ddsw1!infopls!rhps!warlok
  2. From: warlok@rhps.chi.il.us (Jon L Fincher)
  3. Newsgroups: comp.os.msdos.programmer
  4. Subject: Re: Line drawing algorithm wanted
  5. Message-ID: <gmycuB8w165w@rhps.chi.il.us>
  6. Date: Mon, 16 Nov 92 18:45:27 CST
  7. References: <17514@mindlink.bc.ca>
  8. Organization: CrissySoft Incorporated
  9. Lines: 44
  10.  
  11. Damon_Harper@mindlink.bc.ca (Damon Harper) writes:
  12.  
  13. > A request:
  14. > d
  15. > Could someone please post or send me a simple line-drawing algorithm, in C or
  16. > non-language-specific, it doesn't matter, the method is all I want.  Thanks.
  17. >  ---    ---
  18. > damon harper            Damon_Harper@mindlink.bc.ca
  19. >  ---    ---
  20.  
  21. The following routine is in Pascal (pseudo-code) and follows Bresenham's
  22. algorithm from the book "Computer Graphics" by Donald Hearn and M. Pauline
  23. Baker:
  24.  
  25. procedure bres_line(x1,y1,x2,y2:integer);
  26. var
  27.   dx,dy,x,y,x_end,p,const1,const2:integer;
  28. begin
  29.   dx := abs (x1-x2);  dy:=abs(y1-y2);
  30.   p := 2*dy-dx;
  31.   const1 := 2* dy;  const2 := 2* (dy-dx);
  32.  
  33.   if x1>x2 then begin
  34.     x := x2;  y:=y2;  x_end:=x1;
  35.   end
  36.   else begin
  37.     x:=x1;  y:=y1;  x_end:=x2;
  38.   end
  39.   set_pixel(x,y);   (* you supply this one, just set a pixel *)
  40.   while x<x_end do begin
  41.     x:=x+1;
  42.     if p<0 then p:=p+const1;
  43.   else begin
  44.       y:=y+1;
  45.       p:=p+const2;
  46.     end;
  47.     set_pixel(x,y);
  48.   end
  49. end;
  50.  
  51. Hope it helps.  Sorry if some C syntax creeped in...
  52.  
  53. J.L.Fincher, Warlok
  54.