Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth > Re: A Brief Loo...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 10 of 13 Topic 3882 of 4288
Post > Topic >>

Re: A Brief Look at History

by Aleksej Saushev <asau@[EMAIL PROTECTED] > Mar 22, 2008 at 05:13 PM

Elizabeth D Rather <erather@[EMAIL PROTECTED]
> writes:

> Duke Normandin wrote:
>> On Thu, 20 Mar 2008, Bruce McFarling wrote:
>>
>>
>> [snip]
>>
>>> : BOX3 ( x1 y1 x2 y2 -- )
>>>     dup-points topline LINE
>>>     dup-points rightline LINE
>>>     dup-points bottomline LINE
>>>     leftline LINE ;
>>>
>> So if I wanted to play with all of this as a learning exercise, how
would
>> I define LINE?
>
> The thought model is moving a light beam on the screen.  It
> could be off, in which case it makes no mark, or on, in which
> case a line is drawn.  Assume two words, ON and OFF, that
> control the beam.  The beam stays in whatever state it's set.
> 0, 0 is the upper left corner of the screen.
>
> You have a "current position" in x,y coordinates:
>
> 2VARIABLE CURRENT
>
> And you have a word DRAW, which takes a *desired* position in
> x,y coordinates that drives the pen:
>
> : DRAW  ( x, y -- )   GOTOXY CURRENT 2! ;
>
> GOTOXY is a common-usage word that mirrors GETXY by moving the
> cursor. It would probably actually be replaced by some graphics
> primitive.
>
> It's nice to have an incremental draw:
>
> : +DRAW ( dx, dy -- )   CURRENT 2@[EMAIL PROTECTED]
 V+  DRAW ;
>
> (V+ would also be a primitive, adding two vectors. I'd write it
> in code, unless I had an optimizing compiler; YMMV.  You'll
> probably also want V*/ for scaling world to screen coordinates)
>
> So, let's say you want a box, given the x, y size and x, y
> position of the upper left corner.  You're going to have to
> manipulate the deltas:
>
>
> : BOX ( dx dy x y -- )  	 \ Box of size dx dy starting at x y
>    OFF DRAW 			 \ Pen at starting position; dx dy
>    OVER 0 ( x 0) ON +DRAW	 \ Left to right; dx dy
>    0 OVER ( 0 y) +DRAW		 \ Top to bottom; dx dy
>    SWAP NEGATE 0 ( -x 0) +DRAW	 \ Right to left; dy
>    0 SWAP NEGATE ( 0 -y) +DRAW ; \ Bottom to top
>
> Yeah, there's some stack manipulation.  But I think it's
> straightforward, and easy to model in the head.  I included
> line-by-line stack comments, although an experienced Forther
> probably wouldn't.  It's also a good example to someone learning
> stack management.

Easy?

You had to include line-by-line stack comments to explain,
what is going on, and even in-line ones to show, what is on
the stack at the point.

First, you've introduced state, and you don't restore it on exit,
thus you propagate sloppy programming style.
Second, you've converted to relative drawing primitive, but very
inconvenient ones, if you're trying to solve tasks of that kind.
Third, you've chosen inconvenient order of parameters.

: box ( x y dx dy ) 2swap moveto
  over right  dup down  swap left  up ;

Even with stack juggling it is easier to understand.


-- 
CKOPO BECHA...
   CKOPO CE3OH...
 




 13 Posts in Topic:
Re: A Brief Look at History
"Mark W. Humphries&q  2008-03-14 09:37:34 
Re: A Brief Look at History
John Doty <jpd@[EMAIL   2008-03-14 10:45:18 
Re: A Brief Look at History
Duke Normandin <dukeof  2008-03-21 06:06:10 
Re: A Brief Look at History
Elizabeth D Rather <er  2008-03-21 08:57:25 
Re: A Brief Look at History
mhx@[EMAIL PROTECTED] (M  2008-03-21 19:42:36 
Re: A Brief Look at History
Elizabeth D Rather <er  2008-03-21 15:20:41 
Re: A Brief Look at History
Doug Hoffman <no.spam&  2008-03-21 17:42:13 
Re: A Brief Look at History
Roelf Toxopeus <these3  2008-03-22 00:11:25 
Re: A Brief Look at History
Doug Hoffman <no.spam&  2008-03-21 20:22:59 
Re: A Brief Look at History
Aleksej Saushev <asau@  2008-03-22 17:13:10 
Re: A Brief Look at History
Frank Buss <fb@[EMAIL   2008-03-22 05:06:27 
Re: Forth for the Macintosh (was Re: A Brief Look at History)
Duke Normandin <dukeof  2008-03-25 03:57:30 
Re: A Brief Look at History
Duke Normandin <dukeof  2008-03-21 15:40:20 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Mon Oct 13 8:45:01 CDT 2008.