# Tag Archives: processing

## Ancient Egyptian Multiplication

Actionscript:
1. egyptianMultiply(12, 99);
2.
3. // trace(12 * 99) // test to make sure it works
4.
5. /* outputs:
6. /64 768
7. /32 384
8.  16 192
9.  8 96
10.  4 48
11. /2 24
12. /1 12
13. ---
14. 1188
15. */
16.
17.
18. function egyptianMultiply(valueA:Number, valueB:Number):void {
19.
20.     var left:Array = [];
21.     var right:Array = []
22.
23.     // swap if valueB is smaller than value A
24.     if (valueB <valueA){
25.         var swap:int = valueB;
26.         valueB = valueA;
27.         valueA = swap;
28.     }
29.
30.     // create left and right columns
31.     var currentLeft:int = 1;
32.     var currentRight:int = valueA;
33.
34.     while (currentLeft <valueB){
35.         left.push(currentLeft);
36.         right.push(currentRight);
37.         currentRight += currentRight;
38.         currentLeft += currentLeft;
39.     }
40.
41.
42.     // add up the right column based on the left
43.     currentLeft = 0;
44.     var rightSum:int;
45.     var leftSum:int;
46.     var i:int = left.length - 1;
47.
48.     while (currentLeft != valueB){
49.
50.       leftSum = currentLeft + left[i];
51.       // if the next number causes the sum
52.       // to go above valueB, skip it
53.       if (leftSum <= valueB){
54.         currentLeft = leftSum;
55.         rightSum += right[i];
56.         trace("/" + left[i]  + " " + right[i]);
57.       } else {
58.         trace(" " + left[i]  + " " + right[i]);
59.       }
60.       i--;
61.     }
62.     trace("---");
63.     trace(rightSum);
64. }

Someone mentioned egyptian multiplication to me yesterday... So read a little about it here and whipped up this example. For some reason I decided to do it in processing ... once it worked I ported it to ActionScript.

The above link describing the technique I used is from http://www.jimloy.com/... I haven't spent much time digging around the site, but it appears to have some pretty nice stuff on it...

If you're curious, here is the original processing version:

JAVA:
1. Vector left = new Vector();
2. Vector right = new Vector();
3.
4. int valueA = 10;
5. int valueB = 8;
6.
7. if (valueB <valueA){
8.     int swap = valueB;
9.     valueB = valueA;
10.     valueA = swap;
11. }
12.
13. int currentLeft = 1;
14. int currentRight = valueA;
15. while (currentLeft <valueB){
18.     currentRight += currentRight;
19.     currentLeft += currentLeft;
20. }
21.
22. currentLeft = 0;
23.
24. int result = 0;
25. int i = left.size() - 1;
26. while (currentLeft != valueB){
27.
28.   int temp = currentLeft + (Integer) left.get(i);
29.   if (temp <= valueB){
30.
31.     currentLeft = temp;
32.     result += (Integer) right.get(i);
33.     println("/" + left.get(i) + " " + right.get(i));
34.   } else {
35.     println(" " + left.get(i)  + " " + right.get(i));
36.   }
37.
38.   i--;
39. }
40. println("---");
41. println(result);

After writing, this I took a look at the wikipedia entry... I also found myself on this short page about a scribe called Ahmes. (I recommend reading this if you are interested in PI)

Posted in Math, misc | Also tagged , , | 1 Comment

## Circle of Particles #2

Actionscript:
1. [SWF(width = 400, height = 400)];
2. var canvas:BitmapData = new BitmapData(400,400, false, 0x000000);
3. var eraser:BitmapData = new BitmapData(400,400, true, 0x11000000);
5.
6. var particles:Array = new Array();
7. for (var i:int = 0; i <500; i++){
8.     particles.push(makeParticle());
9. }
10.
12. function onLoop(evt:Event):void {
13.     canvas.copyPixels(eraser, eraser.rect, new Point(0,0), null, null, true);
14.     for (var i:int = 0; i <particles.length; i++){
15.         particles[i]();
16.     }
17. }
18.
19. function makeParticle():Function {
20.     var dx:Number, dy:Number;
21.     var x:Number = 200;
22.     var y:Number = 200;
23.     var vx:Number = Math.random() * 4 - 2;
24.     var vy:Number = Math.random() * 4 - 2;
25.     var ang:Number;
26.     return function():void {
27.               x += vx;
28.               y += vy;
29.               dx = 200 - x;
30.               dy=  200 - y;
31.                if (Math.sqrt((dx * dx) + (dy * dy))> 130){
32.                 ang = Math.atan2(dy, dx) / Math.PI * 180;
33.                  vx = Math.cos(ang);
34.                  vy = Math.sin(ang);
35.                }
36.                canvas.setPixel(x, y, 0xFFFFFF);
37.     }
38. }

This is an interesting variation on yesterdays post. The result will looks like this...

Click to view swf... I also decided to do a processing port of this with 3,000 particles:

Processing Version

The processing version eventually evolved into this: Click for enlarged version...

Posted in BitmapData, motion, setPixel | Also tagged , | 2 Comments

• 