34 void setX(
double x_input) {
x.val = x_input; }
42 void eval(
double* result) { result[0] = val; }
44 void eval(
const char** result) {}
52 if (name ==
"x")
return &
x;
57int main(
int argc,
char* argv[]) {
66 GrapherExpr
expr(exprStr);
68 if (!
expr.isValid()) {
69 std::cerr <<
"expression failed " <<
expr.parseError() << std::endl;
71 }
else if (!
expr.returnType().isFP(1)) {
73 <<
expr.returnType().toString() << std::endl;
77 double xmin = -10, xmax = 10, ymin = -10, ymax = 10;
79 char* buffer =
new char[w * h];
80 memset(buffer, (
int)
' ', w * h);
83 int j_zero = (-ymin) / (ymax - ymin) * h;
84 if (j_zero >= 0 && j_zero < h) {
85 for (
int i = 0; i < w; i++) {
86 buffer[i + j_zero * w] =
'-';
90 int i_zero = (-xmin) / (xmax - xmin) * w;
91 if (i_zero >= 0 && i_zero < w) {
92 for (
int j = 0; j < h; j++) {
93 buffer[i_zero + j * w] =
'|';
98 const int samplesPerPixel = 10;
100 for (
int i = 0; i < w; i++) {
101 for (
int sample = 0; sample < samplesPerPixel; sample++) {
104 double x = double(dx + i) / double(w) * (xmax - xmin) + xmin;
107 const double* val =
expr.evalFP();
113 int j = (
y - ymin) / (ymax - ymin) * h;
115 if (j >= 0 && j < h) buffer[i + j * w] =
'#';
120 for (
int j = h - 1; j >= 0; j--) {
121 for (
int i = 0; i < w; i++) {
122 std::cout << buffer[i + j * w];
124 std::cout << std::endl;
virtual void eval(ArgHandle args)
int main(int argc, char *argv[])
ExprType & FP(int d)
Mutate this into a floating point type of dimension d.
std::string toString() const
Stringify the type into a printable string.
ExprType & Varying()
Mutate this into a varying lifetime.
abstract class for implementing variable references
virtual ExprVarRef * resolveVar(const std::string &name) const
</pre >< h3 > Binding our variable reference</h3 > If we now tried to use the variable would still not be found by our expressions To make it bindable we need to override the resolveVar() function as follows</pre >< h3 > Variable setting</h3 > Next we need to make a way of setting the variable As the controlling code will use the expression it will repeatedly alternate between setting the independent variables that are used and calling evaluate(). What it has to do depends very much on the application. In this case we only need to set the independent variable x as</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")
const double one_over_samples_per_pixel
</pre >< h3 > A simple variable reference</h3 > This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this we only need x
This is the same as the prman cellnoise function< br ></div >< br > float< b > float y< br > float< b > float y