Category Archives: strings

Random Guitar Tablature

Actionscript:
  1. // number of notes in chord
  2. var noteNum:int = 3;
  3. var lowNote:Number = 0;
  4. var highNote:Number = 4;
  5. // delay between chords
  6. var delay:int = 2000;
  7. ////////////////////////////////////////
  8. var chord:String = "";
  9. highNote += 1;
  10. var tab:TextField = TextField(addChild(new TextField()));
  11. tab.x = tab.y = 20;
  12. tab.defaultTextFormat = new TextFormat("Courier", 12);
  13. tab.multiline = true;
  14. tab.width = 200;
  15.  
  16. changeChord();
  17. setInterval(changeChord, delay);
  18.  
  19. function changeChord():void{
  20.     var strings:Array = [];
  21.     strings[0] = "e|---------------\n"
  22.     strings[1] = "B|---------------\n"
  23.     strings[2] = "G|---------------\n"
  24.     strings[3] = "D|---------------\n"
  25.     strings[4] = "A|---------------\n"
  26.     strings[5] = "E|---------------\n"
  27.    
  28.     for (var i:int = 0; i<3; i++){
  29.         var place:int = 5 + i * 4;
  30.         var choices:Array = [0,1,2,3,4,5];
  31.         for (var j:int = 0; j<noteNum; j++){
  32.             var ii:int = int(Math.random()*choices.length);
  33.             var index:int = choices[ii];
  34.             strings[index] = strings[index].slice(0, place) + (int(Math.random()*highNote)+lowNote) +  strings[index].substring(place+1);
  35.             choices.splice(ii, 1);
  36.         }
  37.     }
  38.     chord = strings.join("");
  39.     tab.text = chord;
  40. }

I'm working on a small program to help me practice guitar. It randomly generates guitar tabs. The idea for the program is similar to a program that helps you learn to type (like Mavis Beacon).

I wrote this snippet today as a proof of concept - just to help me start thinking about what kind of features I want the program to have. There are settings at the top so that you can tweak the number of notes in the chord and the delay between chords etc....

It generates three chords at a time... here are some stills:

Also posted in random, string manipulation | Tagged , , | 6 Comments

Implicit 3D Plot

Actionscript:
  1. var matrix:Matrix3D = new Matrix3D();
  2. var verts:Vector.<Number> = new Vector.<Number>();
  3. var pVerts:Vector.<Number> = new Vector.<Number>();
  4. var uvts:Vector.<Number> = new Vector.<Number>();
  5. for (var i:Number = -2; i<2; i+=.04) {
  6.     for (var j:Number = -2; j<2; j+=.04) {
  7.         for (var k:Number = -2; k<2; k+=.04) {
  8.             // blobby, from here www.iiit.net/techreports/ImplicitTR.pdf
  9. var s:Number=i*i+j*j+k*k+Math.sin(4*i)-Math.cos(4*j)+Math.sin(4*k)-1;
  10.             if (s<0&&s>-.2) {
  11.                 verts.push(i * 60);
  12.                 verts.push(j * 60);
  13.                 verts.push(k * 60);
  14.                 pVerts.push(0),pVerts.push(0);
  15.                 uvts.push(0),uvts.push(0),uvts.push(0);
  16.             }
  17.         }
  18.     }
  19. }
  20. var brush:BitmapData=new BitmapData(3,2,true,0x41FFFFFF);
  21. var canvas:BitmapData=new BitmapData(400,400,false,0x000000);
  22. addChild(new Bitmap(canvas));
  23. var dx:Number=0;
  24. var dy:Number=0;
  25. addEventListener(Event.ENTER_FRAME, onLoop);
  26. function onLoop(evt:Event):void {
  27.     dx += (mouseX - dx)/4;
  28.     dy += (mouseY - dy)/4;
  29.     matrix.identity();
  30.     matrix.appendRotation(dy,Vector3D.X_AXIS);
  31.     matrix.appendRotation(dx,Vector3D.Y_AXIS);
  32.     matrix.appendTranslation(200, 200, 0);
  33.     Utils3D.projectVectors(matrix, verts, pVerts, uvts);
  34.     canvas.lock();
  35.     canvas.fillRect(canvas.rect, 0x000000);
  36.     var p = new Point();
  37.     for (var i:int = 0; i<pVerts.length; i+=2) {
  38.         p.x = pVerts[i];
  39.         p.y = pVerts[i+1];
  40.         canvas.copyPixels(brush, brush.rect, p, null, null, true);
  41.     }
  42.     canvas.unlock();
  43. }

