Javascript Runtime Error Involving Value Not Written in Code - javascript

I am using App Lab on Code.org, which utilizes JavaScript commands; however, they have their own UI controls. Therefore, the code will contain commands such as onEvent() and setText(), etc. etc. These are all acceptable.
I am attempting to make a code for the Spherical Law of Cosines that calculates the distance between four different locations, but I am getting a runtime error message stating this: "Line: 171: Oops, we can’t figure out what -0.9208185005422157 is - perhaps you meant the string “-0.9208185005422157” with quotes? If this is meant to be a variable, make sure you declared a variable: var -0.9208185005422157."
The line in reference is as follows:
N = Math.acos((((((Math.abs(Math.cos(a)))*(Math.PI/180))*(Math.abs((Math.cos(b)(Math.PI/180))))+(((Math.abs(Math.sin(a)*(Math.PI/180)))+((Math.abs(Math.sin(b)*(Math.PI/180)))))*(((Math.abs(Math.cos(n))*(Math.PI/180)))))))));
Basically, I'm trying to find the value of the angle between two locations using the Spherical Law of Cosines, so I converted all of my values to degrees.
What would cause the error to log a random value in that line that I did not write?

It seems that you are missing a *
Math.cos(b)(Math.PI/180)
should be
Math.cos(b)*(Math.PI/180)

Related

Converting Vector Processing syntax to p5 sketch

