Re: [Haskell-cafe] Line drawing algorithm

2009-07-17 Thread Neil Brown

CK Kashyap wrote:

Hi All,

I am working on a diagraming utility in Haskell. I started with line 
drawing.
I am doing the basic stuff using the y = mx + c formula to draw a line 
between (x1,y1) and (x2,y2)

Hi,

Are you doing this to learn Haskell, learn about drawing lines, or to 
just get it implemented?  If either of the latter two, when drawing a 
straight line you shouldn't need to do floating point operations such as 
this:



newY = y1 + round (slope * (fromIntegral (newX - x1)))


Bresenham's algorithm (or similar variants) allows you to draw a line 
without needing floating point.  A Haskell implementation is here:


http://rosettacode.org/wiki/Bresenham%27s_line_algorithm#Haskell

Although it may not be too understandable!  Wikipedia has an explanation 
of the general algorithm:


http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm

As to how to cope with the dy  dx case in your code given the dx  dy 
case, you could just swap the x and y coords at the start, then swap 
back the x and y coords of all the output points afterwards.  Odd, but 
effective :-)


Thanks,

Neil.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Line drawing algorithm

2009-07-17 Thread CK Kashyap
Thanks Neil ... 


 Are you doing this to learn Haskell, learn about drawing lines, or to just 
 get it implemented?  If either of the latter two, when drawing a straight 
 line you shouldn't need to do floating point operations such as this:

Actually, my reasons are first and third.

 newY = y1 + round (slope * (fromIntegral (newX - x1)))

 http://rosettacode.org/wiki/Bresenham%27s_line_algorithm#Haskell

Thanks for the link.

 As to how to cope with the dy  dx case in your code given the dx  dy case, 
 you could just swap the x and y coords at the start, then swap back the x and 
 y coords of all the output points afterwards.  Odd, but effective :-)
Slope would differ right for both case right?

Regards,
Kashyap



  ___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe