Loading...
Logo
Processing Forum

South Korea flag

in Share your Work  •  2 years ago  
Reference of implementation:
http://en.wikipedia.org/wiki/Flag_of_South_Korea

Copy code
  1. // Base length, to vary to change sketch's size
  2. final int BASE = 240;
  3. // Constants below come from the official flag definition
  4. final int BAR_HEIGHT = BASE / 12;
  5. final int LONG_BAR_WIDTH = BASE / 2;
  6. final int SHORT_BAR_VERTICAL_INTERVAL = BASE / 24;
  7. final int SHORT_BAR_WIDTH = (LONG_BAR_WIDTH - SHORT_BAR_VERTICAL_INTERVAL) / 2;
  8. final int BAR_VERTICAL_INTERVAL = BASE / 24;
  9. final int BAR_INTERVAL = BAR_HEIGHT + BAR_VERTICAL_INTERVAL;
  10. final float DISTANCE_TRIGRAM_CENTER = BASE * (1.0 / 2 + 1.0 / 4);
  11.  
  12. // enums might be better here, but Processing doesn't seem to support them...
  13. final int LONG_BAR = 1;
  14. final int TWO_BARS = 2;
  15.  
  16. final int TOP_LEFT = 1;
  17. final int TOP_RIGHT = 2;
  18. final int BOTTOM_LEFT = 3;
  19. final int BOTTOM_RIGHT = 4;
  20.  
  21. final float baseAngle = atan(2.0 / 3.0);
  22. int flagCenterX, flagCenterY;
  23.  
  24. final boolean DEBUG = false;
  25.  
  26. // Draw bars with origin point at 0, 0 (top-left)
  27. void drawLongBar(int pos)
  28. {
  29.   rect(0, pos *  BAR_INTERVAL, LONG_BAR_WIDTH, BAR_HEIGHT);
  30. }
  31. void drawTwoBars(int pos)
  32. {
  33.   rect(0, pos * BAR_INTERVAL, SHORT_BAR_WIDTH, BAR_HEIGHT);
  34.   rect(SHORT_BAR_WIDTH + SHORT_BAR_VERTICAL_INTERVAL, pos * BAR_INTERVAL, SHORT_BAR_WIDTH, BAR_HEIGHT);
  35. }
  36. void drawTrigram(int trigramPosition, int... types)
  37. {
  38.   // Save transformation state
  39.   pushMatrix();
  40.  
  41.   float angle = 0;
  42.   switch (trigramPosition)
  43.   {
  44.   case TOP_LEFT:
  45.     angle = baseAngle - HALF_PI;
  46.     break;
  47.   case TOP_RIGHT:
  48.     angle = HALF_PI - baseAngle;
  49.     break;
  50.   case BOTTOM_LEFT:
  51.     angle = -HALF_PI - baseAngle;
  52.     break;
  53.   case BOTTOM_RIGHT:
  54.     angle = HALF_PI + baseAngle;
  55.     break;
  56.   default:
  57.     assert false : "Bad trigram position!";
  58.   }
  59.   final float posX = flagCenterX + DISTANCE_TRIGRAM_CENTER * sin(angle);
  60.   final float posY = flagCenterY - DISTANCE_TRIGRAM_CENTER * cos(angle);
  61.   if (DEBUG)
  62.   {
  63.     println(degrees(angle));
  64.     println(cos(angle) + " " + sin(angle));
  65.     println(posX + " " + posY);
  66.   }
  67.  
  68.   // Put in place
  69.   translate(posX, posY);
  70.   // Do the rotation
  71.   rotate(angle);
  72.   // Compensate to rotate relative to the bottom-center
  73.   translate(-LONG_BAR_WIDTH / 2, -BASE / 3);
  74.  
  75.   // Draw the figure
  76.   for (int i = 0; i < types.length; i++)
  77.   {
  78.     if (types[i] == LONG_BAR)
  79.     {
  80.       drawLongBar(i);
  81.     }
  82.     else
  83.     {
  84.       drawTwoBars(i);
  85.     }
  86.   }
  87.  
  88.   // Restore transformation state
  89.   popMatrix();
  90. }
  91.  
  92. void drawTaegeuk()
  93. {
  94.   color red = #C60C30; // Official red
  95.   color blue = #003478; // Official blue
  96.  
  97.   pushMatrix();
  98.  
  99.   translate(flagCenterX, flagCenterY);
  100.   rotate(baseAngle);
  101.  
  102.   fill(blue);
  103.   arc(0, 0, BASE, BASE, 0, PI);
  104.   fill(red);
  105.   arc(0, 0, BASE, BASE, -PI, 0);
  106.   fill(blue);
  107.   ellipse(BASE / 4, 0, BASE / 2, BASE / 2);
  108.   fill(red);
  109.   ellipse(-BASE / 4, 0, BASE / 2, BASE / 2);
  110.  
  111.   popMatrix();
  112. }
  113.  
  114. void setup()
  115. {
  116.   size(800, 600);
  117.   background(100);
  118.   smooth();
  119.  
  120.   flagCenterX = width / 2;
  121.   flagCenterY = height / 2;
  122.  
  123.   // Official ratio: 2 x 3
  124.   int flagWidth  = 3 * BASE;
  125.   int flagHeight = 2 * BASE;
  126.  
  127.   int posX = flagCenterX - flagWidth / 2;
  128.   int posY = flagCenterY - flagHeight / 2;
  129.  
  130.   noStroke();
  131.   fill(255);
  132.   rect(posX, posY, flagWidth, flagHeight);
  133.   fill(0);
  134.   // first
  135.   drawTrigram(TOP_LEFT, LONG_BAR, LONG_BAR, LONG_BAR);
  136.   // second
  137.   drawTrigram(TOP_RIGHT, TWO_BARS, LONG_BAR, TWO_BARS);
  138.   // third
  139.   drawTrigram(BOTTOM_LEFT, LONG_BAR, TWO_BARS, LONG_BAR);
  140.   // fourth
  141.   drawTrigram(BOTTOM_RIGHT, TWO_BARS, TWO_BARS, TWO_BARS);
  142.   // Center
  143.   drawTaegeuk();
  144.  
  145.   if (DEBUG)
  146.   {
  147.     stroke(#FF8800);
  148.     line(posX, posY, posX + flagWidth - 1, posY + flagHeight - 1);
  149.     line(posX,  posY + flagHeight - 1, posX + flagWidth - 1, posY);
  150.   }
  151. }
You can also find the code at:
http://bazaar.launchpad.net/~philho/+junk/Processing/files/head:/_SmallPrograms/KoreanFlag/

Inspirational thread: how can I draw something like this?