Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

yahoo logo polygon generator

a guest Jul 8th, 2011 2,666 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <vector>
  4.  
  5. // "Y!" logo
  6. float vertex[] = {
  7.   -52,-30,  // 0
  8.   -52,-25,
  9.   -7,14,    // 2
  10.   -7,34,
  11.   -23,34,
  12.   -23,39, // 5
  13.   27,39,
  14.   27,34,
  15.   9,34,
  16.   9,14,
  17.   39,-13, // 10
  18.   53,-16,
  19.   56,-21,
  20.   12,-21,
  21.   12,-16,
  22.   27,-14, // 15 (this one might want to move?)
  23.   5,6,    // 16 is the top vertex of the fork in the Y
  24.   -17,-24, // 17  is the y coord right??
  25.   -1,-26,  // 18
  26.   -1,-30,  // 19
  27.   -37,-24,  // 20 = 1.5 (oops)
  28.   106,-67,  // 21 (start of !)
  29.   106,26,
  30.   123,28,
  31.   146,-63,
  32.   102,40, // 25
  33.   100,60,
  34.   122,63,
  35.   125,43,
  36. };
  37.  
  38. float xscale = 0.300;
  39. float yscale = 0.50;
  40. int angle_quantum = 46;
  41. int offset_quantum = 47;
  42. float offset_max = 39.5*(offset_quantum)/(offset_quantum-1);
  43.  
  44. int polygon[] = {
  45.   0,1,20,17,18,19,0,-1,  // top left serif
  46.   20,2,16,17,20,-1,      // left diagonal
  47.   2,3,8,9,16,2,-1,       // middle vertical
  48. //  3,4,5,6,7,8,3,-1,      // bottom serif
  49.   3,4,5,6,7,3,-1,      // bottom serif (point 8 omitted as it's colinear with 7 and 3)
  50.   9,10,15,16,9,-1,       // right diagonal
  51.   10,11,12,13,14,15,10,-1, // top right serif
  52.   21,22,23,24,21,-1,     // top of !
  53.   25,26,27,28,25,-1,     // bottom of !
  54.   -2};
  55.  
  56. void dump_vector(const std::vector<int> &o, const char *var, int offset) {
  57.   printf("// %s=[\"", var, o.size());
  58.   for(int i=0;i<o.size();i++) {
  59.     int c = o[i] + offset;
  60.     if(c == '"' || c == '\\') putchar('\\');
  61.     putchar(c);
  62.   }
  63.   printf("\"]-%d\n", offset);
  64. }
  65.  
  66. int main()
  67. {
  68.   int i,j,pn=0;
  69.   std::vector<int> output_l, output_a, output_b;
  70.   for(j=0;polygon[j] != -2;pn++) {
  71.     float avgx=0, avgy=0;
  72.     for(i=0;polygon[j+i+1]!=-1;i++) {
  73.       int p0 = polygon[j+i],
  74.           p1 = polygon[j+i+1];
  75.       float x0 = vertex[p0*2]*xscale,
  76.             y0 = vertex[p0*2+1]*yscale,
  77.             x1 = vertex[p1*2]*xscale,
  78.             y1 = vertex[p1*2+1]*yscale,
  79.             dx = x1-x0,
  80.             dy = y1-y0;
  81.       avgx += x0;
  82.       avgy += y0;
  83.       float norm = sqrt(dx*dx + dy*dy);
  84.       // L(x,y) is the half-plane normal = line segment vector (dx,dy) rotated
  85.       // 90 degrees left (on a left-handed coordinate system)
  86.       float Lx = dy;
  87.       float Ly = -dx;
  88.       float angle = round(angle_quantum*atan2(Ly, Lx)/M_PI);
  89.       //if(angle<0) angle += 2*angle_quantum;  // positive angles only?  no need
  90.       // todo: quantize Lx,Ly
  91.       // just use sin/cos?  64 angles?
  92.       float Lx2 = cos(M_PI*angle/angle_quantum);
  93.       float Ly2 = sin(M_PI*angle/angle_quantum);
  94.       // calculate B based on center of segment so any angle errors are
  95.       // propagated equally through the segment
  96.       float B = Lx2*(x0+x1)/2+Ly2*(y0+y1)/2;
  97.       float B2 = ceil(offset_quantum*B/offset_max); // hmm, we need to slightly inflate this away from the polygon centroid but how?
  98.       // round B2 such that ((x0+x1)/2, (y0+y1)/2) is in the interior
  99.       float B2scaled = offset_max*B2/offset_quantum;
  100.       if(Lx2*(x0+x1)/2 + Ly2*(y0+y1)/2 < B2scaled) {
  101.         B2--;
  102.       }
  103.  
  104.       if(B2 == offset_quantum) B2--;
  105.       B2scaled = offset_max*B2/offset_quantum;
  106.       float ax = avgx/(1+i);
  107.       float ay = avgy/(1+i);
  108.       printf("// polygon %d segment %d (%d-%d): L=(%g,%g) B=%g check(%g,%g)=%g\n", pn, i, p0,p1, Lx,Ly,B, ax/xscale,ay/yscale, ax*Lx+ay*Ly-B);
  109.       //printf("a=%g,(%g,%g)->(%g,%g),%g,\n", angle, Lx/norm,Ly/norm, Lx2,Ly2,B);
  110.       printf("// %d,%d,\n", (int)angle, (int)B2);
  111.       int aa = (int) angle;
  112.       int bb = (int) B2; // -32 to 31
  113.       int e1 = aa, e2 = -bb;
  114.       output_a.push_back(e1);
  115.       output_b.push_back(e2);
  116.       printf("%g,%g,%g,\n", Lx2,Ly2,B2scaled);
  117.     }
  118.     output_l.push_back(output_a.size());
  119.     printf("-99,\n");
  120.     j += i+2;
  121.   }
  122.   printf("-98\n");
  123.   printf("// anglescale = %g ^-1 = %g\n", M_PI/angle_quantum, angle_quantum/M_PI);
  124.   printf("// offsetscale = %g ^-1 = %g\n", offset_max/offset_quantum, offset_quantum/offset_max);
  125.  
  126.   dump_vector(output_l, "length", 50);
  127.   dump_vector(output_a, "a", 79);
  128.   dump_vector(output_b, "b", 79);
  129.   printf("// total length=%d\n", output_a.size());
  130. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top