home *** CD-ROM | disk | FTP | other *** search
/ com!online 2002 July / com!online0702.iso / software / livemotion / DATA1.CAB / Scripting_Resources / Samples / Automation_Scripts / kochSnowflake.js < prev    next >
Encoding:
Text File  |  2002-05-13  |  4.3 KB  |  118 lines

  1. /***************************************************************
  2. ADOBE SYSTEMS INCORPORATED 
  3. Copyright 2001 Adobe Systems Incorporated 
  4. All Rights Reserved 
  5.  
  6. NOTICE:  Adobe permits you to use, modify, and distribute this 
  7. file in accordance with the terms of the Adobe license agreement 
  8. accompanying it.  If you have received this file from a source 
  9. other than Adobe, then your use, modification, or distribution
  10. of it requires the prior written permission of Adobe. 
  11. ***************************************************************/
  12. /***************************************************************
  13. Author: Mary Obelnicki
  14. ***************************************************************/
  15.  
  16. /***************************************************************
  17.  
  18. Description goes here
  19.  
  20. ***************************************************************/
  21.  
  22. #include "../../Include/Vector.js" 
  23. #include "../../Include/fractal.js"
  24.  
  25. /***************************************************************
  26. To change the behavior of this script, 
  27. make your changes below
  28. ***************************************************************/
  29.  
  30. //will draw a kochSnowflake with radius 200, depth 2; 
  31. var comp = application.currentComposition; 
  32. var diameter = Math.min(comp.size.x, comp.size.y); 
  33. kochSnowflake(diameter/2, 3, comp.size.x/2, comp.size.y/2); 
  34.  
  35. /***************************************************************
  36. DO NOT EDIT BELOW THIS LINE
  37. ***************************************************************/
  38.  
  39.  
  40. function kochSnowflake(radius, depth, x, y)
  41. // draws a koch snowflake of depth <depth> with a radius of <radius>.
  42. {
  43.     // create the base triangle
  44.     var basePath = kochSetup(radius, x, y);
  45.     //call the fractal function on the base triangle
  46.     fractal(basePath, kochReplaceLine, depth); 
  47.  
  48. function kochSetup(rad, inX, inY)
  49. //draws an equalateral triangle path object on a circle of radius <rad>.
  50.  
  51. //returns: the path that is the triangle (not the object).  
  52. {
  53.     //draw the initial triangle
  54.     var triangle = application.currentComposition.createObject(LMGeometricType.path, inX, inY); 
  55.     
  56.     var anglePerPoint = 2*Math.PI/3; 
  57.     var angleOffset  = Math.PI/2;  // so a point is straight up
  58.     var startAngle = 0 + angleOffset; 
  59.  
  60.     //point 0 
  61.     var path = triangle.addPath(rad*Math.cos(startAngle), -rad*Math.sin(startAngle), 0); 
  62.     triangle.deletePath(1); 
  63.  
  64.     //point 1
  65.     path.addKnot(rad*Math.cos(startAngle-anglePerPoint), -rad*Math.sin(startAngle-anglePerPoint), 1); 
  66.  
  67.     //point 2
  68.     path.addKnot(rad*Math.cos(startAngle-2*anglePerPoint), -rad*Math.sin(startAngle-2*anglePerPoint), 2); 
  69.     path.closed = true; 
  70.     return path; 
  71.  
  72. }
  73.  
  74. function kochReplaceLine(path, knotOneIndex, knotTwoIndex, knotsArray)
  75. //path: the path to edit
  76. //knotOne: the index of the first knot in the line segment
  77. //knotTwo: the index of the second knot in the line segment
  78. //knotsArray: an array containing the knots, the index of <knotOne>
  79. //<knotTwo> must be the same in <knotsArray> and <path>.knots.
  80. // an optional arguement for speed
  81. {
  82.     //vectors representing the defining points of the line segment. 
  83.     var p1;
  84.     var p2; 
  85.     if (arguments.length == 4)
  86.     {
  87.     p1 = new Vector(knotsArray[knotOneIndex].position.x, -knotsArray[knotOneIndex].position.y); 
  88.     p2 = new Vector(knotsArray[knotTwoIndex].position.x, -knotsArray[knotTwoIndex].position.y);  
  89.     }
  90.     else
  91.     {
  92.     p1 = new Vector(path.knots[knotOneIndex].position.x, -path.knots[knotOneIndex].position.y); 
  93.     p2 = new Vector(path.knots[knotTwoIndex].position.x, -path.knots[knotTwoIndex].position.y); 
  94.     }
  95.  
  96.     //the line segment
  97.     var p2minusp1 = Vector.subtract(p2, p1); 
  98.     //one thrid of the line segment
  99.     var segment = Vector.scalerMult(p2minusp1, 1/3); 
  100.  
  101.     //add the three knots to the path to chage the line segment to the next depth
  102.     // of the koch snowflake. 
  103.  
  104.     //the first knot
  105.     var pointA = Vector.add(p1, segment); 
  106.     path.addKnot(pointA.x, -pointA.y, knotOneIndex+1); 
  107.  
  108.     //the second knot
  109.     var pointC = Vector.add(pointA, Vector.zAxisRotate(segment, Math.PI/3));
  110.     path.addKnot(pointC.x, -pointC.y, knotOneIndex+2); 
  111.  
  112.     //the thrid knot
  113.     var pointB = Vector.add(pointA, segment); 
  114.     path.addKnot( pointB.x , -pointB.y, knotOneIndex+3); 
  115.  
  116. }
  117.