Advertisement
Guest User

Creative Coding w5_05_a

a guest
Sep 2nd, 2015
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. /*
  2.  * Creative Coding
  3.  * Week 5, 05 - Text agents
  4.  * by Indae Hwang and Jon McCormack
  5.  * Copyright (c) 2014 Monash University
  6.  *
  7.  * This sketch creates a simple agent-based simulation using text and objects
  8.  * The sketch reads in characters from the keyboard and dynamically creates
  9.  * new objects for each character.
  10.  *
  11.  * (c) 2015 Giles Cartmel
  12.  * Changes to animate characters towards target points in the order
  13.  * they are typed, adding of colours and other changes to special effects.
  14.  *
  15.  */
  16. ArrayList<AniCharacter> aniChars;
  17.  
  18. PFont myFont;
  19.  
  20. int currentCount;
  21. boolean saveFrames = false;
  22. int frameNo = 0;
  23.  
  24. void setup() {
  25.   size(700, 700);
  26.   myFont = loadFont("HelveticaNeue-UltraLight-200.vlw");
  27.   textFont(myFont);
  28.   //textMode(SHAPE);
  29.   aniChars = new ArrayList<AniCharacter>();
  30.   smooth(8); // enable antialiasing
  31.   colorMode(HSB,360,100,100);  
  32. }
  33.  
  34.  
  35. void draw() {
  36.   background(0,0,0);
  37.  
  38.   for (int i = aniChars.size()-1; i >= 0; i--) {
  39.     AniCharacter tempObj = aniChars.get(i);
  40.     tempObj.run(aniChars.size()); // run each char
  41.   }
  42.  
  43.   // if save frames is turned on (toggled with mouse click)
  44.   // then save every 10th frame
  45. //  if (saveFrames && frameCount%10 == 0) {
  46. //    saveFrame("frames/"+str(++frameNo)+".jpg");
  47. //  }
  48. }
  49.  
  50. void keyReleased() {
  51.   // CTRL-H to "backspace" the characters.
  52.   if (int(key) != 65535) { // prevent SHIFT key causing glyph error by being treated as a character
  53.     if (8 == int(key) && aniChars.size() > 0) {
  54.       aniChars.remove(aniChars.size()-1);
  55.     } else {
  56.       aniChars.add( new AniCharacter(random(width), random(height), key, aniChars.size()) );
  57.     }
  58.   }
  59. }
  60.  
  61. void mouseReleased() {
  62.   saveFrames = !saveFrames;
  63.   println("Saving Frames: "+saveFrames);
  64. }
  65.  
  66. // AniCharacter class
  67. //
  68. //
  69. class AniCharacter {
  70.   float x, y;
  71.   float targX, targY;
  72.   float xSpeed, ySpeed;
  73.   float dynamic;
  74.   float dynamic_c;
  75.   float size_font;
  76.   float size_shape, randSize1, randSize2;
  77.   char letter;
  78.   int  index;
  79.   color fontcolour;
  80.  
  81.   // Constructor
  82.   AniCharacter(float x_, float y_, char c_, int i_) {
  83.     x = x_;
  84.     y = y_;
  85.     targX = x;
  86.     targY = y;
  87.     xSpeed = 0;
  88.     ySpeed = 0;
  89.  
  90.     letter = c_;
  91.     index = i_;
  92.     println("Letter["+index+"]: "+letter);
  93.     size_font = random(50, 200);
  94.     size_shape = size_font + random(10, 50);
  95.     randSize1 = random(0.5,1.0);
  96.     randSize2 = random(0.5,1.5);
  97.     dynamic_c = random(-0.1, 0.1);
  98.     fontcolour = color(random(360),50+random(50), 50+random(50), 100+random(155));
  99.   }
  100.  
  101.   void run(int letters) {
  102.     // based on the number of letters determine how many 'grid' positions we need to place
  103.     // all the letters, and move the letters in the direction of their calculated position
  104.     float rowcols = (float) (sqrt(letters)+1); // determine how many cols/rows to have
  105.     // determine the target Position
  106.     float newTargX = lerp(0, width-(width/20), (float) ((index%rowcols)+1)/(rowcols+1));
  107.     float newTargY = lerp(height/20, height+(height/20), (float) (floor((index/rowcols))+1)/(rowcols+1));
  108.     if (newTargX != targX) {
  109.       // target X position has changed
  110.       // reset X speed
  111.       targX = newTargX;
  112.     }
  113.     if (newTargY != targY) {
  114.       // target Y position has changed
  115.       targY = newTargY;
  116.     }
  117.     xSpeed = (targX-x)/200;
  118.     ySpeed = (targY-y)/200;
  119.    
  120.     x = x + xSpeed;
  121.     y = y + ySpeed;
  122.  
  123.     switch(letter) {
  124.     case 'a':
  125.       dynamic -= dynamic_c;
  126.       break;
  127.     case 'c':
  128.       dynamic += dynamic_c;
  129.       break;
  130.     case 'e':
  131.       dynamic += dynamic_c;
  132.       break;
  133.     case 'G':
  134.       dynamic -= 2*dynamic_c;
  135.       break;
  136.     }
  137.  
  138.     visual();
  139.   }
  140.  
  141.   /*
  142.    * visual
  143.    * draw the char with special cases for some letters
  144.    */
  145.   void visual() {
  146.  
  147.     textSize(size_font);
  148.     float textWidth =  textWidth(letter);
  149.     float ascent = textAscent() * 0.75;
  150.  
  151.     switch(letter) {
  152.     case 'a':
  153.       fill(fontcolour);
  154.       noStroke();
  155.       text(letter, x-textWidth/2, y+ascent/2);
  156.  
  157.       stroke(245,100,100);
  158.       noFill();
  159.       pushMatrix();
  160.       translate(x, y);
  161.       rotate(dynamic);
  162.       arc(0, 0, size_shape, size_shape*randSize1, HALF_PI*randSize2, PI+HALF_PI);
  163.       popMatrix();
  164.       break;
  165.     case 'c':
  166.       fill(fontcolour);
  167.       noStroke();
  168.       text(letter, x-textWidth/2, y+ascent/2);
  169.  
  170.       float tempx = x + size_shape*cos(dynamic*0.5);
  171.       float tempy = y + size_shape*sin(dynamic*0.5);
  172.       float tempx2 = tempx - (size_shape/2)*sin(dynamic*0.2);
  173.       float tempy2 = tempy + (size_shape/2)*cos(dynamic*0.3);
  174.       //fill(255, 0, 0);
  175.       fill(200,100,100,100);
  176.       //stroke(255, 0, 0, 140);
  177.       stroke(200,50,50,100);
  178.       line(x, y, tempx, tempy);
  179.       noStroke();
  180.       ellipse(tempx, tempy, 10, 10);
  181.       stroke(0,100,100,128);
  182.       fill(100,100,100,100);
  183.       ellipse(tempx2, tempy2, 5, 5);
  184.       break;
  185.     case 'e':
  186.       rectMode(CENTER);
  187.       //fill(0,128,255);
  188.       fill(fontcolour);
  189. //      rect(x, y, textWidth, size_shape*sin( radians(dynamic) ));
  190.       ellipse(x, y, textWidth, size_shape*sin( radians(dynamic) ));
  191.       rectMode(CORNER);
  192.  
  193.       fill(255,100,100);
  194.       noStroke();
  195.       text(letter, x-textWidth/2, y+ascent/2);
  196.       break;
  197.     case 'G':
  198.       pushMatrix();
  199.       rectMode(CENTER);
  200.       fill(0,100,100,75);
  201.       translate(x,y);      
  202.       rotate(cos(radians(dynamic)));
  203.       rect(0, 0, textWidth*1.1, textWidth*1.4);
  204.       rectMode(CORNER);
  205.  
  206.       fill(150,80,100,200);
  207.       noStroke();
  208.       text(letter, -textWidth/2, ascent/2);
  209.       popMatrix();
  210.       break;
  211.     default:
  212.       fill(fontcolour);
  213.       noStroke();
  214.       text(letter, x-textWidth/2, y+ascent/2);
  215.       break;
  216.     }
  217.  
  218.     //ellipse(x, y, 10, 10);
  219.   }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement