user8003769
user8003769

Reputation:

Is there a way to handle angles without using trigonometry functions (Javascript)

I am wondering if there's a way to handle angles without using trigonometry functions in Javascript. I am only 13 years old and I don't understand how we can use trigonometry in our code. That's why I want know if there's a way through which we can handle angles without actually using the trigonometry functions.

Below is a code which uses trigonometry functions.

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");

var cx=150;
var cy=150;
var radius=100;
var amp=10;
var sineCount=10;

ctx.beginPath();
for(var i=0;i<360;i++){
  var angle=i*Math.PI/180;
  var pt=sineCircleXYatAngle(cx,cy,radius,amp,angle,sineCount);
  ctx.lineTo(pt.x,pt.y);
}
ctx.closePath();
ctx.stroke();

function sineCircleXYatAngle(cx,cy,radius,amplitude,angle,sineCount){
  var x = cx+(radius+amplitude*Math.sin(sineCount*angle))*Math.cos(angle);
  var y = cy+(radius+amplitude*Math.sin(sineCount*angle))*Math.sin(angle);
  return({x:x,y:y});
}
body{ background-color: ivory; }
#canvas{border:1px solid red; margin:0 auto; }
<canvas id="canvas" width=300 height=300></canvas>

Upvotes: 0

Views: 128

Answers (1)

Brian Peacock
Brian Peacock

Reputation: 1849

Simply put: we use trig functions when we want to do trig.

As you gain a greater understanding of trigonometry you'll understand more about how and why we use the trig functions in JavaScript. The HTML5 Canvas is all about co-ordinates, so trig is a basic tool for creating and manipulating Canvas images - and the more you use the Canvas methods the more you'll understand about trig.

Don't be down-hearted, and keep coding!

(You may find these JavaScript Trigonometry tutorials @ HScripts useful).

Upvotes: 2

Related Questions