24 GrapherExpr(
const std::string &expr)
30 void setX(
double x_input)
44 void eval(
double *result)
override
49 void eval(
const char **)
override
59 ExprVarRef *resolveVar(
const std::string &name)
const override
67 int main(
int argc,
char *argv[])
77 GrapherExpr expr(exprStr);
79 if (!expr.isValid()) {
80 std::cerr <<
"expression failed " << expr.parseError() << std::endl;
82 }
else if (!expr.returnType().isFP(1)) {
83 std::cerr <<
"Expected expression of type " <<
ExprType().
FP(1).
Varying().
toString() <<
" got " << expr.returnType().toString() << std::endl;
93 auto buffer = std::vector<char>(w * h,
' ');
96 int j_zero =
static_cast<int>((-ymin) / (ymax - ymin) * h);
97 if (j_zero >= 0 && j_zero < h) {
98 for (
int i = 0; i < w; i++) {
99 buffer[i + j_zero * w] =
'-';
103 int i_zero =
static_cast<int>((-xmin) / (xmax - xmin) * w);
104 if (i_zero >= 0 && i_zero < w) {
105 for (
int j = 0; j < h; j++) {
106 buffer[i_zero + j * w] =
'|';
111 const int samplesPerPixel = 10;
112 const double one_over_samples_per_pixel = 1. / samplesPerPixel;
113 for (
int i = 0; i < w; i++) {
114 for (
int sample = 0; sample < samplesPerPixel; sample++) {
116 double dx = double(sample) * one_over_samples_per_pixel;
117 double x = double(dx + i) / double(w) * (xmax - xmin) + xmin;
120 const double *val = expr.evalFP();
126 int j =
static_cast<int>((y - ymin) / (ymax - ymin) * h);
129 buffer[i + j * w] =
'#';
134 for (
int j = h - 1; j >= 0; j--) {
135 for (
int i = 0; i < w; i++) {
136 std::cout << buffer[i + j * w];
138 std::cout << std::endl;
void eval(ArgHandle args) override
int main(int argc, char *argv[])
ExprType & Varying()
Mutate this into a varying lifetime.
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.
abstract class for implementing variable references