I got bored so I decided to make a personal javascript library (I think that's what it is, I've only been using plain javascript, not even jQuery), and I wanted to make a javascript function, that works much like the
move (10) stepsblock. But the problem is, for some reason my conversion isn't working. I added a method onto the Math object, like this:
Math.convertRadToDeg=function(radians){ var degreeInRadians = Math.PI/180; var degrees=degreeInRadians*radians; return degrees; };
Except when I put Math.sin(9) in there, it returns 0.007192824475800312 , and according to google calculator, Math.sin(9) in degrees is 0.15643446504 .
I'm relatively new to Javascript (which is why I've been focusing on plain javascript), and would like to know if there's a better way or what's wrong with my conversion equation, or if it's something else that's causing it. Here's the code that I'm using it for:
[i]object[/i].move=function(el,st,dir){ var oldX=el.style.left; var oldY=el.style.top; var newX=(Math.convertRadToDeg(Math.sin(dir)))*st; var newY=(Math.convertRadToDeg(Math.cos(dir)))*st; newY+=oldY; newX+=oldX; el.style.left=newX; el.style.top=newY; };
Any help would be appreciated.
Offline
bump
Also forgot that the [code] BBcode doesn't allow [i] in it...
Offline
Sine doesn't return an angle, it returns a ratio. If you're unfamiliar with trigonometry, try out our own Scratch Wiki page here.
To solve your problem, you need to first write a function to convert degrees to radians (not the other way around). Then do this:
Math.sin(Math.convertDegreesToRadians(90));
Does that help?
Offline
Hardmath123 wrote:
Sine doesn't return an angle, it returns a ratio. If you're unfamiliar with trigonometry, try out our own Scratch Wiki page here.
To solve your problem, you need to first write a function to convert degrees to radians (not the other way around). Then do this:Code:
Math.sin(Math.convertDegreesToRadians(90));Does that help?
Not really.
When I do:
Math.sin(Math.convertDegToRad(90));
I get as a result -0.9540914674728181
where the Math.convertDegToRad function is like this:
Math.convertDegToRad=function(degrees){ var conversion=180/Math.PI; var result=degrees*conversion; return result; };
What's wrong?
(Also, as a side note: I got this message:
Sorry, New Scratchers / TBGers cannot post urls to websites other than Scratch in their posts. Please remove all BB code [url] tags.
for quoting you when you had this in url tags: wiki.scratch.mit.edu/wiki/Trigonometry
Offline
Well, new Scratchers can't post URLs, though I assumed the Wiki was allowed. Strange.
Anyway, "conversion" should be the opposite: Math.PI/180. A nice trick is to try to figure out what you get from your formula by inputting 180: you should get PI.
Offline
Hardmath123 wrote:
Well, new Scratchers can't post URLs, though I assumed the Wiki was allowed. Strange.
Anyway, "conversion" should be the opposite: Math.PI/180. A nice trick is to try to figure out what you get from your formula by inputting 180: you should get PI.
I did that and it worked, but the object still isn't moving like it's supposed to. It's in fact, adding in the style attribute for right, in which it's never specified every in any of my code, here's the function:
object.move=function(el,st,dir){ //element, steps, direction var oldX=el.style.left; var oldY=el.style.top; var newX=((Math.sin(Math.convertDegToRad(dir)))*st)+oldX; var newY=((Math.cos(Math.convertDegToRad(dir)))*st)+oldY; el.style.left=newX; el.style.right=newY; };
and when I put
div=document.getElementById('divIMade'); div.style.left='5px'; div.style.top='5px'; object.move(div,1,90);
and I inspect the element, it's new coordinates are:
left: 15px; top: 5px; right: 0.(bunch of zeros)6123233995736765px;
The left is 9 more than it should be (because i said that the steps should be 1), and I never specified right.
Offline
Well, here are your problems:
1. Switch the sin and cos functions. I won't go into the geometry here, but trust me, you've got them opposite.
2. The last line of you first function does in fact say el.style.right when it should be el.style.top.
3. This isn't a problem, but don't use the word "object" for an object. The word "Object" (capitalized) is part of the JavaScript language as a built-in, so it's not too nice to use "object". I don't know what the rest of your code looks like, but I personally would write a library like this:
function(myLibraryName) { myLibraryName.aCoolFunction = function() { } myLibraryName.aConstant = 42; }(window.myLibraryName)
This lets you change the name of your library really easily by substituting whatever you want into the last line (window.banana would rename your library to banana). The upshot is you don't have any name conflicts.
Offline