Plot function graphs

By HeilTec on Sep 03, 2012

Screenshots

Preliminary experimental - plotting function graph in a picture window..

I know that much more efficient ways exist using some of the free-ware programs available. I just wanted to do it in mIRC. Call it an exercise in MSL. Call it a bad whim. Still I hope some may find interest in this.

TODO: More optimized looping. MSL is slow, but this can run a lot faster (I'm sure)

Some strangeness about seeing differences between the use of drawdot vs. drawline (off by a pixel)

NOTE: This has as yet no formula parser. The function must be passed as a valid mIRC expression using a pre-chosen variable named $x (a script actually)

(On the command-line the ! escapes in the examples are not needed.)
So just enter like /plot $sin($x)
calls to $calc() are needed inside functions parameters if it contains expresions.

; Plotter

; F(x) plot

alias plot_init {
  if (!$window(@plot)) window -pk0e @plot 50 50 450 450
  if (!$hget(plot)) hmake plot 10
  hadd plot scale 100 100
  hadd plot offset 210 210
  hadd plot size 180 180
  hadd plot axes $rgb(255,255,255) 2
  hadd plot curve $rgb(127,255,127) 1
}

alias plot {
  if ($0 == 0) {
    echo -ag usage: plot F($x)
  }
  else {
    if (!$hget(plot)) plot_init
    echo -sg plotting $1-
    drawsave -b32 @plot plotundo.bmp
    var %sinvs $calc($scale(1)*$sin($skew).deg)
    var %cosv $cos($skew).deg
    var %x = $calc($ofs(1) - $size(1))
    while (%x < $calc($ofs(1)+$size(1))) {
      hadd plot x $calc((%x -$ofs(1)) / $scale(1))
      var %y_ $calc( [ $1- ] )
      if (%y_ == $null) goto error
      var %xs $calc(%x - %sinvs * %y_ )
      var %ys $calc(%y_ * %cosv)
      var %y $calc($ofs(2) - %ys * $scale(2))
      if (%x < $calc($ofs(1)-$size(1)+4)) echo -sg skew $x %x %xs %y_ $calc( [ $1- ] ) %y %y_
      if ($hget(plot,y_1)) { 
        ;&& ($calc(($hget(plot,y_1) - %y)^2) > 4)) {
        drawline -nr @plot $curve $hget(plot,x_1) $hget(plot,y_1) %xs %y
      }
      ;else drawdot -r @plot $curve $calc(%x +1) $calc(%y +1)
      hadd plot y_1 %y
      hadd plot x_1 %xs
      if ($hget(plot,drop) && 10 // %x) {
        drawline -nr @plot $curve $calc(%x -0) 210 $round(%xs +.5) $round($calc(%y +.5))
      }
      goto done

      :error
      hdel plot y_1
      echo -sg Error: $error
      reseterror

      :done
      inc %x
    }
    drawline @plot
    ;    hfree plot
    hdel plot y_1
  }
}

on *:close:@Plot: hfree plot

alias plot_clear { clear @plot | hfree plot }
alias plot_undo drawpic @plot 0 0 plotundo.bmp

;fast configurations access
alias -l axes return $hget(plot,axes)
alias -l curve return $hget(plot,curve)

; ofs,scale,size $1: 1->x; 2->y
alias -l ofs {
  return $gettok($hget(plot,offset),$1,32)
}
alias -l scale {
  return $gettok($hget(plot,scale),$1,32)
}
alias -l size {
  return $gettok($hget(plot,size),$1,32)
}
alias -l skew return $iif($hget(plot,skew),$v1,0)
alias -l x return $hget(plot,x)

; Demos / utils 
;--------------

alias plot_axes {
  if (!$hget(plot)) plot_init
  drawline -nr @Plot $axes $calc($ofs(1) - $size(1)) $ofs(2) $calc($ofs(1) + $size(1)) $ofs(2)
  drawline -nr @Plot $axes $ofs(1) $calc($ofs(2) - $size(2)) $ofs(1) $calc($ofs(2) + $size(2)) 
  drawline @plot
}
alias plot_cross {
  if (!$hget(plot)) plot_init
  plot $!x
  plot 0- $!x
  plot_axes
}
alias plot_hyper {
  if (!$hget(plot)) plot_init
  var %r 0.2
  while (%r <= 1.6) {
    plot    $!sqrt( $!calc( $!x ^ 2 - [ %r ] ^2) ) 
    plot 0- $!sqrt( $!calc( $!x ^ 2 - [ %r ] ^2) ) 
    inc %r 0.2
  }
}
alias plot_hyper_ {
  if (!$hget(plot)) plot_init
  var %r 0.2
  while (%r <= 1.6) {
    plot    $!sqrt( $!calc( $!x ^ 2 + [ %r ] ^2) ) 
    plot 0- $!sqrt( $!calc( $!x ^ 2 + [ %r ] ^2) ) 
    inc %r 0.2
  }
}

alias plot_wave {
  if (!$hget(plot)) plot_init
  hadd plot scale 50 25

  hadd plot curve $rgb(255,127,127) 1
  hadd plot drop $true
  plot $!sin($calc($x *1.75))

  hadd plot skew 135
  hadd plot curve $rgb(127,255,127) 1
  plot $!sin($calc($x *1.75))

  plot_axes
  drawline -r @plot $rgb(255,255,255) 2 $calc($ofs(1) - $size(1)) $calc($ofs(2) + $size(2)) $calc($ofs(1) + $size(1)) $calc($ofs(2) - $size(2))
  hdel plot skew
  hdel plot drop
}

Comments

Sign in to comment.
Sorasyn   -  Sep 03, 2012

Very nice.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.