00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef lint
00036 static const char rcsid[] =
00037 "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/tools/integrator.cc,v 1.8 1998/06/27 01:23:57 gnguyen Exp $";
00038 #endif
00039
00040 #include <stdlib.h>
00041 #include "integrator.h"
00042
00043 static class IntegratorClass : public TclClass {
00044 public:
00045 IntegratorClass() : TclClass("Integrator") {}
00046 TclObject* create(int, const char*const*) {
00047 return (new Integrator);
00048 }
00049 } integrator_class;
00050
00051 Integrator::Integrator() : lastx_(0.), lasty_(0.), sum_(0.)
00052 {
00053 bind("lastx_", &lastx_);
00054 bind("lasty_", &lasty_);
00055 bind("sum_", &sum_);
00056 }
00057
00058 void Integrator::set(double x, double y)
00059 {
00060 lastx_ = x;
00061 lasty_ = y;
00062 sum_ = 0.;
00063 }
00064
00065 void Integrator::newPoint(double x, double y)
00066 {
00067 sum_ += (x - lastx_) * lasty_;
00068 lastx_ = x;
00069 lasty_ = y;
00070 }
00071
00072 int Integrator::command(int argc, const char*const* argv)
00073 {
00074 if (argc == 2) {
00075 if (strcmp(argv[1], "reset") == 0) {
00076 set(0., 0.);
00077 return (TCL_OK);
00078 }
00079 } else if (argc == 4) {
00080 if (strcmp(argv[1], "newpoint") == 0) {
00081 double x = atof(argv[2]);
00082 double y = atof(argv[3]);
00083 newPoint(x, y);
00084 return (TCL_OK);
00085 }
00086 }
00087 return (TclObject::command(argc, argv));
00088 }
00089
00090
00091
00092
00093
00094 static class SamplesClass : public TclClass {
00095 public:
00096 SamplesClass() : TclClass("Samples") {}
00097 TclObject* create(int, const char*const*) {
00098 return (new Samples);
00099 }
00100 } samples_class;
00101
00102 int Samples::command(int argc, const char*const* argv)
00103 {
00104 if (argc == 2) {
00105 if (strcmp(argv[1], "mean") == 0) {
00106 if (cnt_ > 0) {
00107 Tcl::instance().resultf("%g", mean());
00108 return (TCL_OK);
00109 }
00110 Tcl::instance().resultf("tried to take mean with no sample points");
00111 return (TCL_ERROR);
00112 }
00113 if (strcmp(argv[1], "cnt") == 0) {
00114 Tcl::instance().resultf("%u", cnt());
00115 return (TCL_OK);
00116 }
00117 if (strcmp(argv[1], "variance") == 0) {
00118 if (cnt_ == 1) {
00119 Tcl::instance().resultf("0.0");
00120 return (TCL_OK);
00121 }
00122 if (cnt_ > 2) {
00123 Tcl::instance().resultf("%g", variance());
00124 return (TCL_OK);
00125 }
00126 return (TCL_ERROR);
00127 }
00128 if (strcmp(argv[1], "reset") == 0) {
00129 reset();
00130 return (TCL_OK);
00131 }
00132 } else if ( argc == 3 ) {
00133 if ( strcmp(argv[1],"newpoint") == 0 ) {
00134 double x = atof(argv[2]);
00135 newPoint(x);
00136 return (TCL_OK);
00137 }
00138 }
00139 return (TclObject::command(argc, argv));
00140 }