|
| 1 | +// By and copyright Julian D. A. Wiseman of www.jdawiseman.com, April 2025 |
| 2 | +// Released under GNU General Public License, Version 3, https://www.gnu.org/licenses/gpl-3.0.txt |
| 3 | +// smalls_SVG.c, in PenroseC |
| 4 | + |
| 5 | +#include "penrose.h" |
| 6 | + |
| 7 | +#define almostZero 5E-10 // consistent with %0.9lf |
| 8 | + |
| 9 | +char * svgTransform(char * const str, double const x, double const y, double const angDeg, short int angMod ) |
| 10 | +{ |
| 11 | + double angDegNew = angDeg; |
| 12 | + int8_t i; |
| 13 | + extern char scratchString[]; |
| 14 | + |
| 15 | + for( i = 0 ; i < 5 ; i++ ) // Finitely many in case a DBL_MAX has somehow crept in. Shouldn't happen, but... |
| 16 | + if( angDegNew < 0 ) |
| 17 | + angDegNew += angMod; |
| 18 | + else |
| 19 | + break; |
| 20 | + |
| 21 | + for( i = 0 ; i < 5 ; i++ ) |
| 22 | + if( angDegNew + almostZero >= angMod ) |
| 23 | + angDegNew -= angMod; |
| 24 | + else |
| 25 | + break; |
| 26 | + |
| 27 | + |
| 28 | + // Six cases: {Ang != 0, Ang == 0} * {Y != 0, Y == 0 but X != 0, both == 0} |
| 29 | + if( fabs(y) <= almostZero ) |
| 30 | + { |
| 31 | + if( fabs(angDegNew) <= almostZero ) |
| 32 | + { |
| 33 | + if( fabs(x) <= almostZero ) |
| 34 | + str[0] = 0; |
| 35 | + else |
| 36 | + sprintf(str, " transform='translate(%0.9lf)'", x); |
| 37 | + } // angDegNew == 0 |
| 38 | + else |
| 39 | + { |
| 40 | + if( fabs(x) <= almostZero ) |
| 41 | + sprintf(str, " transform='rotate(%0.9lf)'", angDegNew); |
| 42 | + else |
| 43 | + sprintf(str, " transform='translate(%0.9lf) rotate(%0.9lf)'", x, angDegNew); |
| 44 | + } // angDegNew != 0 |
| 45 | + } // y == 0 |
| 46 | + else |
| 47 | + { |
| 48 | + if( fabs(angDegNew) <= almostZero ) |
| 49 | + sprintf(str, " transform='translate(%0.9lf,%0.9lf)'", x, y); |
| 50 | + else |
| 51 | + sprintf(str, " transform='translate(%0.9lf,%0.9lf) rotate(%0.9lf)'", x, y, angDegNew); |
| 52 | + } // y != 0 |
| 53 | + |
| 54 | + stringClean(scratchString); |
| 55 | + return str; |
| 56 | +} // svgTransform() |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +// A is 'long' string; B is 'short' string. |
| 61 | +void exportColourSVG(char * const strA, char * const strB, bool * const isWhiteP, const Physique ph, const bool pathClosed, const long int pathLength, const bool pointy) |
| 62 | +{ |
| 63 | + strA[0] = 0; // default |
| 64 | + strB[0] = 0; // default |
| 65 | + *isWhiteP = false; // default |
| 66 | + |
| 67 | + if( Thin == ph ) |
| 68 | + { |
| 69 | + sprintf(strB, "#CCC"); |
| 70 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Thins: light grey -->", strB); |
| 71 | + return ; |
| 72 | + } |
| 73 | + |
| 74 | + if( pathClosed ) // Fat and closed |
| 75 | + { |
| 76 | + if( pathLength == 5 && pointy ) |
| 77 | + { |
| 78 | + sprintf(strB, "#9F9"); |
| 79 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed PathLength=5 pointy: light green -->", strB); |
| 80 | + return ; |
| 81 | + } // 5 pointy |
| 82 | + |
| 83 | + if( pathLength == 5 ) // so round |
| 84 | + { |
| 85 | + *isWhiteP = true; |
| 86 | + sprintf(strB, "#FFF"); |
| 87 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed PathLength=5 round: white -->", strB); |
| 88 | + return ; |
| 89 | + } // 5 round |
| 90 | + |
| 91 | + if( pathLength == 15 ) |
| 92 | + { |
| 93 | + sprintf(strB, "#004C00"); |
| 94 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength=15: very dark green -->", strB); |
| 95 | + return ; |
| 96 | + } // 15 closed |
| 97 | + |
| 98 | + if( pathLength == 25 ) |
| 99 | + { |
| 100 | + sprintf(strB, "#009"); |
| 101 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength=25: dark blue -->", strB); |
| 102 | + return ; |
| 103 | + } // 25 closed |
| 104 | + |
| 105 | + if( pathLength == 55 ) |
| 106 | + { |
| 107 | + sprintf(strB, "#00F"); |
| 108 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength=55: blue -->", strB); |
| 109 | + return ; |
| 110 | + } // 55 closed |
| 111 | + |
| 112 | + if( pathLength == 105 ) |
| 113 | + { |
| 114 | + sprintf(strB, "#F6F"); |
| 115 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength=105: pink -->", strB); |
| 116 | + return ; |
| 117 | + } // 105 closed |
| 118 | + |
| 119 | + if( pathLength == 215 ) |
| 120 | + { |
| 121 | + sprintf(strB, "#600000"); |
| 122 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength=215: maroon -->", strB); |
| 123 | + return ; |
| 124 | + } // 215 closed |
| 125 | + |
| 126 | + if( pathLength == 425 ) |
| 127 | + { |
| 128 | + sprintf(strB, "#090"); |
| 129 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength=425: dark green -->", strB); |
| 130 | + return ; |
| 131 | + } // 425 closed |
| 132 | + |
| 133 | + if( pathLength == 855 ) |
| 134 | + { |
| 135 | + sprintf(strB, "#09F"); |
| 136 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength=855: light blue -->", strB); |
| 137 | + return ; |
| 138 | + } // 855 closed |
| 139 | + |
| 140 | + if( pathLength == 1705 ) |
| 141 | + { |
| 142 | + sprintf(strB, "#F60"); |
| 143 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength=1705: orange -->", strB); |
| 144 | + return ; |
| 145 | + } // 1705 closed |
| 146 | + |
| 147 | + if( pathLength >= 3415 ) |
| 148 | + { |
| 149 | + sprintf(strB, "#191919"); |
| 150 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, closed, PathLength>=3415: very dark grey -->", strB); |
| 151 | + return ; |
| 152 | + } // >=3415 closed |
| 153 | + } // Closed |
| 154 | + else |
| 155 | + { |
| 156 | + if( pathLength >= 84 ) |
| 157 | + { |
| 158 | + sprintf(strB, "#%2X0000", 128 + ( 73 * ((int)floor(log2(pathLength) + 0.5)) ) % 128 ); // The +0.5 distinguishes the likes of 128 and 253. |
| 159 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, open, PathLength=%li: shades of red -->", strB, pathLength); |
| 160 | + return ; |
| 161 | + } // Open and long |
| 162 | + |
| 163 | + if( pathLength == 3 || pathLength == 4 ) |
| 164 | + { |
| 165 | + sprintf(strB, "#333"); |
| 166 | + sprintf(strA, "fill='%s' opacity='1' stroke='#000'> <!-- Fats, open, PathLength=%li: dark grey, to give whole tiling appearance of a serrated edge -->", strB, pathLength); |
| 167 | + return ; |
| 168 | + } // Open, 3 or 4, to give whole tiling appearance ofa serrated edge. |
| 169 | + |
| 170 | + *isWhiteP = true; |
| 171 | + sprintf(strB, "#FFF"); |
| 172 | + sprintf(strA, "fill='%s' opacity='1'> <!-- Fats, open PathLength=%li: white -->", strB, pathLength); |
| 173 | + return ; // Open, neither long nor 3 nor 4, boringly white. |
| 174 | + |
| 175 | + } // Open |
| 176 | +} // exportColourSVG() |
| 177 | + |
| 178 | + |
0 commit comments