paulojamorim

hypocycloid

Jan 22nd, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.29 KB | None | 0 0
  1. <!-- Paulo Henrique Junqueira Amorim -->
  2. <html>
  3.   <head>
  4.     <title>Hypocycloid com HTML5</title>
  5.  
  6.       <script type="text/javascript">
  7.        
  8.         var canvas, ctx;  
  9.         var theta = 0;
  10.         var k, x, y;
  11.  
  12.         function animate()
  13.         {
  14.           reqFrame = window.mozRequestAnimationFrame    ||
  15.                          window.webkitRequestAnimationFrame ||
  16.                          window.msRequestAnimationFrame     ||
  17.                          window.oRequestAnimationFrame;
  18.           theta=theta+0.006;
  19.           coord = calculateCoordinates(theta);
  20.           x = coord[0];
  21.           y = coord[1];
  22.           draw();
  23.          
  24.           reqFrame(animate);
  25.        }
  26.  
  27.         function init(form)
  28.         {
  29.           if (form != null)
  30.           {
  31.            
  32.             k =  parseFloat(form.k.value);
  33.             canvas = document.getElementById("hypocycloid");
  34.             ctx = canvas.getContext("2d");
  35.             ctx.clearRect(0, 0, canvas.width, canvas.height);
  36.           }
  37.           else
  38.           {
  39.             k = 4.5;
  40.           }
  41.           animate();
  42.         }
  43.  
  44.        function calculateCoordinates(theta)
  45.        {
  46.            canvas = document.getElementById("hypocycloid");
  47.  
  48.            width = canvas.width;
  49.            height = canvas.height;
  50.            
  51.            r = width/(k*3);
  52.            x = width/2 + r * (k - 1) * Math.cos(theta) + r * Math.cos((k-1)*theta);
  53.            y = height/2 + r * (k - 1) * Math.sin(theta) - r * Math.sin((k-1)*theta);
  54.            return [x,y];
  55.        }
  56.  
  57.         function draw()
  58.         {
  59.             canvas = document.getElementById("hypocycloid");
  60.             ctx = canvas.getContext("2d");
  61.             ctx.fillRect(x,y,1,1);
  62.         }
  63.     </script>
  64.  
  65.     <style type="text/css">
  66.       canvas {border: 1px solid black;}
  67.     </style>
  68.  
  69.   </head>
  70.   <body onload="init(null);">
  71.     <canvas id="hypocycloid" width="500" height="500"></canvas>
  72.     <p>
  73.     <form name="form" action="" method="GET">Valor de K
  74.         <input type="text" name="k" value="4.5" size=4>
  75.     <input type="button" name="button" value="Atualizar" onclick="init(this.form)">
  76.     <p>
  77.     Valores interessantes de K: 2.1,  3,  3.1,  3.15,  3.2,  4,  4.15,  4.5,  6, 12  ...
  78.     <p>
  79.     by Paulo Henrique Junqueira Amorim
  80.   </body>
  81. </html>
Advertisement
Add Comment
Please, Sign In to add comment