i am using toRad() function in javascript as below
var alpha1 = brng.toRad();
but when i run my code it gives an error saying "brng.toRad is not a function"
How does it work in java script, do i need to import some library???
You can also do this:
Number.prototype.toRad = function () { return this * Math.PI / 180; }
var oneeighty = 180;
console.log( oneeighty.toRad() ); // 180 degrees = Pi radians
Native javascript doesn't have a toRad function. Either
1) toRad was defined by google-maps somewhere and brng is just pointing to the wrong object (perhaps null?)
or
2) You need to define the method yourself
function toRad(degrees){
return degrees * Math.PI / 180;
}
toRad(180);
Related
I want to use the math.js library in my javascript file - say it's called webpage.js.
What I did so far is having a script tag in the main html file that calls math.js, and then another that calls webpage.js. The script tags are as follows:
<script
src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.2.5/math.min.js"
integrity="sha256-fWwwg2Pf3Ox5xhm9xCE7O+czkI2dSkqN6gUZumzGrx0="
crossorigin="anonymous"></script>
<script src="js/lat-long.js"></script>
Is that it? Can I now go to my webpage.js file and use "Math.whatever" without having to 'import' or 'include' anything at the beginning of the file?
The reason I am asking this is because I have the following code which is not working properly (anything using Math.something is returning a NaN, which I assumed was because Math.js was not imported properly).
Here is the code for reference:
function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2 - lat1); // deg2rad below
var dLon = deg2rad(lon2 - lon1);
console.log("dLat------------------" + dLat);
var a =
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(deg2rad(lat1)) *
Math.cos(deg2rad(lat2)) *
Math.sin(dLon / 2) *
Math.sin(dLon / 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = R * c; // Distance in km
console.log("d is " + d);
return d;
}
function deg2rad(deg) {
return deg * (Math.PI / 180);
}
var distance = getDistanceFromLatLonInKm(
locationLatUser,
locationLongUser,
supplier.latLng.latitude,
supplier.latLng.longitude
);
//all the values used in the function call exist and are defined.
console.log("distance between user and supplier: " + distance);
Console logging any value in the above code gives me a NaN.
I am trying to code with JavaScript a function to calculate the surface area of a sphere. The radius using a NumericUpDown.
I have tried to use what I did in a different assignment and it is not working like the other did. I am new at this and have not had any help.
function calcSurface() {
var radius = document.getElementByID("radius").value;
radius = math.abs(radius);
Surface = 4*Math.PI*Math.pow(radius);
}
Math.abs() needs a capital M, and getElementById() needs a lower-case d. Math.pow takes two arguments, the base and the exponent, while you're only supplying one: the radius (which is the base).
Try this:
function calcSurface() {
var radius = document.getElementById("radius").value;
radius = Math.abs(radius);
var surface= 4 * Math.PI * Math.pow(radius, 2);
}
You can use this function
function calculateSurface() {
let radius = document.getElementById("radius").value;
radius = Math.abs(radius);
return 4 * Math.PI * Math.pow(radius, 2);
}
And if you have some div in your html like
<div id='result'></div>
You can do this to get the result in your div
document.getElementById('result').innerText = calculateSurface();
Hellow it possible get angle two lines in react js, using svg, without using the svg set attribute?
already tried several tutorials of the stack but none really returned the angle between the two lines and yes only the angle in which the line is, I tried this.
findAngle(p0,p1,p2) {
var a = Math.pow(10,2) + Math.pow(100,2),
b = Math.pow(10,2) + Math.pow(10,2),
c = Math.pow(10,2) + Math.pow(70,2);
var aa = Math.acos( (a+b-c) / Math.sqrt(4*a*b) );
console.log(aa);
}
obs: these values are in my two lines.
Based on this answer, you want something like this:
// helper function: make a point
function point(x,y){
return {'x':x,'y':y};
}
// helper function: get distance between points a and b (by Pythagoras)
function d(a,b) {
return Math.sqrt(d2(a,b));
}
// helper function: get square of distance between points a and b
function d2(a,b) {
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
}
// helper function: convert radians to degrees
function rad2deg(angleInRadians) {
return angleInRadians/(Math.PI/180);
}
// get the angle in degrees between ab and ac, using the cosine rule:
function angle_deg(a,b,c) {
return rad2deg(Math.acos((d2(a,b) + d2(a,c) - d2(c,b)) / (2 * d(a,b) * d(a,c))));
}
p0 = point(0,0);
p1 = point(1,0);
p2 = point(0,1);
console.log(angle_deg(p0,p1,p2)); // 90
I am trying to "alter" the sin cos and tan function from Math object so it can accept recognize if it is a degree "d" or radians. I have an idea on how to do it but I do not know to do it without changing my main function
(function() {
var angle;
while (angle = parseFloat(readline())) {
print(Math.sin(angle, "d").toPrecision(5)); // degrees
print(Math.sin(angle).toPrecision(5)); // radians
print(Math.cos(angle, "d").toPrecision(5));
print(Math.cos(angle).toPrecision(5));
print(Math.tan(angle, "d").toPrecision(5));
print(Math.tan(angle).toPrecision(5));
}
})();
How do alter does function so they can accept the "d" argument I tried use Object.create and another things like JSON.parse(JSON.stringify(Math)); but it doesn't work I need to know how to deep copy Math
You can override Math (in a closure) with an object which inherits from Math:
(function(globalMath) {
// Overriding Math:
var Math = Object.create(globalMath);
// Enhancing trigonometric methods:
var trig = ['sin', 'cos', 'tan'];
for(var i=0; i<3; ++i)
Math[trig[i]] = (function(trigFunc){
return function(angle, d) {
if(d==="d") angle *= Math.PI / 180;
return trigFunc(angle);
};
})(globalMath[trig[i]]);
// Now you can use the enhanced methods:
Math.sin(Math.PI/6); // 0.5
Math.sin(30, 'd'); // 0.5
// You can also use original methods:
globalMath.sin(Math.PI/6); // 0.5
globalMath.sin(Math.PI/6, 'd'); // 0.5 ('d' is ignored)
// Math is a shortcut of globalMath for other methods:
Math.max(1,2); // 2
})(Math);
Everything's an Object in JavaScript, so you can re-write the native Math functions. But this is not recommended, as other commentators have said.
It's simpler to create your own function that converts to degrees internally, like this:
function sinDegrees(angle) {
return Math.sin(angle * (Math.PI / 180));
}
It could even be part of the Math object, if you want:
Math.sinDegrees = sinDegrees;
If you still want to modify the Math.sin function like that, then you can do this:
Math._sin = Math.sin; // save a ref. to the old sin
Math.sin = function sin(angle, type) {
if (type == 'd')
return Math._sin(angle * (Math.PI / 180));
else
return Math._sin(angle);
}
The better solution here is to have a toRad function. It looks very similar to your target code without breaking basic good practices (don't modify objects you didn't create).
function toRad(angle){
return angle * (Math.PI / 180);
}
print(Math.sin(toRad(angle)).toPrecision(5)); // degrees
print(Math.sin(angle).toPrecision(5)); // radians
print(Math.cos(toRad(angle)).toPrecision(5));
print(Math.cos(angle).toPrecision(5));
print(Math.tan(toRad(angle)).toPrecision(5));
print(Math.tan(angle).toPrecision(5));
This also saves you from defining custom versions of each function.
I am having trouble understanding a function hopefully someone can help me out here. I am trying to find the pitch diameter of a sprocket the function for this in JavaScript is:
function sprocket_diam(dataform,pitch,teeth)
{
var a,b,c,d,e;
a = pitch / 2;
b = teeth * 2;
c = 360 / b;
d = Math.sin ((c * Math.PI) / 180);
e = (a / d) * 2
dataform.diam.value = e;
}
The above function works just as intended but I am trying to do this by hand on a calculator. I think the problem I am having comes in the d variable. For example lets say I have a 15 tooth sprocket with a pitch of .5". Using the above formula the numbers for the variables I get are:
a=0.25,b=30,c=12, and for d I take (12*3.14)/180 which gives me 0.2093 so e=(0.25/.2093)*2 which ends up being 2.388915432 but is the incorrect answer it should be 2.404867172372066 Can someone point out what I am doing wrong? I have always struggled with math.
You didn't compute the sinus. (Math.sin)
Your error is that you do as if Math.PI would be 3.14.
If you use the precise value of Math.PI, you get 12*Math.PI/180 == 0.20943951023931953 instead of the 0.2093 you use and at the end you find 2.404867172372066