
!if $click_x=$empty or $click_y=$empty
 !exit
!endif

cl_x=$click_x
cl_y=$click_y
click_x=$[$click_x/$prop+$stx]
click_y=$[$click_y/$prop+$sty]
!if NaN isin $click_x$click_y
 error=bad_click
 !exit
!endif

norandom=
rcl=$[$randomclose*2]
tol1=$[$tolerance1/$prop]
tol2=$[$tolerance2/$prop]
!distribute item ,, into near,far,hidden
!for i=1 to $pointcnt
 h=!char $i of $hidepoint
 l=!line $i of $points
 !distribute item $l into px,py,c
 dist=$[sqrt(($click_x-($px))^2+($click_y-($py))^2)]
 !if $dist<$tol1*1.25 and $h=+
  near=!append item $i to $near
 !endif
 !if $dist<$tol2*1.25
  hidden=!append item $i to $hidden
  !if $h=+
   far=!append item $i to $far
  !endif
  !if $dist<$rcl
   norandom=point $i
  !endif
 !endif
!next i
n=!itemcnt $near
!if $n>1 or $near!=$far
 error=ambiguous_click
 ambiguous=point
 !exit
!endif
!if $near!=$empty
 !if l isin $click1 or c isin $click1
  error=mixt_click
  !exit
 !endif
 !if $preclick!=$empty
  !if $clickask!=point
   error=curve_please
   !exit
  !endif
  !if $clickgot=1
   parm2=$near
   parm1=!item 2 of $clickgot1
  !else
   parm1=$near
   parm2=!item 2 of $clickgot2
   clickgot=1
  !endif
  request=$preclick
  clickback=yes
  !exit
 !endif
 !if $click1=$empty
  click1=$near
  !exit
 !endif
 !if $near!=$click1
  click2=$near
 !endif
 !exit
!endif

!distribute item , into near,far
!for i=1 to $linecnt
  h=!char $i of $hideline
  l=!line $i of $lineid
  !distribute item $l into a,b,c
  disc=$[sqrt(($a)^2+($b)^2)]
  dist=$[abs(($a)*($click_x)+($b)*($click_y)+$c)/$disc]
  !if $dist<$tol1 and $h=+
   near=!append item l$i to $near
  !endif
  !if $dist<$tol2
    hidden=!append item l$i to $hidden
    !if $h=+
     far=!append item l$i to $far
    !endif
  !endif
  !if $dist<$rcl
   norandom=line $i
  !endif
!next i
!for i=1 to $circlecnt
  h=!char $i of $hidecircle
  l=!line $i of $circles
  !distribute item $l into x,y,r
  dist=$[abs(sqrt(($click_x-($x))^2+($click_y-($y))^2)-$r)]
  !if $dist<$tol1 and $h=+
   near=!append item c$i to $near
  !endif
  !if $dist<$tol2
   hidden=!append item c$i to $hidden
   !if $h=+
    far=!append item c$i to $far
   !endif
  !endif
  !if $dist<$rcl
   norandom=circle $i
  !endif
!next i

!if $near!=$far
 error=ambiguous_click
 ambiguous=curve
 !exit
!endif
!if $preclick!=$empty
  !if $clickask!=curve
   error=point_please
   !exit
  !endif
  !if , isin $near
   error=ambiguous_click
   !exit
  !endif
  !if $near=$empty
   error=$(clickask)_please
   !exit
  !endif
  nl=line
  nc=circle
  num=!char 2 to -1 of $near
  typ=!char 1 of $near
  typ=$(n$typ)
  !if $clickgot=1
   parm2=$num
   minor2=$typ
   !distribute item $clickgot1 into minor1,parm1
  !else
   parm1=$num
   minor1=$typ
   !distribute item $clickgot2 into minor2,parm2
   clickgot=1
  !endif
  request=$preclick
  clickback=yes
  !exit
!endif
!if ($near!=$empty and $click1!=$empty and l notin $click1 and c notin $click1)\
	or ($near=$empty and $click1!=$empty)
 error=mixt_click
 !exit
!endif
!if $click1!=$empty
 near=!append item $click1 to $near
 click1=
!else
 near=!item 1,2 of $near
!endif
n=!itemcnt $near
!if $n>2
 error=ambiguous_click
 ambiguous=curve
 !exit
!endif
!ifval $n=2
 !distribute item ,, into randp,click1,click2
 com=!replace l by line, in $near
 com=!replace c by circle, in $com
 !distribute item $com into minor1,parm1,minor2,parm2
 !read pari_header
 !changeto proc/point
!endif
!ifval $n=1
 click1=$near
 !exit
!endif

# random
!if $near=$empty
 !if random notitemof $special
  error=random_noallow
  !exit
 !endif
 !if $hidden!=$empty
  error=false_free
  !exit
 !endif
 !if $pointcnt>=$objlimit
  error=too_many
  !exit
 !endif
 !if $norandom!=$empty
  error=random_close
  !exit
 !endif
 randp=$click_x,$click_y
 randclick=yes
!endif