I am learning coding and am a novice. I am currently trying to convert a Processing(java) sketch to a p5(javascript) sketch to put on my first website.
I'm having trouble translating the Vector and Array syntax from the Processing sketch.
This is the Vector from the processing Sketch (working):
for (int i = pts.size()-1; i >= 0; i --){
PVector pt = (PVector)pts.get(i);
......
PVector pt2 = (PVector)pts.get(j);
if (pt.dist(pt2) < 20){
......
Here is how I've been trying to translate it in p5 (not working)
for (var i = pts.size()-1; i >= 0; i --){
pt = p5.Vector.pts.get(i);
...........
var pt2 = (PVector)pts.get(j);
if (pt.dist(pt2) < 20){
line(pt.x, pt.y, pt2.x, pt2.y);
}
}
You shouldn't try to translate code by going line-by-line and translating the syntax. Instead, you need to take a step back and convert the program into English first. Then you take that English and try to implement it in the targed language. That might sound dumb, but that English is called an algorithm.
So, you should have a description of your program like this:
"Show 10 circles bouncing around the screen. If a circle touches the edge of the screen, it should bounce off by..."
That's just an example, but you get the idea. Then you'd take that and implement it in P5.js.
So instead of saying "how do I convert this array syntax to P5.js", you should be asking yourself "how do arrays work in JavaScript" or even "how do variables work in JavaScript". From there you can read tutorials to figure out how to implement your algorithm in P5.js.
Then if you get stuck on a specific syntax error, please look in the JavaScript console for any errors you're getting. We can't really help if all you tell us is that it's not working. What error are you getting? Where is your MCVE?
All of that being said, I'll try to help with your specific question. Let's take the original line:
PVector pt = (PVector)pts.get(i);
This line is declaring a variable named pt and is pointing it at whatever is returned from pts.get(i), which it casts to a PVector because Java is statically typed so everything needs a type.
Compare that to what you're trying to do in P5.js:
pt = p5.Vector.pts.get(i);
First off, where did you declare the pt variable? Secondly what is p5.Vector.pts? This syntax doesn't make any sense. You need to read up on how variables and arrays work in JavaScript.
Similarly, let's look at this line in your P5.js code:
var pt2 = (PVector)pts.get(j);
Again, where is pts declared? And you never have to cast anything in JavaScript, because it's dynamically typed. Again, you need to go back and read up on how variables in JavaScript work.
Shameless self-promotion: I've written a series of tutorials geared towards Processing developers trying to learn JavaScript, available here.

Formula not calculating correctly in JavaScript

I'm experiencing an issue with a formula in JavaScript.
var animMoveX = $(this).attr('data-start') + (animPercentage / 100) * ($(this).attr('data-finish') - $(this).attr('data-start'));
To my eyes it's a fairly simple piece of math, but the console outputs 120-[variable no relative to animPercentage, eg. 126.49681528662421].
I've double-checked all variables, and they are correct, and if I replace one of the $(this).attr('data-start') variable in one of the positions with a fixed number, then the calculation is run just fine. Can someone shed some light on why this is, and how I could potentially work around it?
From my comment: Precedence means it will calculate a number on the right and add it to the string from data-start (i.e. using string concatenation). That needs to be converted to a number too. #Pointy's suggestion will do that as data converts strings to appropriate data types (when it can).
So basically change all the attr() calls to data() calls and "numbers" (stored in attributes) will become numbers:
var animMoveX = $(this).data('start') + (animPercentage / 100) * ($(this).data('finish') - $(this).data('start'));
As an added bonus, using data instead of attr is shorter code too :)

How to read PostGIS point in JavaScript?

I am currently getting PostGIS points at client side using JavaScript, is there a way to convert those points into ordinary x, y coordinates using JavaScript?
Here is how I get the points currently:
0101000020E6100000DE02098A1FF33F40BADA8AFD65F74140
You haven't posted your actual query, but if you wrap the geometry referenced in your SELECT statement with ST_AsText() then it will return the geometry in a human readable format called Well Known Text.
e.g.
SELECT my_id, ST_AsText(my_geom) FROM my_table;
-- returns POINT(31.9497 35.9328) using the geometry from your question.
This is then fairly ordinary parsing exercise. Or, if you just want the raw lon / lat (or easting / northing), then structure your query like this:
SELECT ST_X(my_geom), ST_Y(my_geom) FROM my_table;
-- returns: 31.9497, 35.9328 using the geometry from your question
Note that you can also transform the geometry, if necessary, to the coordinate system that you need to use. For example, if the geometry is stored in the database in conventional lon/lat format (EPSG code 4326) and you need to retrieve it in the Pseudo Mercator Projection ordinarily used in online maps (EPSG code 3857), then you need to do this:
-- note the explicit ::geometry cast
SELECT my_id, ST_AsText(ST_Transform(my_geom::geometry, 3857)) FROM my_table;
For fun, you can try this in a PostGIS enabled Postgres SQL query window (using the geometry from your question):
Return as WKT:
SELECT ST_AsText('0101000020E6100000DE02098A1FF33F40BADA8AFD65F74140');
-- returns: POINT(31.9497 35.9328)
Return as Pseudo Mercator:
SELECT ST_AsText(ST_Transform('0101000020E6100000DE02098A1FF33F40BADA8AFD65F74140'::geometry, 3857))
-- returns: POINT(3556624.33499785 4291378.69099916)
Return as X, Y:
SELECT ST_X('0101000020E6100000DE02098A1FF33F40BADA8AFD65F74140'),
ST_Y('0101000020E6100000DE02098A1FF33F40BADA8AFD65F74140');
-- returns: 31.9497, 35.9328

JavaScript: How do i use a string as a piece of code

I am making a simple-ish graph maker to visualise equations. I need to be able to have the user input a string in a textbox and then interpret that as a piece of code I can execute to produce the graph. The way I am displaying the graph is by going through x in small increments and using an equation to then calculate the y position and then drawing a line between the points. At the moment I am just manually making a function in the code for example:
function(val) { return (val * val) + 5; }
but I need to be able to create a similar function from a string so the user could just input something like "(x*x)+(2*x)". is there any way to do this?
Canonically, this is done with eval(), although it comes with many caveats and should probably be avoided.
There are several questions on SO that discuss eval alternatives, but in your case, I would suggest a very bare-bones parser -- especially if all you're handling are mathematical equations.

CANNON.js: check if a body is being constrained

I've been trying to make a multiplayer game using javascript (most of which is on the server, using Node.js); one of the core mechanics I want to make is that players will be able to design their own fighting style (right down to how they swing their sword etc). Problem is that I can't find any simple way of constraining players' movements.
I first tried to write a method that checks then clamps the player's style so it doesn't look like they're breaking every limb simultaneously, but that didn't really work. I then found the wonderful CANNON.ConeTwistConstraint, but after looking through the documentation I've found that CANNON.js's constraints don't seem to have any sort of built-in function for just testing whether two bodies are exceeding the constraint's limits. I've thought about having my game just create objects in a separate simulation and check whether a force is being applied to either object, but I'm not sure about how to go about this, or if there's a better way.
Is there a simple/easier solution to my problem? If not, what would be the least CPU-intensive way of implementing the above?
You can manually check if the ConeTwistConstraint is hitting its limit. If you have a look at the method CANNON.ConeEquation.prototype.computeB, you can see that it computes the constraint violation, "g", using cos() and a dot product. You can simply do the same with the following code.
var eq = coneTwistConstraint.coneEquation;
var g = Math.cos(eq.angle) - eq.axisA.dot(eq.axisB);
if(g > 0) {
// Constraint limit exceeded
} else {
// Constraint is within limits
}
Using the same strategy, you can check if the twist limit is exceeded. Since the twist equation is a CANNON.RotationalEquation, the code becomes:
var eq2 = coneTwistConstraint.twistEquation;
var g2 = Math.cos(eq2.maxAngle) - eq2.axisA.dot(eq2.axisB);

Categories

Resources