.PS

define Affang { ($1-($2))/sqrt(($1-($2)).x^2+($1-($2)).y^2) }
#                   Affine(x,y,A,S) = A+rot(S)*(x,y) = A+dprot(a,x,y)
define Affine { $3 + ($4.x*($1)-$4.y*($2), $4.y*($1)+$4.x*($2)) }
define Pprop { $2: ($1*($3)+$2*($4))/($5) } 
#
define ahoffset {($3)/($2)*sqrt(($1)^2+($2)^2)}
define mark { {if "$2"=="" then {TT:$1} else {TT:$2}
   circle thick 0.8 rad 0.01*scale at TT
  "$1" at last circle.n above } }
define distance { sqrt(($1.x-$2.x)*($1.x-$2.x)+($1.y-$2.y)*($1.y-$2.y)) }

define markahead {[ # (Name,wid,ht,thick,type)
  Point: $1.start; Shaft: $1.end; wid_=$2; ht_=$3; ltu_=($4)/72*scale
  C: Affang(Shaft,Point);          # Shaft direction cosines
  po = min(ahoffset(ht_,wid_,ltu_),ht_)
  P: Affine(po,0,Point,C);         # Point adjusted by line thickness
  h = ht_-ltu_/2
  x = h - po;
  if ht_ == 0.0 then {v = 0.0} else {v = (wid_/2)*x/ht_}
  R: Affine(h, v,Point,C);
  L: Affine(h,-v,Point,C);
  if x == 0.0 then {t = 1} else {t = ht_/x}
  Rx: (Point.x + (R.x-P.x)*t,Point.y + (R.y-P.y)*t)
  Lx: (Point.x + (L.x-P.x)*t,Point.y + (L.y-P.y)*t)
  Px: ((Point.x+Lx.x+Rx.x)/3,(Point.y+Lx.y+Rx.y)/3)
  if distance(Px,Point) < distance(P,Point) then { Px: P }
  if ht_ == 0.0 then {y = 0} else {y = ht_-po+ltu_*wid_/ht_/4}
  mark(P)
  mark(L)
  mark(R)
  mark(Px)
  mark(Lx)
  mark(Rx)
  ] with .Point at $1.start }

define ArcCentre { # (A,B,ccw,rsq,R)
  Q: $1-$2
  $5: ($1+$2)/2+(-Q.y,Q.x)*sqrt($4/(Q.x^2+Q.y^2)-1/4)*($3)
  }

define markarchead {[ # (Name,wid,ht,thickp,type)
  C: $1.c
  Point: $1.start
  radius_ = $1.rad
  angle = cangle($1.start,$1.c,$1.end)
  if angle < 0 then { angle += 2*pi_ }
  if radius_*angle > 0 then {ccw_ = 1} else {ccw_ = -1}
  startarrow = (radius_ >= 0);
  ht_ = abs($3); wid_ = abs($2);
  lth = $4
  lwi = abs(lth)/72*scale
  wid_ = max(wid_,lwi);
  radius_ = abs(radius_)
  rsq = radius_*radius_
  if radius_ == 0 then {ha = 0} else {ha = ht_/radius_}
  Q: (cos(ha), ccw_*sin(ha))
  Ac: Affine(Point.x-C.x,Point.y-C.y,C,Q);
  Ao: Ac; Pprop(C,Ao,-wid_/2,radius_+wid_/2,radius_);
  Ai: Ac; Pprop(C,Ai, wid_/2,radius_-wid_/2,radius_);
#                               (* Co, Ci *)
  ArcCentre(Ao,Point,ccw_,rsq,Co)
  ArcCentre(Ai,Point,ccw_,rsq,Ci)
  if wid_==0 then {P:Ao} else {if radius_==0 then {P:C} else {
    t = min(wid_,lwi)/wid_*ht_/radius_
#   t = min(wid_,lwi)/wid_
#   t = t*(2-t)*ht_/radius_
    Q: (cos(t),sin(t))
    P: Affine(Point.x-C.x,Point.y-C.y,C,Q)
    }}
#                                (* Type 3 center and corners *)
  if radius_ == 0 then {t = 0} else {t = min(pi_/2,ht_/radius_*2/3)}
  Q: (cos(t),ccw_*sin(t))
  Px: Affine(Point.x-C.x,Point.y-C.y,C,Q);
  if distance(Px,Point) < distance(P,Point) then { Px: P }
  ArcCentre(Ao,Px,ccw_,rsq,Cox)
  ArcCentre(Ai,Px,ccw_,rsq,Cix)
     mark(C)
     mark(Ao)
     mark(Ai)
     mark(Ac)
     mark(Co)
     mark(Ci)
     mark(P)
  if $5==3 then {
    mark(Px)
    mark(Cox)
    mark(Cix)
    }
  ] with .C at $1.c }

.PE
