Distance calculating algorithm for an app - java

I'm trying to calculate the distance for a third grader's geometry game app I'm developing. The basic idea is that the user inputs the directions using directions and how long he wants to go in each direction.( eg input: Right (radians) , travel a metres, left (radians), travel b metres).
So, now my app will find out if the user can get back to the starting point and how far he is away from home.
So far, I have been able to proceed with this logic here. I would like to know if there are other ways to do this? thanks!

You can do it using third grade geometry fittingly.
When you use the commands you listed, you are creating a vector. If you store dir, x, and y you can use simple sin and cosin to figure out your ending location.
dir+=rad
x+=cosin(dir)
y+=sin(dir)
If you have saved your start location, you can use Pythagorean Theorem to figure out the displacement from home!
public double distance (int x, int y){
int xdist = x-startx;
int ydist = y-starty;
int squarex = xdist*xdist
int squarey = ydist*ydist
return Math.sqrt((double)(squarex+squarey));
}

Related

Java Library To Animated Movement Between Coordinates

I am working on an application that deals with moving objects from point A to point B in a 2D space. The job of the application is to animate that transition in a given number to steps (frames).
What I am currently doing is divide the distance by the number steps, hence creating a very linear and boring movement in a straight line:
int frames = 25;
int fromX = 10;
int toX = 20;
double step = (toX - fromX) / frames;
List<Double> values = new ArrayList<>();
int next = start;
for (int i = 0; i < frames; i++) {
values.add(next);
next += step;
}
As a first improvement - since my poor users have to look at this misery - I would like that to be an accelerated and decelerated movement starting slow, picking up speed, then getting slower again until arrival at the destination.
For that particular case, I could probably figure out the math somehow but in the end, I want to be able to provide more complex animations that would go beyond my capabilities as a mathematician ;) I have many of the capabilities of e.g. PowerPoint or iMovie in mind.
My ask is: Is there a library that would allow me to generated these sequences of coordinates? I found a few things but they where often tied to some Graphics object etc which I am not using. For me it's all about Lists of Doubles.

Why Command Prompt and calculator gives different answer?

enter image description hereI'm learning how those arithmetic operation and using notepad and command prompt and checking those if its right at my phone's calculator and laptops calculator and it gives different answer . I'm using java language
I've tried computing it manually
Int x =12;
float y = 13.54f;
System.out.println(x/y+" devide 12 - 13.54");//0.88691795 on my calcu i get 0.886262924667...
TL;DR: Unable to reproduce.
From question:
I've tried computing it manually
Int x =12;
float y = 13.54f;
System.out.println(x/y+" devide 12 - 13.54");//0.88691795 on my calcu i get 0.886262924667...
I don't know where you get 0.88691795
When I run your code, I get 0.88626295
Here is extended version of your code, with output:
int x = 12;
float y = 13.54f;
double z = 13.54;
System.out.println(x/y + " using float math");
System.out.println(x/z + " using double math");
System.out.println("0.886262924667... using calcu");
0.88626295 using float math
0.8862629246676514 using double math
0.886262924667... using calcu
As you can see, none of it gives the result you claim to get. Please try again.

How to find the closest target given the start point coordinates?

There are 2 targets in my game. I am using Breadth First Search to find the path to one of them. I want to know know how can I determine which is the closest target using their coordinates. I have X and Y coordinates of my targets (all int type).
int result = 0;
int target1dist = (playerX - target1x) + (playerY - target1y);
int target2dist = (playerX - target2x) + (playerY - target2y);
if (target1dist < target2dist){
result = BFS(target1x,target1y,playerX ,playerY);
} else {
result = BFS(target2x,target2y,playerX ,playerY);
}
This was not effective when the distance of one of the targets was negative. So I added absolute value function. So target1dist and target2dist cannot be negative.
int target1dist =Math.abs ((playerX - target1x) + (playerY - target1y));
Would this be a effective way to find the closest target so my AI player can direct towards it? P.S: My Ai player can only go 4 directions so diagonals are excluded. My other thought was to use Pythagoras to find the distance.
With a slight modification your method should work. You don't want to take the absolute value of the full expression, but each component:
int traget1dist = Math.abs(playerX - target1x) + Math.abs(playerY - target1y);
This is called Manhattan distance and is a common measurement for tile based searches as yours. As Breadth-First-Search is guaranteed to find the optimal solution your solution will work.

