REM >TestCode
REM ** Test out Some crap maths **

nv%=4
xv=0
yv=1
zv=2
MODE 12
DIM v%(nv%-1,2)
FOR c%=0 TO nv%-1
    READ v%(c%,0),v%(c%,1),v%(c%,2)
    PRINT v%(c%,0),v%(c%,1),v%(c%,2)
NEXT
PRINT'

PROCtest_height
END

REM ** Are we in a polygon check **

DEF PROCtest_poly
REPEAT
      INPUT "Xpos";x
      INPUT "Zpos";z
      c=0
      FOR c%=0 TO nv%-1
          p1%=c%
          p2%=c%+1:IF p2%=nv% p2%=0
          IF (v%(p1%,zv)<=z AND z<v%(p2%,zv)) OR (v%(p2%,zv)<=z AND z<v%(p1%,zv)) THEN
             IF x < ( (v%(p2%,xv) - v%(p1%,xv)) * (z - v%(p1%,zv)) / (v%(p2%,zv) - v%(p1%,zv)) + v%(p1%,xv) ) THEN
                c=NOT c
             ENDIF
          ENDIF
      NEXT
      IF c THEN PRINT "In it Dude!"' ELSE PRINT "Out of it Dude!"'
UNTIL FALSE
ENDPROC

REM ** Calc height at our point within the polygon **

DEF PROCtest_height
a=0:b=0:c=0:d=0
x=0:y=0:z=0
FOR c%=0 TO nv%-1
    p1%=c%
    p2%=c%+1:IF p2%=nv% p2%=0
    a+= (v%(p1%,yv)-v%(p2%,yv)) * (v%(p1%,zv)+v%(p2%,zv))
    b+= (v%(p1%,zv)-v%(p2%,zv)) * (v%(p1%,xv)+v%(p2%,xv))
    c+= (v%(p1%,xv)-v%(p2%,xv)) * (v%(p1%,yv)+v%(p2%,yv))
    x+= v%(p1%,xv)
    y+= v%(p1%,yv)
    z+= v%(p1%,zv)
NEXT
len = SQR(a*a + b*b +c*c)
x = x/nv%
y = y/nv%
z = z/nv%
a = a/len
b = b/len
c = c/len
d = -(x*a+y*b+z*c)
REPEAT
    INPUT "CarX";cx
    INPUT "CarZ";cz
    cy=-(a*cx+c*cz+d)/b
    PRINT "CarY:";cy'
UNTIL FALSE
ENDPROC

REM ** Points Data **

DATA  500, 200,-200

DATA -500,   0,-200

DATA -500,   0, 200

DATA  500, 200, 200
