nbonneel

SVG writer for polygons

May 6th, 2020
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.71 KB | None | 0 0
  1. // if the Polygon class name conflicts with a class in wingdi.h on Windows, use a namespace or change the name
  2. class Polygon {  
  3. public:
  4.     std::vector<Vector> vertices;
  5. }; 
  6.  
  7. // saves a static svg file. The polygon vertices are supposed to be in the range [0..1], and a canvas of size 1000x1000 is created
  8.     void save_svg(const std::vector<Polygon> &polygons, std::string filename, std::string fillcol = "none") {
  9.         FILE* f = fopen(filename.c_str(), "w+");
  10.         fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n");
  11.         for (int i=0; i<polygons.size(); i++) {
  12.             fprintf(f, "<g>\n");
  13.             fprintf(f, "<polygon points = \"");
  14.             for (int j = 0; j < polygons[i].vertices.size(); j++) {
  15.                 fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000 - polygons[i].vertices[j][1] * 1000));
  16.             }
  17.             fprintf(f, "\"\nfill = \"%s\" stroke = \"black\"/>\n", fillcol.c_str());
  18.             fprintf(f, "</g>\n");
  19.         }
  20.         fprintf(f, "</svg>\n");
  21.         fclose(f);
  22.     }
  23.  
  24.  
  25. // Adds one frame of an animated svg file. frameid is the frame number (between 0 and nbframes-1).
  26. // polygons is a list of polygons, describing the current frame.
  27. // The polygon vertices are supposed to be in the range [0..1], and a canvas of size 1000x1000 is created
  28.     void save_svg_animated(const std::vector<Polygon> &polygons, std::string filename, int frameid, int nbframes) {
  29.         FILE* f;
  30.         if (frameid == 0) {
  31.             f = fopen(filename.c_str(), "w+");
  32.             fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n");
  33.             fprintf(f, "<g>\n");
  34.         } else {
  35.             f = fopen(filename.c_str(), "a+");
  36.         }
  37.         fprintf(f, "<g>\n");
  38.         for (int i = 0; i < polygons.size(); i++) {
  39.             fprintf(f, "<polygon points = \"");
  40.             for (int j = 0; j < polygons[i].vertices.size(); j++) {
  41.                 fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000-polygons[i].vertices[j][1] * 1000));
  42.             }
  43.             fprintf(f, "\"\nfill = \"none\" stroke = \"black\"/>\n");
  44.         }
  45.         fprintf(f, "<animate\n");
  46.         fprintf(f, "    id = \"frame%u\"\n", frameid);
  47.         fprintf(f, "    attributeName = \"display\"\n");
  48.         fprintf(f, "    values = \"");
  49.         for (int j = 0; j < nbframes; j++) {
  50.             if (frameid == j) {
  51.                 fprintf(f, "inline");
  52.             } else {
  53.                 fprintf(f, "none");
  54.             }
  55.             fprintf(f, ";");
  56.         }
  57.         fprintf(f, "none\"\n    keyTimes = \"");
  58.         for (int j = 0; j < nbframes; j++) {
  59.             fprintf(f, "%2.3f", j / (double)(nbframes));
  60.             fprintf(f, ";");
  61.         }
  62.         fprintf(f, "1\"\n   dur = \"5s\"\n");
  63.         fprintf(f, "    begin = \"0s\"\n");
  64.         fprintf(f, "    repeatCount = \"indefinite\"/>\n");
  65.         fprintf(f, "</g>\n");
  66.         if (frameid == nbframes - 1) {
  67.             fprintf(f, "</g>\n");
  68.             fprintf(f, "</svg>\n");
  69.         }
  70.         fclose(f);
  71.     }
Add Comment
Please, Sign In to add comment