home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l4p160
< prev
next >
Wrap
Text File
|
1990-07-15
|
3KB
|
96 lines
╔════════════════════════════════════════════════════╗
║ Lesson 4 Part 160 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
\ This is the program that calculates the area of a polygon in terms
\ of its verticies. You should study the program and note how it
\ uses variables and arrays that have been discussed in lesson 4.
\ This should be replaced by the bullet proof version of #IN given
\ in Lesson 3.
: #IN ( -- n )
QUERY INTERPRET ;
CREATE X 102 ALLOT \ Array for x coordinates
CREATE Y 102 ALLOT \ Array for y coordinates
VARIABLE #POINTS \ Number of points in polygon
VARIABLE AREA \ Sum of the x(i)y(i-1) - x(i-1)y(i)
\ Array fetching and storing words.
\ Fetch ith x component.
: X@ ( i x{i} ) 2* X + @ ;
\ Fetch ith y component.
: Y@ ( i y{i} ) 2* Y + @ ;
\ Store ith x component.
: X! ( x i -- ) 2* X + ! ;
\ Store ith y component.
: Y! ( y i -- ) 2* Y + ! ;
\ Move to the next tab stop.
: TAB ( -- )
BEGIN #OUT @ 8 MOD
IF SPACE ELSE EXIT THEN
AGAIN ;
\ Get number from keyboard with prompt symbol.
: GET# ( -- n )
ASCII > EMIT SPACE #IN ;
\ Prompt and fetch number of data points.
: GET_#POINTS ( -- )
BEGIN
CR ." Enter number of data points. "
GET# DUP 3 <
WHILE CR ." You need at least 3 data points!"
REPEAT 50 MIN #POINTS ! ;
\ Prompt and fetch all data points.
: GET_DATA ( -- )
CR CR ." Point " TAB ." X" TAB ." Y"
#POINTS @ 1+ 1
DO CR I 3 .R TAB GET# I X!
TAB GET# I Y! LOOP
#POINTS @ DUP X@ 0 X! Y@ 0 Y! ; \ Store last point in 0th slot
\ Sum data points.
: FIND_AREA ( -- )
0 AREA !
#POINTS @ 1+ 1 ( n+1 so we loop n times )
DO I X@ I 1- Y@ * ( X{i}*Y{i-1} )
I 1- X@ I Y@ * ( X{i-1}*Y{i} )
- AREA +!
LOOP ;
\ Display computed area.
: PUT_AREA ( -- )
AREA @ 2 /MOD
CR ." AREA = " 6 .R ASCII . EMIT
IF ASCII 5 EMIT ELSE ASCII 0 EMIT THEN SPACE ;
\ Compute area of polygon.
: POLY ( -- )
GET_#POINTS
GET_DATA
FIND_AREA
PUT_AREA ;
\ ╓──────────────╖
\ ║ Problem 4.24 ║
\ ╙──────────────╜
\ Implement and Test the POLY program.
\ POLY is the name of the end user program. Type POLY and use the test
\ data that is provided in the examples of lesson 4 part 15 and then
\ make up some test data of your own.
\ Your next problem is going to be to modify the above program so that
\ it also computes the perimeter of the polygon. You can begin thinking
\ about how this might be done. Some suggestion on how to proceed will
\ be provided in part 170 of lesson 4 which follows.