I was looking at some equations for implicit 3D surfaces in this pdf about raytracing... anyway, I realized I could just modify the Utils3D.projectVectors() code (that I wrote a little while ago) to easily render any of the implicit equations mentioned in the pdf. I also did some experimentation with fake lighting and distance rendering which I may post in the future.

(check out the swf on wonderfl.net)

Here are some stills of the above snippet:

Also posted in 3D, BitmapData, Math, Vector, graphics algorithms, pixel manipulation | Tagged , | Leave a comment

Hill Climbing

Actionscript:
  1. var target:Array = ("actionsnippet").split("");
  2. var leng:int=target.length;
  3. var iterations:int = 0;
  4.  
  5. var alphabet:Array = ("abcdefghijklmnopqrstuvwxyz").split("");
  6. var search:Array = randomString();
  7. var indices:Array = new Array();
  8. for (var i:int = 0; i<leng; i++) indices.push(i);
  9.  
  10. addEventListener(Event.ENTER_FRAME, onLoop);
  11. function onLoop(evt:Event):void {
  12.     for (var i:int = 0; i<10; i++){
  13.         if (indices.length> 0){
  14.            var ii:int = int(Math.random()*indices.length);
  15.            var index:int = indices[ii];
  16.          
  17.            search[index] = randomChar();
  18.            
  19.            if (search[index] == target[index]){
  20.                indices.splice(ii,1);  
  21.            }
  22.            trace(search);
  23.            iterations++;
  24.         }else{
  25.             trace("found after "+iterations+" iterations");
  26.             removeEventListener(Event.ENTER_FRAME, onLoop);
  27.             break;
  28.         }
  29.     }
  30. }
  31. function randomChar():String { return alphabet[int(Math.random()*alphabet.length)]; };
  32. function randomString():Array {
  33.     var str:Array = new Array();
  34.     for (var i:int = 0; i<leng; i++) {
  35.         str.push(randomChar());
  36.     }
  37.     return str;
  38. }

A little random hill climbing...

Here are the last few iterations... lowest number of iterations I noticed was round 250...

....
j,c,t,i,o,n,s,n,i,p,p,e,t
y,c,t,i,o,n,s,n,i,p,p,e,t
s,c,t,i,o,n,s,n,i,p,p,e,t
w,c,t,i,o,n,s,n,i,p,p,e,t
e,c,t,i,o,n,s,n,i,p,p,e,t
j,c,t,i,o,n,s,n,i,p,p,e,t
z,c,t,i,o,n,s,n,i,p,p,e,t
l,c,t,i,o,n,s,n,i,p,p,e,t
f,c,t,i,o,n,s,n,i,p,p,e,t
a,c,t,i,o,n,s,n,i,p,p,e,t
found after 361 iterations

Also posted in arrays, misc | Tagged , | 5 Comments

Command Draw Source

