Posted to tcl by evilotto at Wed Jul 19 21:45:48 GMT 2017view raw

  1. set plotsize 7
  2. proc textplot {values} {
  3. set xvals [lmap {x y} $values {set x}]
  4. set yvals [lmap {x y} $values {set y}]
  5. set minx [tcl::mathfunc::min {*}$xvals]
  6. set miny [tcl::mathfunc::min 0 {*}$yvals]
  7. set maxx [tcl::mathfunc::max {*}$xvals]
  8. set maxy [tcl::mathfunc::max {*}$yvals]
  9.  
  10. set maxx [expr {$maxx + 1}]
  11. set maxy [expr {$maxy + 1}]
  12.  
  13. # 10x10 grid
  14. set grid [lrepeat $::plotsize [lrepeat $::plotsize { }]]
  15.  
  16. foreach {x y} $values {
  17. set gx [expr {int((double($x) - $minx) / (double($maxx) - $minx) * $::plotsize)}]
  18. set gy [expr {int((double($y) - $miny) / (double($maxy) - $miny) * $::plotsize)}]
  19. # puts "$gx $gy "
  20. for {set _y 0} {$_y <= $gy} {incr _y} {
  21. lset grid $_y $gx "**"
  22. }
  23. }
  24.  
  25. foreach row [lreverse $grid] {
  26. puts "|[join $row ""]"
  27. }
  28. puts "+[string repeat "--" $::plotsize]"
  29. }
  30.