>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Phrases

# 9F. Geometry

We begin with some simple functions (Length, Area, Volume) of various figures (rectangle, box, circle, cone, sphere, pyramid) applied to a length or list of lengths. For example:

`m0=: Ar=: */` | Area of rectangle
`m1=: Ab=: 2: * [:+/ ] *1&|.` | Area of box
`m2=: Vb=: */` | Volume of box
`m3=: Lci=: 2: * o.` | Length (circumference) of circle (radius)
`m4=: Aci=: [:o. ] ^ 2:` | Area of circle (r)
`d5=: Aco=: o.@*` | Area of cone, excluding base (h r)
`d6=: Vco=: 1r3p1"_ * ] * *` | Volume of cone (h r)
`m7=: As=: 4p1"_ * ] ^ 2:` | Area of sphere (r)
`m8=: Vs=: 4r3p1"_ * ] ^ 3:` | Volume of sphere (r)
`m9=: L=: +/&.(*:"_)"1` | Length of a vector
`d10=: Lp=: [: L [ , [: L [: -: ]` | Length of edges of pyramid (h w,l)
`d11=: Ap=: [:+/ ]* [:L"1 [,"0-:@]` | Area of pyramid, excluding base (h w,l)
`d12=: Vp=: 1r3"_ * */@,` | Volume of pyramid
`m13=: sp=: -:@(+/)` | Semi-perimeter
`m14=: h=: [: %: [: */ sp - 0: , ]` | Heron's formula for area of triangle

For example:

       h 3 4 5
    6

       h 51 52 53
    1170

       h 2 2 2
    1.73205

In treating coordinate geometry we will use a list of n elements to represent a point in n-space, and an m by n table to represent a polygon of m vertices. For example:

       p=: 3 1 [ q=: 4 1 [ r=: 5 9   NB. Three points
       T=: p,q,:r                    NB. A triangle
       L=: +/&.(*:"_)"1              NB. Length function
       L p
    3.16228

       u=: 1&|.   Rotate up
       D=: u-]   Displacements

       ,.&.>(];u;D;L@D)T3            NB. Displacements and lengths (of sides)
    +---+---+-----+-------+
    |3 1|4 1| 1  0|      1|
    |4 1|5 9| 1  8|8.06226|
    |5 9|3 1|_2 _8|8.24621|
    +---+---+-----+-------+

       line=: 3 1 4,:1 5 9           NB. A line in 3-space
       (];-/;L@(-/);L)               NB. line Line, disp, length, distances to ends
    +-----+-------+------+---------------+
    |3 1 4|2 _4 _5|6.7082|5.09902 10.3441|
    |1 5 9|       |      |               |
    +-----+-------+------+---------------+

       T3=: ?.3 3$10                 NB. Random triangle in 3-space
       ,.&.>(];u;D;L@D) T3
    +-----+-----+--------+-------+
    |1 7 4|5 2 0| 4 _5 _4|7.54983|
    |5 2 0|6 6 9| 1  4  9|9.89949|
    |6 6 9|1 7 4|_5  1 _5|7.14143|
    +-----+-----+--------+-------+

`m15 =: L=: +/&.(*:"_)"1` | Length
`m16=: D=: 1&|.-]` | Displacement
`m17=: LS=: L"1@D` | Lengths of sides
`m18=: S=: 1&o.@(*&1r180p1)` | Sine in degrees
`m19=: C=: 2&o.@(*&1r180p1)` | Cosine in degrees
`m20=: r=: (C,S),:(-@S,C)` | 2-dim rotation matrix in degrees
`m21=: b=: <"1@(,"0/~)` | Table of boxed index pairs: do `i 0 2`
`d22=: R=: (r@])`(b@[)`(=@i.@3:)}` | 3-dim rm: From axis 0 to 2 is `0 2 R a`
`d23=: mp=: +/ . *` | Matrix product
`m24=: R3=: (2 0"_ R 0&{)mp(1 2"_ R 1&{)mp(0 1"_ R 2&{)` | `R3 p,q,r` is p-rotate from axis 2 to 1 on q-r from 1 to 2 on r-r from 0 to 1
`m25=: Det=: -/ . *` | Determinant
`m26=: Area=: [:Det ] ,. %@!@{:@$` | Area of triangle
`m27=: Vol=: Area f.` | Volume of simplex in n-space (fixed)
`d28=: dsplitby=: ~:/@:*@:Vol@: (,"1 2)` | Are points pairs (2 by n matrix) x separated by n by n simplex y?
m29=: Area2=: [: -: [: +/ 2: Det\ ] | Area of polygon

Area yields the area of a triangle expressed as a 3 by 2 list of x-y coordinates:

       TT
    3 3
    6 5
    2 7

       Area TT
    7

Area2 also yields the area of a triangle, expressed by a similar table, but with the top row repeated at the bottom:

       TT2
    3 3
    6 5
    2 7
    3 3

       Area2 TT2
    7

It is more general, however, and will yield the area of arbitrary polygons:

       Polygon
     7 2
    10 5
     6 8
     3 6
     4 3
     7 2

       Area2 Polygon
    24.5

>> << Ndx Usr Pri Phr Dic Rel Voc !: wd Help Phrases