/* Point_2d_jgraph.cpp - Create a pretty jgraph for viewing 2d "points" problems. James S. Plank CS494/594 Lecture notes. Fri May 15 16:38:07 EDT 2020 */ #include "points.hpp" #include #include #include #include #include #include #include using namespace std; typedef std::runtime_error SRE; int main(int argc, char **argv) { Points points; vector x; double d; double granularity; double max; size_t i; if (argc != 2) { fprintf(stderr, "usage: points_2d_jgraph granularity - points on stdin\n"); exit(1); } if (sscanf(argv[1], "%lf", &granularity) != 1) { fprintf(stderr, "Bad granularity\n"); exit(0); } /* Read the points from stdin. */ try { points.Read(cin); if (points.p[0].size() != 2) throw SRE("Points have to be 2D points\n"); } catch (const SRE &e) { fprintf(stderr, "Error reading points on stdin:\n%s\n", e.what()); exit(1); } /* Emit the jgraph axes -- let the user customize later */ printf("newgraph\n"); printf("xaxis min 0 max 10\n"); printf("yaxis min 0 max 10\n"); printf("clip\n"); /* I know this is really inefficient, but so be it. Run through all of the points that you're going to graph, and find the one that optimizes the equation. */ max = 0; x.resize(2); for (x[0] = 1; x[0] <= 9; x[0] += granularity) { for (x[1] = 1; x[1] <= 9; x[1] += granularity) { d = points.Min_Distance(x); if (d > max) max = d; } } /* Run though the points and plot them where 0 = white and max = red */ printf("curve 0 marktype box marksize %lg %lg\n", granularity, granularity); for (x[0] = 1; x[0] <= 9; x[0] += granularity) { for (x[1] = 1; x[1] <= 9; x[1] += granularity) { d = points.Min_Distance(x); printf("copycurve 0 color 1 %lg %lg pts %lg %lg\n", 1 - d / max, 1 - d/max, x[0], x[1]); } } /* Now plot the points themselves */ printf("newcurve marktype circle pts\n"); for (i = 0; i < points.p.size(); i++) printf("%lg %lg\n", points.p[i][0], points.p[i][1]); return 0; }