home *** CD-ROM | disk | FTP | other *** search
- ╔════════════════════════════════════════════════════╗
- ║ Lesson 4 Part 170 F-PC 3.5 Tutorial by Jack Brown ║
- ╚════════════════════════════════════════════════════╝
-
- ╓──────────────╖
- ║ Problem 4.25 ║
- ╙──────────────╜
- Modify the poly program so that it also calculates the perimeter
- of the polygon. To do this we will need a better square root
- algorithm. One which appeared in Forth Dimensions is given below.
-
- We give only the skeleton of the solution below. The only new words you
- need to code and add are DIST , FIND_PERIMETER , and PUT_PERIMETER
-
- \ The theorem of Pythagorous is used to calculate the length of
- \ each leg of the polygon.
- \ Y
- \ | p2 p1 = ( x1,y1 )
- \ | /| p2 = ( x2,y2 )
- \ | / |
- \ | d / | b = y2 - y1
- \ | / | b a = x2 - x1
- \ | / |
- \ | / a | d = [(x2-x1)^2 + (y2-y1)^2]^.5
- \ | p1 --------
- \ |----------------X
- \
- \ 32-bit Fixed Point Square Root by Klaxon Suralis
- \ From Forth Dimensions Volume 4 Number 9 Page 9 May/June 1982
- \ Read the original artical for an explanation of the code.
- \ We are just going to use it here, not understand it.
-
- : EASY-BITS ( drem1 partial.root1 count -- drem2 partial.root2 )
- 0 DO >R D2* D2*
- R@ - DUP 0<
- IF R@ + R> 2* 1-
- ELSE R> 2* 3 +
- THEN LOOP ;
-
- : 2'S-BIT ( drem2 proot2 -- drem3 proot3 ) \ get penultimate bit
- >R D2* DUP 0<
- IF D2* R@ - R> 1+
- ELSE D2* R@ 2DUP U<
- IF DROP R> 1-
- ELSE - R> 1+
- THEN THEN ;
-
- \ Get the last bit.
- : 1'S-BIT ( drem3 proot3 -- fullroot ) \ remainder lost
- >R DUP 0<
- IF 2DROP R> 1+
- ELSE D2* 32768 R@ DU< 0= R> THEN ;
-
- \ 32-bit unsigned radicand to 16-bit unsigned square root
- : SQRT ( ud -- un ) \ un is the 16-bit square root of 32-bit ud.
- 0 1 8 EASY-BITS ROT DROP 6 EASY-BITS
- 2'S-BIT 1'S-BIT SWAP DROP ; \ SWAP DROP added to leave 16-bits
-
- \ Display 16-bit number with two decimal places.
- \ Don't worry about how this works... It will be explained in a
- \ later tutorial lesson.
- : I.XX ( 100*n -- )
- 0 <# # # ASCII . HOLD #S #>
- TYPE SPACE ;
-
- \ Display square root on n to 2 decimal places.
- \ Number is scaled by 10000 first forming a 32-bit product so
- \ that no significance is lost.
- : .SQRT ( n -- )
- 10000 UM* SQRT I.XX ;
-
- \ Polygon Area & Perimeter
- \ ***** Add one new variable to the variable list ****
- VARIABLE PERIMETER \ Sum of [{x(i)-x(i-1)}^2+{y(i)-y(i-1)}]^.5
-
- \ **** This is a new defintion that you must make.
- \ Calculate the distance between (x1,y1) and (x2,y2)
- \ and leave it scaled by a factor of 100.
- : DIST ( x2 y2 x1 y1 -- 100*d )
-
- \ **** This is a new definition that you must make.
- \ Find the perimeter of the polygon saving result in the
- \ Variable PERIMETER
- : FIND_PERIMETER ( -- )
-
- \ **** This is a new definition that you must make.
- \ Display computed perimeter.
- : PUT_PERIMETER ( -- )
-
- \ Compute area of polygon.
- : POLY ( -- )
- GET_#POINTS GET_DATA
- FIND_AREA FIND_PERIMETER
- PUT_AREA PUT_PERIMETER ;
-
- \ Good luck!
-
- ┌───────────────────────────────────┐
- │ Please move to Lesson 4 Part 180 │
- └───────────────────────────────────┘
-