Hi,
Thanks very much for the previous help. Since then I've tried to refine the
Bresenham routine a bit and did actually get it working, but then decided
to indulge my functional-programmer urges and try to make it higher level.
Unfortunately, this hit another problem..
The idea of doline is that it takes two coordinates and a quotation, traces
a line between the coordinates, and calls the quotation at each point along
the line, leaving the x and y coordinates on the stack for it. The
quotation is expected to leave two value on the stack itself: a "return"
value which is what doline should return if that's the end of the line, and
a "continue" value which specifies if the quotation wants to force the line
loop to halt at that point. The line loop may halt without being forced if
it has reached the destination coordinate, which is why the quotation is
always expected to push a return and continue value, rather than only
pushing a return if continue is f.
The problem, is after calling the quotation, if the quotation's continue
value is t (ie, it doesn't want to halt) and the line has not reached the
end, then an unused return value is left on the stack and I want to drop
it. Unfortunately it seems that there is no conditional function that will
allow me to drop a value based on a condition, as all of the conditions
seem to require that the stack is balanced on both sides of the
conditional. So this code refuses to compile because of the drop inside the
when block. Is there any way of doing this?
(Also, is the manual short circuit optimization of "and" into two whens
necessary or would the compiler have done it automatically? And, the code
is still.. um, rather C-like.. is there a way to style this better?)
Mark
! Create a new 10x10 grid
: <grid> ( -- byte-array ) 100 <byte-array> ;
! Get address of cell x,y
: celladdress ( n n -- n ) 10 * + ;
! Get value of cell at x,y
: cellat ( byte-array n n -- n ) celladdress swap nth ;
! Do operation on cell at x,y
:: docell! ( array x y op -- ) x y celladdress array op change-nth ; inline
! Set value of cell at x,y
:: setcell! ( array x y val -- ) array x y [ drop val ] docell! ; inline
:: doLine ( ax ay bx by code -- codeout )
bx ax - abs :> dx!
by ay - abs :> dy!
dx dy - :> err!
t :> continue!
ax :> cx!
ay :> cy!
[ continue ] [
cx cy code call continue!
cy by = [ cx bx = [ f continue! ] when ] when
[ continue ] [ drop
err 2 *
dup 0 dy - > [ err dy - err! cx dx sgn + cx! ] when
dx < [ err dx + err! cy dy sgn + cy! ] when
] when
] while ; inline
:: los? ( map ax ay bx by -- bool ) ax ay bx by [| cx cy | map cx cy cellat
0 = dup ] doLine ;
------------------------------------------------------------------------------
This SF.net email is sponsored by Windows:
Build for Windows Store.
http://p.sf.net/sfu/windows-dev2dev
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk