1. Promoting euphoria, bresenham's line algorithm for beginner/intermediate
- Posted by local2 Mar 02, 2012
- 1591 views
From the euphoria page Promoting Euphoria : http://openeuphoria.org/wiki/view/PromoteEuphoria.wc It suggests to promote euphoria. I had free time. I liked the language. I wrote short sentences.
Bresenham's line algorithm. Use it to make lines from one point to another in graphics, data structures. Much more information here : http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
I'm new to euphoria and I have never made a rosetta code post, so I spent about 10 hours today translating the C version there into something usable in euphoria 4.0.3 . I'm using Edita for writing euphoria and I like the bracket and color highlighting in that. The C version of this algorithm is very good, in my opinion. I couldn't make it quite as dense because of the ternary operators and shortened if statements in C, but I am finding euphoria very easy to read at a glance. If anyone notices logic errors or other possible bugs due to translation or my unfamiliarity with euphoria I would be interested to know, since I don't program often. Questions, comments, edits welcome.
Thanks very much to euphoria 4.0 manual writers, website maintainers, everybody involved in euphoria. Code below, it runs for me in euphoria 4.0. Code also at : http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#Euphoria
include std/console.e include std/graphics.e include std/math.e -- the new_image function and related code in the 25 or so -- lines below are from http://rosettacode.org/wiki/Basic_bitmap_storage#Euphoria -- as of friday, march 2, 2012 -- Some color constants: constant black = #000000, white = #FFFFFF, red = #FF0000, green = #00FF00, blue = #0000FF -- Create new image filled with some color function new_image(integer width, integer height, atom fill_color) return repeat(repeat(fill_color,height),width) end function -- Usage example: sequence image1 image1 = new_image(800,600,black) -- Set pixel color: image1[400][300] = red -- Get pixel color atom color1 color1 = image1[400][300] -- Now color is #FF0000 --grid used for drawing lines in this program sequence screenData = new_image(16,16,black) --the line algorithm procedure bresLine( integer x0, integer y0, integer x1, integer y1) integer deltaX = abs(x1 - x0), deltaY = abs(y1 - y0) integer stepX, stepY, lineError, error2 if x0 < x1 then stepX = 1 else stepX = -1 end if if y0 < y1 then stepY = 1 else stepY = -1 end if if deltaX > deltaY then lineError = deltaX else lineError = -deltaY end if lineError = round(lineError / 2, 1) while 1 do screenData[x0][y0] = white if (x0 = x1 and y0 = y1) then exit end if error2 = lineError if error2 > -deltaX then lineError -= deltaY x0 += stepX end if if error2 < deltaY then lineError += deltaX y0 += stepY end if end while end procedure --prevents console output wrapping to next line if it is too big for the screen wrap(0) --outer diamond bresLine(8,1,16,8) bresLine(16,8,8,16) bresLine(8,16,1,8) bresLine(1,8,8,1) --inner diamond bresLine(8,4,12,8) bresLine(12,8,8,12) bresLine(8,12,4,8) bresLine(4,8,8,4) -- center lines drawing from left to right, and the next from right to left. bresLine(7,7,9,7) bresLine(9,9,7,9) --center dot bresLine(8,8,8,8) --print to the standard console output for i = 1 to 16 do puts(1,"\n") for j = 1 to 16 do if abs( ( (screenData[j][i] / #FFFFF) - 1 ) ) = 1 then printf(1, "%s", ".") else printf(1, "%s", "#") end if end for end for puts(1,"\n\n") any_key() --/* --output was edited to replace the color's hex digits for clearer output graphics. --to output all the hex digits, use printf(1,"%06x", screenData[j][i]) --to output 'shortened' hex digits, use : --printf(1, "%x", ( abs( ( (screenData[j][i] / #FFFFF) - 1 ) ) - 1 ) ) --and --printf(1,"%x", abs( ( (screenData[j][i] / #FFFFF) - 1 ) ) ) -- --,respectively in the last if check. --*/