[Haskell-cafe] Need feedback on my Haskell code

CK Kashyap ck_kashyap at yahoo.com
Tue Jul 28 09:04:47 EDT 2009

Hi Everyone,
I managed to write up the line drawing function using the following links - 

line :: Point -> Point -> [Point]
line (xa,ya) (xb,yb) = line' (x1,y1) (x2,y2) deltax deltay ystep isSteep 0
    isSteep = abs (yb - ya) > abs (xb - xa)
    (xa',ya',xb',yb') = if isSteep
      then (ya,xa,yb,xb)
      else (xa,ya,xb,yb)
    (x1,y1,x2,y2) = if xa' > xb'
      then (xb',yb',xa',ya')
      else (xa',ya',xb',yb')
    deltax = x2 - x1
    deltay = abs (y2 - y1)
    ystep = if y1 < y2 then 1 else -1

line' :: Point -> Point -> Integer -> Integer -> Integer -> Bool -> Integer -> [Point]
line' (x1,y1) (x2,y2) deltax deltay ystep isSteep error =
  if x1 == x2
  then if isSteep then [(y1,x1)] else [(x1,y1)]
    if isSteep
      then [(y1,x1)] ++ line' (newX,newY) (x2,y2) deltax deltay ystep isSteep newError 
      else [(x1,y1)] ++ line' (newX,newY) (x2,y2) deltax deltay ystep isSteep newError
          newX = x1 + 1
          tempError = error + deltay
          (newY, newError) = if (2*tempError) >= deltax then (y1+ystep,tempError-deltax) else (y1,tempError)

Can someone please provide feedback on this? In terms of, how do I get more Haskell'ism into it.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090728/ecd2935b/attachment.html

More information about the Haskell-Cafe mailing list