Category Archives: graphics algorithms

Kusner-Schmitt (Tetrahedral Implicit Surface)

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

The same as yesterdays with a different plot from Paul Bourke's website.

Also posted in 3D, BitmapData, Vector | Tagged , | Leave a comment

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, pixel manipulation, strings | Tagged , | Leave a comment

Point in Polygon

Actionscript:
  1. // function found here:
  2. // http://www.alienryderflex.com/polygon/
  3. function pointInPolygon(x:Number, y:Number, polyX:Array, polyY:Array):Boolean{
  4.     var j:int = polyX.length - 1;
  5.     var oddNodes:Boolean = false;
  6.     for (var i:int=0; i <polyX.length; i++) {
  7.     if (polyY[i] <y && polyY[j]>= y ||  polyY[j] <y && polyY[i]>= y) {
  8.  
  9.       if (polyX[i] + (y - polyY[i]) / (polyY[j] - polyY[i]) * (polyX[j] - polyX[i]) <x) {
  10.         oddNodes = !oddNodes;
  11.       }
  12.     }
  13.     j = i;
  14.   }
  15.   return  oddNodes;
  16. }
  17.  
  18. // draw an overly complex poly and store all  x y coords
  19. var pX:Array= new Array();
  20. var pY:Array = new Array();
  21. graphics.beginFill(0xCC0000);
  22. for (var i:int = 0; i<60; i++){
  23.     pX[i] = Math.random()*stage.stageWidth;
  24.     pY[i] =Math.random()*stage.stageHeight;
  25.     if (i == 0){
  26.         graphics.moveTo(pX[i], pY[i]);
  27.     }else{
  28.         graphics.lineTo(pX[i], pY[i]);
  29.     }
  30. }
  31. addEventListener(Event.ENTER_FRAME, onLoop);
  32. function onLoop(evt:Event):void {
  33.        alpha = 1;
  34.        if (pointInPolygon(mouseX, mouseY, pX, pY)){
  35.             alpha = .5;
  36.        }
  37. }

This snippet shows how to test to see if a point is inside a polygon. This may not really be useful as hitTestPoint() is likely faster - however this could be modified for other purposes such as polygon to polygon collision. This algorithm works nicely on all kinds of polygons ... convex, concave etc...

I found this algorithm in an article by Darel Rex Finley ... here.

I've used this function in Processing a few times...

Also posted in misc | Tagged , | 2 Comments

Supershapes / Superformula

Actionscript:
  1. // Superformula (equations from):
  2. // http://www.geniaal.be/downloads/AMJBOT.pdf
  3. // http://en.wikipedia.org/wiki/Superformula
  4. const TWO_PI:Number = Math.PI * 2;
  5. function superShape(a:Number, b:Number, m:Number, n1:Number, n2:Number, n3:Number, pnt:Point, scale:Number):void{
  6.     var r:Number = 0
  7.     var p:Number = 0;
  8.     var xp:Number = 0, yp:Number = 0;
  9.     while(p <= TWO_PI){
  10.         var ang:Number = m * p / 4;
  11.         with(Math){
  12.             r = pow(pow(abs(cos(ang) / a), n2) + pow(abs(sin(ang) / b), n3),-1/n1);
  13.             xp = r * cos(p);
  14.             yp = r * sin(p);
  15.         }
  16.         p += .01;
  17.         canvas.setPixel(pnt.x + xp *scale, pnt.y + yp * scale,  0xFFFFFF);
  18.      }
  19. }
  20. // test it out:
  21. var canvas:BitmapData = new BitmapData(700,600,false, 0x000000);
  22. addChild(new Bitmap(canvas, "auto", true));
  23.  
  24. superShape(1, 1, 5, 23, 23, 23, new Point(100,80), 30);
  25. superShape(1, 1, 5, 13, 13, 3, new Point(200,80), 30);
  26. superShape(1, 1, 8, 3, 13, 3, new Point(300,80), 30);
  27. superShape(10,8, 16, 30, 13, 3, new Point(450,80), 30);
  28. superShape(1,1, 1, .5, .5, .5, new Point(100,190), 100);
  29.  
  30. for (var i:int = 0; i <150; i++){
  31.   superShape(1,1, 2, 1+i/800, 4, 8-i * .1, new Point(550,350), 50);
  32. }
  33. for (i = 0; i <20; i++){
  34.   superShape(1.1,1.2, 6, 2 + i , 4, 9 - i, new Point(200,350), 50);
  35. }

The above snippet demos a function that will draw Supershapes using the Superformula...

From wikipedia:
The Superformula appeared in a work by Johan Gielis. It was obtained by generalizing the superellipse, named and popularized by Piet Hein...

Here is the result of the above code:


You can read more about the Superformula here in the original paper by Gielis.

wikipedia entry...

3d Supershapes by Paul Bourke

Also posted in Math, misc, setPixel | Tagged , | Leave a comment