[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
## [epsilon-devel] Learning to write I/O actions: paint_line

**From**: |
Luca Saiu |

**Subject**: |
[epsilon-devel] Learning to write I/O actions: paint_line |

**Date**: |
Thu, 20 Nov 2003 23:03:14 +0100 |

**User-agent**: |
Mozilla/5.0 (X11; U; Linux i586; en-US; rv:1.3a) Gecko/20021212 |

` Ok Matteo, I am giving you some hints to write a function paint_line,
``with type
` integer -> integer -> integer -> integer -> color -> i/o of void

`The function takes the coordinates of two points and a color, and
``returns an action which, when executed, paints a segment on the screen:
` x1 |--> y1 |--> x2 |--> y2 |--> color |--> action

` The function should *not* be named draw_line, since in the CVS
``version a function with that name is already present.
`

` We want to draw a segment on the screen. A segment is a subset of a
``straight line, which can be described by one of the following equations:
` y = m x + q // Case 1: a non-vertical straight line
x = k // Case 2: a vertical straight line
Let's exclude case 2 by now. So we are in case 1.

` It's easy to compute m and q; we want to force the line to include
``(x1, y1) and (x2, y2). So we solve the following linear system in m and q:
` / y1 = m x1 + q
\ y2 = m x2 + q
The solution is
/ m = (y2 - y1) / (x2 - x1)
\ q = (x2 y1 - x1 y2) / (x2 - x1)

` Note that there is no solution when x1 = x2, i.e. when the line is
``vertical. This is correct.
`

` Let's say that we want to draw one pixel for each integer value of x.
``Later I'm going to justify this siplification. So we are going to recur
``on x, and *only* on x, to make the computation faster; we are going to
``start from the minimum between x1 and x2 and we are going to stop at the
``maximum (it is useful to define two auxiliary functions min and max,
``both with type integer -> integer -> integer). For each x it's easy to
``compute y, since y = m x + q. Remember to compute m as a float, since it
``will be much more precise (this shouldn't be needed for q). You'll need
``integer_to_float and float_to_integer.
`

` We want to make our program fast, so we'll compute min_x, max_x, m
``and q just *once*. It's convenient to have a function which computes
``them (you can use a let) and then calls the recursive function with the
``computed values (and the color, and anything else which may be needed)
``as arguments. The convenience function (the non-recursive one) will be
``paint_line itself.
`

` Ok, you can start and send the result here; we are going to finish
``the next time; if what you do now is correct it will be very easy.
`` This list supports attachments, you can attach the .epb file. Use
``'reply all'.
`
Bye,
--
Luca Saiu, maintainer of GNU epsilon
http://www.gnu.org/software/epsilon

**[epsilon-devel] Learning to write I/O actions: paint_line**,
*Luca Saiu* **<=**