Distance formula

I need help with a lab. My teacher did not tell us anything and he told us to figure it out. So i can use any help I can get. Thank You
Step 1: Is everything good with step one including my distance formula and I am trying to set xx equal to x and yy equal to y so I have not clue how to do that could could you help me with that.
class Point {
private int x;
private int y;
public Point() // (0, 0)
{
x = 0;
y = 0;
}
public Point(int xx, int yy)
{
xx = x;
yy = y;
}
public int getX() // return field values
{
return x;
}
public int getY()
{
return y;
}
// Use the distance formula to find the distance of this point from the origin (0,0)
public double distanceFromOrigin()
{
int d = 0;
d = Math.squrt(Math.pow(xx - x) + (Math.pow(yy - y);
}
Is my distance formula good if not what is wrong and can you help me fix it.
Step 2: I need to find the manhattan distance
// Find the "manhattan" distance between current point and other.
// You can look at http://x...content-available-to-author-only...t.gov/dads//HTML/manhattanDistance.html for help
public double distance(Point other)
{/*write the code for here*/}
I really have no clue how to do step 2 so i can really use a lot of help thank you guys so much
Step 3: After i find the manhattan distance between current point and other. I need to change it to its new values which i have no clue how to get the new value and how to change it. After that i need to shift the point by the translation T which i have no clue how to do that so i need help on that
// Changes the coordinate to new values
public void setLocation(int x, int y)
{/*write the code for here*/}
//Shift the point by the translation T<x+h,y+k>
public void translate(int h, int k)
{/*write the code for here*/}
by the way guys I am just on the basic computer science so I can't use all the fancy stuff so keep it basic and simple
Like torvin said in the comments, that really looks like Java and not Javascript. Those are two different languages, despite the similar names.
Generally looking at your code, there are obvious mistakes, which make me wonder what IDE (integrated development environment) you are using to write code. A proper program that helps you write code will spot those.
Just in your distanceFromOrigin() you are missing:
2 closing bracets
Math.squrt() should be Math.sqrt()
You are not returning a value, despite having set the methods return type to double
You should probably first set up an IDE and do a basic Java tutorial before continuing, honestly.

Apollonian gaskets - algorithm

The task is definitely described there Recursively create apollonian gaskets [With solution]
But I tried to find the radius of all the circles. So, I used Descartes' theorem for it. You can find radius using the curvatures.
But there is error in algorithm, because the output differs from the check data.
How do you think, where is it?
Formule of finding the curvatures of the new circle.
public static double next(double a,double b, double c){
return (a+b+c+2*Math.sqrt(a*b+b*c+c*a));
}
The code of finding all the curvatures.
Radius = 1/curvature.
Su = sum of areas of all circles in the gasket except for the bigggest circle.
Ar = sum of perimeters of all circles in the gasket except for the bigggest circle.
public static void newCircle(double a,double b, double c){
double k=next(a,b,c);
Su=(float) (Su+2*(2*Math.PI*(1/k)));
Ar=(float) (Ar+2*(Math.PI*((1/k)*(1/k))));
if(2*(1/k)>minD){
newCircle(a,c,k);
}
}
I suggest, there must be somthing, that would make my algorithm truly. For example, output should be '2439.258588 835.263228', but it is '2161.9133 499.28397'.
But I can`t find, what is incorrect.
Maybe, new circles should be found in another way, I think, that one circle gives 2 new(that have the same radius). First 2 circles and one Big gives 2 circles, than one Big, one of the first and new circle give 2...
So
'newCircle(k1,k2,next);
newCircle(k1,k3,next);'
'k1'-Big
'k2,k3' - first ones
Well, now there is good solving Recursively create apollonian gaskets [With solution]

Categories