sinus.as

  1. /*
  2.  * Onda sinusoidal - 2011-02-01 - ProInf.net
  3.  */
  4. // http://tutorials.flashmymind.com/2009/02/actionscript-3-drawing-api/
  5. // http://www.flashandmath.com/howtos/circlefill/index.html
  6.  
  7. /* DATOS ----------------------------------------*/
  8.  
  9. /* Datos globales */
  10. var xCenter:Number = stage.stageWidth / 2;
  11. var yCenter:Number = stage.stageHeight / 2;
  12. var xPos, yPos:Number;
  13. var xDist, yDist:Number;
  14. var angle:Number = 0;
  15. var incrAngle:Number = Math.PI/100;
  16.  
  17. /* Datos del círculo */
  18. var radius:Number = 100;
  19. var xCircle:Number = radius+10;
  20. var yCircle:Number = yCenter;
  21. var lengthCircle:Number = radius * 2 * Math.PI;
  22.  
  23. /* Clip principal */
  24.  
  25. //stage.scaleMode = StageScaleMode.NO_SCALE;
  26. var spriteMain:Sprite = new Sprite();
  27. addChild(spriteMain);
  28.  
  29. /* DIBUJO ESTÁTICO ------------------------------*/
  30.  
  31. var spriteBack:Sprite = new Sprite();
  32. spriteMain.addChild(spriteBack);
  33. drawBackground();
  34.  
  35. function drawBackground()
  36. {
  37. var alpha = 0.25;
  38.  
  39. with (spriteBack.graphics)
  40. {
  41. /* Líneas rectas en la gráfica del seno */
  42. lineStyle(1, 0x999999, alpha);
  43. xPos = xCircle + radius;
  44. for (var j=-1; j<=1; ++j) { // Horizontales
  45. var yTemp:Number = yCircle + j*radius;
  46. moveTo(xPos, yTemp); lineTo(xPos + lengthCircle, yTemp);
  47. }
  48. for (var i=0; i<=4; ++i) { // Verticales
  49. var xTemp:Number = xPos + i*lengthCircle/4;
  50. moveTo(xTemp, yCircle - radius);
  51. lineTo(xTemp, yCircle + radius);
  52. }
  53.  
  54. /* Líneas rectas en el círculo */
  55. lineStyle(1, 0x999999, alpha);
  56. moveTo(xCircle - radius, yCircle); lineTo(xCircle + radius, yCircle);
  57. moveTo(xCircle, yCircle - radius); lineTo(xCircle, yCircle + radius);
  58.  
  59. /* Dibujar el círculo */
  60. lineStyle(2, 0x66cc66, alpha);
  61. moveTo (xCircle + radius, yCircle);
  62. for(angle = 0; angle <= Math.PI*2 + incrAngle; angle+=incrAngle) {
  63. xPos = xCircle + Math.cos(angle) * radius;
  64. yPos = yCircle - Math.sin(angle) * radius;
  65. lineTo(xPos, yPos);
  66. }
  67.  
  68. /* Dibujar el seno */
  69. lineStyle(2, 0xcc6666, alpha);
  70. xDist = xCircle + radius;
  71. moveTo (xDist, yCircle);
  72. for(angle = 0; angle <= Math.PI*2; angle+=incrAngle) {
  73. xPos = xDist + radius * angle;
  74. yPos = yCircle - Math.sin(angle) * radius;
  75. lineTo(xPos, yPos);
  76. }
  77. }
  78. }
  79.  
  80. /* ANIMACION ------------------------------------*/
  81.  
  82. /* Animación de dibujar el círculo y el seno */
  83.  
  84. var spriteAux:Sprite = new Sprite();
  85. var spriteCircle:Sprite = new Sprite();
  86. var spriteSinus:Sprite = new Sprite();
  87.  
  88. spriteMain.addChild(spriteAux);
  89. spriteMain.addChild(spriteCircle);
  90. spriteMain.addChild(spriteSinus);
  91.  
  92. addEventListener (Event.ENTER_FRAME, onEnterFrame);
  93. function onEnterFrame (event:Event):void
  94. {
  95. if (angle ==0 || angle > Math.PI*2) // Reset
  96. {
  97. spriteCircle.graphics.clear();
  98. spriteCircle.graphics.lineStyle(3, 0x006600);
  99. spriteCircle.graphics.moveTo(xCircle + radius, yCircle);
  100.  
  101. spriteSinus.graphics.clear();
  102. spriteSinus.graphics.lineStyle(3, 0x660000);
  103. spriteSinus.graphics.moveTo(xCircle + radius, yCircle);
  104.  
  105. angle = 0;
  106. }
  107. xDist = xCircle + radius + radius * angle;
  108. xPos = xCircle + Math.cos(angle) * radius;
  109. yPos = yCircle - Math.sin(angle) * radius;
  110.  
  111. spriteCircle.graphics.lineTo(xPos, yPos);
  112. spriteSinus.graphics.lineTo(xDist, yPos);
  113.  
  114. // Líneas
  115. with (spriteAux.graphics) {
  116. clear();
  117. lineStyle(1, 0x00cccc, 0.5);
  118. moveTo(xCircle, yCircle);
  119. lineTo(xPos, yPos);
  120. lineTo(xDist, yPos);
  121. lineTo(xDist, yCircle);
  122. }
  123.  
  124. // Ángulo
  125. dtAngle.text = int(angle*180/Math.PI) + "º";
  126. dtSinus.text = round2(Math.sin(angle));
  127.  
  128. // Siguiente
  129. angle += incrAngle;
  130. }
  131.  
  132. function round2(n:Number):String {
  133. var str:String = "" + Math.round(n*100)/100;
  134. if (str.indexOf(".") < 0) str += ".";
  135. return (str+"00").substr(0,(n<0?5:4));
  136. }
  137.  
  138. /* Mostrar a toda la pantalla ------------------- */
  139.  
  140. // Añadir el parámetro "allowFullScreen" a "true" en el código HTML del Flash
  141.  
  142. stage.addEventListener(MouseEvent.CLICK, switchFullScreen);
  143. function switchFullScreen(e:MouseEvent):void {
  144. if (stage.displayState != StageDisplayState.FULL_SCREEN) {
  145. stage.displayState = StageDisplayState.FULL_SCREEN;
  146. }
  147. else {
  148. stage.displayState = StageDisplayState.NORMAL;
  149. }
  150. }
  151.  

Proinf.net