I need help calculating the angle between the 5 and the 3. I have absolutely no idea how to even model the question, let alone convert it to java. This is entirely specific, does not need to be abstracted.
int a=3; int b=4; int c=5;
is honestly as far as I can get. This would be cos(b), right? I just need a quick pointer/solution
Is this a right-triangle? So the 5 is the hypotenuse (the side opposite the right angle)?
If that's the case, you need to find any one of the following (which should all be equal to each other): arctan(4/3), ArcSin(4/5), or arccos(3/5).
For example, using the first:
Math.toDegrees(Math.atan(4.0/3.0))
I've got a matrix of values like the one below that I need to scale. I've been looking around for an inbuilt function if there is one that could do this for me. I haven't found one & so have ended up writing code to do the scaling using the below formula
scaledMatrix = (Matrix - MeanMatrix)/Standard Deviation
This code is a bit buggy & I'm working on correcting it. While I do that, I happened to bump on java.math.BigDecimal.scale() & did look up an equivalent for double as the matrix I have is double type numbers
If someone could please help me with details on
1) If there is an inbuilt function that accepts matrix of values & returns me the scaled matrix
2) `java.math.BigDecimal.scale()` equivalent for `double` type data
Any help would be much appreciated please.
The BigDecimal.scale() method does not do what you seem to think it is doing. A BigDecimal value is stored as a * 10^b (where ^ denotes exponentiation). The BigDecimal.scale() method basically returns the b part of that.
I do not know of a similar method for double values, nor do I know of a method which performs the function you need. Since you put apache-commons in the tags, I suggest you look into Apache Commons's extensive statistical library.
I am fairly new to computer science and have been trying, unsuccessfully, to use a point class, a line class, and a Rational number class in order to solve a System of linear equations. The code for the System is old and I'm adapting it to work for the new one I'm making but I can't, for the life of me, debug/work-through one section of the code(the relates to the rest so if I can't do one I can't do all and the whole code doesn't work) at oneChosenFirst(). I'm not seeking the complete code but a very simplified explanation as to how to properly do this, thank you. Here is what i have in systems so far. the systems is based on user inputs too.
My Systems of Linear equations for slope and a y intercept:
public static void oneChosenFirst() {
System.out.println("Enter values for Y intercept (0,#). ");
double A = scn.nextDouble();
double B = scn.nextDouble();
Point Yint1 = new Point();
System.out.println("Enter values for slope (Rise/Run). ");
Line slope1 = new Line();
}
Once again, many thanks, anything you think may help me on my journey through java is much appreciated.
To solve a system of linear equations geometrically (as with your point and line classes), represent each equation as a line, and then the solution to the system is the point at which these lines intersect. If you are having difficulty with a particular step of this process, please say so and show what you've tried so far to accomplish that step.
So I'm programming a recursive program that is supposed to draw Koch's snowflake using OpenGL, and I've got the program basically working except one tiny issue. The deeper the recursion, the weirder 2 particular vertices get. Pictures at the bottom.
EDIT: I don't really care about the OpenGL aspect, I've got that part down. If you don't know OpenGL, all that the glVertex does is draw a line between the two vertices specified in the 2 method calls. Pretend its drawLine(v1,v2). Same difference.
I suspect that my method for finding points is to blame, but I can't find anything that looks incorrect.
I'm following the basically standard drawing method, here are the relevant code snips
(V is for vertex V1 is the bottom left corner, v2 is the bottom right corner, v3 is the top corner):
double dir = Math.PI;
recurse(V2,V1,n);
dir=Math.PI/3;
recurse(V1,V3,n);
dir= (5./3.)* Math.PI ;
recurse(V3,V2,n);
Recursive method:
public void recurse(Point2D v1, Point2D v2, int n){
double newLength = v1.distance(v2)/3.;
if(n == 0){
gl.glVertex2d(v1.getX(),v1.getY());
gl.glVertex2d(v2.getX(),v2.getY());
}else{
Point2D p1 = getPointViaRotation(v1, dir, newLength);
recurse(v1,p1,n-1);
dir+=(Math.PI/3.);
Point2D p2 = getPointViaRotation(p1,dir,newLength);
recurse(p1,p2,n-1);
dir-=(Math.PI*(2./3.));
Point2D p3 = getPointViaRotation(p2, dir, newLength);
recurse(p2,p3,n-1);
dir+=(Math.PI/3.);
recurse(p3,v2,n-1);
}
}
I really suspect my math is the problem, but this looks correct to me:
public static Point2D getPointViaRotation(Point2D p1, double rotation, double length){
double xLength = length * Math.cos(rotation);
double yLength = length * Math.sin(rotation);
return new Point2D.Double(xLength + p1.getX(), yLength + p1.getY());
}
N = 0 (All is well):
N = 1 (Perhaps a little bendy, maybe)
N = 5 (WAT)
I can't see any obvious problem code-wise. I do however have a theory about what happens.
It seems like all points in the graph are based on the locations of the points that came before it. As such, any rounding errors that occurs during this process eventually start accumulating, eventually ending with it going haywire and being way off.
What I would do for starters is calculating the start and end points of each segment before recursing, as to limit the impact of the rounding errors of the inner calls.
One thing about Koch's snowflake is, that the algorithm will lead to a rounding issue one time (it is recursive and all rounding errors add up). The trick is, to keep it going as long as possible. There're three things you can do:
If you want to get more detailed, the only way is to expand the possibilities of Double. You will need to use your own range of coordinates and transform them, every time you actually paint on the screen, to screen coordinates. Your own coordinates should zoom and show the last recursion step (the last triangle) in a coordination system of e.g. 100x100. Then calculate the three new triangles on top of that, transform into screen coordinates and paint.
The line dir=Math.PI/3; divides by 3 instead of (double) 3. Add the . after the 3
Make sure you use Point2D.Double anywhere. Your code should do so, but I would explicitely write it everywhere.
You won the game, when you still have a nice snowflake but get a Stackoverflow.
So, it turns out I am the dumbest man alive.
Thanks everyone for trying, I appreciate the help.
This code is meant to handle an equilateral triangle, its very specific about that (You can tell by the angles).
I put in a triangle with the height equal to the base (not equilateral). When I fixed the input triangle, everything works great.
i am using haversine formula in java programming (using eclipse).
My problem is at the end of the equation where i get an error (atan is underlined) saying that "The method atan(double) is undefined for the type Math".
i dont know what is the problem. Does anyone have an idea?
double angle = 45;
double arctan = Math.atan(angle);
Do you have any other Math class in the same package? Maybe you could try:
double angle = 45;
double arctan = java.lang.Math.atan(angle);
The only possible reason that I can think of for why this might happen is that there is a namespace conflict. That is to say that the compiler thinks the Math in your method is referencing some other Class called Math instead of java.lang.Math
In eclipse if you hover your mouse cursor over Math it will bring up a context window showing you what package it thinks Math belongs to. If it's not java.lang then that's your problem.
Are you programming mobile application and using Java ME? In that case the Math library really doesn't contains atan function. So you have to calculate atan by yourself. This topic was discussed here and also here.