Actionscript:
  1. [SWF(width = 600, height = 500, frameRate=30, background = 0xFFFFFF)]
  2.  
  3. var cmd:String = "1000 small orange circles, 9000 black lines,3 large white circles"
  4. // try some of these commands:
  5. //"300 small red triangles and 2 big orange circle 3 big black lines";
  6. //"100 big black triangles, ten small white rectangles 1 big white circle and 1 big gray poly";
  7. // "300 small red triangles and 2 big orange circle 3 big black lines";
  8. //"10 big black circles 1000 big white lines 100 small white rects 1 big red circle and 1 green poly"
  9. stage.align = "TL"
  10. var msk:Shape = new Shape();
  11. with(msk.graphics) beginFill(0x00FF00), drawRect(0,0,600, 400);
  12. this.mask = msk;
  13. var amount:Number;
  14. var color:uint = 0;
  15. var shape:String;
  16.  
  17. var hexLookup:Object = {red:0xFF0000, green:0x00FF00, blue:0x0000FF, yellow:0xFFFF00, orange:0xFFCC00, white:0xFFFFFF, gray:0xCCCCCC, grey:0xCCCCCC, black:0x000001};
  18. var sizeLookup:Object = {large:true, big:true, small:true, tiny:true};
  19. var defaultSize:Number = 50;
  20. var size:Number = defaultSize;
  21. var halfSize:Number = size / 2;
  22. var circles:Function = circle;
  23. var ovals:Function = oval, ellipse:Function = oval;
  24. var rects:Function = rect, rectangle:Function = rect, rectangles:Function = rect;
  25. var lines:Function = line;
  26. var triangles:Function = triangle;
  27. var polygon:Function = poly, polygons:Function = poly;
  28. var large:Function = big;
  29. var tiny:Function = small;
  30.  
  31. parse();
  32. function parse():void {
  33.     amount = 1;
  34.     color = 0x000000;
  35.     shape = "circle";
  36.     var words:Array = cmd.split(/[\W]+/g)
  37.     for (var i:int = 0; i<words.length; i++){
  38.         var w:* = words[i];
  39.         if (isNaN(Number(w))){
  40.             if (hexLookup[w]){
  41.                 color = hexLookup[w];
  42.                 trace(w, "color");
  43.             }else
  44.             if (sizeLookup[w]){
  45.                 this[w]();
  46.             }else
  47.             if (this[w]){
  48.                 for (var j:int = 0; j<amount; j++){
  49.                    this[w]();
  50.                 }
  51.                 color = 0x000000;
  52.                 size = defaultSize;
  53.                 trace(w, "command");
  54.             }
  55.         }else{
  56.             trace(w, "number");
  57.             amount = w;
  58.         }
  59.     }
  60. }
  61. function makeShape(p:Array):void{
  62.         var s:Shape = Shape(addChild(new Shape()));
  63.          for (var i:int = 0; i<p.length; i++) {
  64.             s.graphics[p[i][0]].apply(graphics,p[i].splice(1));
  65.          }
  66.         s.rotation = Math.random() * 360;
  67.         s.x = Math.random() * stage.stageWidth;
  68.         s.y = Math.random() * stage.stageHeight;
  69. }
  70.  
  71. function circle():void{
  72.       makeShape([["beginFill",color], ["drawCircle",0,0,Math.random() * halfSize + halfSize]]);
  73. }
  74. function oval():void{
  75.       makeShape([["beginFill",color],["drawEllipse", 0,0,Math.random() * size + halfSize]]);  
  76. }
  77. function square():void{
  78.         var s:Number = Math.random() * halfSize + halfSize;
  79.         var p:Number = -s/2;
  80.         makeShape([["beginFill",color],["drawRect", p, p, s, s]]);  
  81. }
  82. function rect():void{
  83.         var w:Number = Math.random() * halfSize + halfSize;
  84.         var h:Number = Math.random() * halfSize + halfSize;
  85.         makeShape([["beginFill",color],["drawRect", -w/2, -h/2, w, h]]);
  86. }
  87. function line():void{
  88.     var s:Number = size * 2;
  89.     makeShape([["lineStyle",Math.random()*3, color],["lineTo", Math.random()*s - size, Math.random()*s - size]])
  90. }
  91. function triangle():void {
  92.      var s:Number = size * 2;
  93.      makeShape([["beginFill",color],["lineTo", Math.random()*s - size, Math.random()*s - size], ["lineTo", Math.random()*s - size, Math.random()*s - size]]);
  94. }
  95. function poly():void {
  96.      var s:Number = size * 2;
  97.      var cmds:Array = [["beginFill",color]]
  98.      var num:Number = int(Math.random()*8) + 2;
  99.      for (var i:int = 0; i<num; i++){
  100.           cmds.push(["lineTo", Math.random()*s - size, Math.random()*s - size]);
  101.      }
  102.      makeShape(cmds);
  103. }
  104. function big():void{
  105.     size = 100 + Math.random() * 40;
  106.     halfSize = size / 2;
  107. }
  108. function small():void {
  109.     size = 3 + Math.random() * 20;
  110.     halfSize = size / 2;
  111. }

I've been meaning to post this source for a long time. It's a project I created for shapevent called Command Draw. It allows you to use simple sentence structure to create textures and patterns.

See it in action here (this one has some minimal UI and uses flashvars for the command string).


As a side note, this is the 200th post on ActionSnippet.com

Also posted in Graphics, dynamic | Tagged , | 3 Comments