alias rf {
  window -pkd @rf 100 100 600 360
  clear @rf
  rf.drawall
}
alias rf.axes {
  drawline @rf 12 1 0 180 360 180
  drawline @rf 12 1 180 0 180 360
}
alias rf.drawall {
  clear @rf
  rf.axes
  rf.drawforces
  rf.drawarc
}
alias rf.drawarc {
  if (%res.i >= 0) && (%res.j >= 0) {
    %a = 90
    %inc = -1
  }
  if (%res.i < 0) && (%res.j >= 0) {
    %a = 270
    %inc = 1
  }
  if (%res.i >= 0) && (%res.j < 0) {
    %a = 90
    %inc = 1
  }
  if (%res.i < 0) && (%res.j < 0) {
    %a = 270
    %inc = -1
  }
  %a = $calc(%a - 90)
  %loop = 0
  %r = $calc(%res.magnitude * 360 / (%xscale + %yscale) / 2)
  while (%loop <= $int(%res.angle)) {
    %a = $calc(%a + %inc)
    titlebar %a : %loop
    %rad = $calc(%a / 180 * $pi)
    %x = $calc(%orig.x + %r * $cos(%rad))
    %y = $calc(%orig.y + %r * $sin(%rad))
    drawdot @rf 4 1 %x %y
    inc %loop
  }
}
alias rf.drawforces {
  ; Find the right scale
  %loop = 0
  %t = $lines(forces.txt)
  %max.x = 0
  %max.y = 0
  %res.i = 0
  %res.j = 0
  while (%loop < %t) {
    inc %loop
    %read = $read(forces.txt,ln,%loop)
    %i = $gettok(%read,1,$asc(;))
    %j = $gettok(%read,2,$asc(;))
    if ($abs(%i) > %max.x) %max.x = $abs(%i)
    if ($abs(%j) > %max.y) %max.y = $abs(%j)
    %res.i = $calc(%res.i + %i)
    %res.j = $calc(%res.j + %j)
  }
  %res.magnitude = $sqrt($calc(%res.i ^2 + %res.j ^2))
  if ($abs(%res.i) > %max.x) %max.x = $abs(%res.i)
  if ($abs(%res.j) > %max.y) %max.y = $abs(%res.j)
  %xscale = $calc(%max.x * 2 + 5)
  %yscale = $calc(%max.y * 2 + 5)
  if (%xscale > %yscale) %yscale = %xscale
  if (%yscale > %xscale) %xscale = %yscale
  %loop = 0
  %t = $lines(forces.txt)
  if (!%userpos) || (%userpos <= 0) %userpos = 1
  if (%userpos > %t) %userpos = %t
  while (%loop < %t) {
    inc %loop
    %read = $read(forces.txt,ln,%loop)
    %i = $gettok(%read,1,$asc(;))
    %j = $gettok(%read,2,$asc(;))
    %temp = $rf.pointconvert(%i,%j)
    %i.pos = $gettok(%temp,1,$asc(;))
    %j.pos = $gettok(%temp,2,$asc(;))
    drawline @rf $iif(%userpos == %loop,14,0) 1 %orig.x %orig.y %i.pos %j.pos
  }
  %temp = $rf.pointconvert(%res.i,%res.j)
  %i.pos = $gettok(%temp,1,$asc(;))
  %j.pos = $gettok(%temp,2,$asc(;))
  drawline @rf 4 1 %orig.x %orig.y %i.pos %j.pos
  drawtext @rf 4 380 10 Forces:
  %textpos = 10
  %loop = 0
  while (%loop < %t) {
    inc %loop
    %read = $read(forces.txt,ln,%loop)
    %i = $gettok(%read,1,$asc(;))
    %j = $gettok(%read,2,$asc(;))
    %write = %i $+ i + %j $+ j
    if (%userpos == %loop) drawrect -f @rf 4 1 450 %textpos $width(%write,fixedsys,10) 15
    drawtext @rf 0 450 %textpos %write
    inc %textpos 15
  }
  ;Find the angle from i-axis
  ;tan(a) = opp/adj
  %res.angle = $atan($calc($abs(%res.j) / $abs(%res.i)))
  %res.angle = $calc(%res.angle * 180 / $pi)
  inc %textpos 15
  drawtext @rf 4 380 %textpos Resultant Force
  inc %textpos 30
  %write = %res.i $+ i + %res.j $+ j
  drawtext @rf 4 380 %textpos Vector:
  drawtext @rf 0 450 %textpos %write
  inc %textpos 15
  %write = $round(%res.magnitude,$calc(3 - $len($int(%res.magnitude)))) $+ N
  drawtext @rf 4 356 %textpos Magnitude:
  drawtext @rf 0 450 %textpos %write
  inc %textpos 15
  %write = $round(%res.angle,$calc(3 - $len($int(%res.magnitude))))
  drawtext @rf 4 390 %textpos Angle:
  drawtext @rf 0 450 %textpos %write $+ $chr(176)
  inc %textpos 30
  if (%t == 0) || (%t == $null) drawtext @rf 9 390 %textpos No forces.
  inc %textpos 15
  if (%t == 0) || (%t == $null) drawtext @rf 9 390 %textpos Right click to add some.

}
alias rf.pointconvert {
  %x = $1
  %y = $2
  %orig.x = 180
  %orig.y = 180
  %new.x = $calc(%orig.x + (%x * 360/ %xscale))
  %new.y = $calc(%orig.y - (%y * 360/ %yscale))
  return %new.x $+ ; $+ %new.y
}
menu @rf {
  Add force: {
    %temp.i = $?="i component?"
    %temp.j = $?="j component?"
    if (%temp.i !isnum) || (%temp.j !isnum) halt
    write forces.txt %temp.i $+ ; $+ %temp.j
    rf.drawall
  }
  Edit selected force: {
    %temp.i = $?="i component?"
    %temp.j = $?="j component?"
    if (%temp.i !isnum) || (%temp.j !isnum) halt
    write -l $+ %userpos forces.txt %temp.i $+ ; $+ %temp.j
    rf.drawall
  }
  Delete selected force: {
    write -dl $+ %userpos forces.txt
    rf.drawall
  }
  Delete all forces: {
    .remove forces.txt
    rf.drawall
  }
}
on *:keydown:@rf:*: {
  if ($keyval == 38) {
    dec %userpos
    rf.drawall
  }
  if ($keyval == 40) {
    inc %userpos
    rf.drawall
  }
}
