I have written this code but it doesn't compute ((((bY - aY) / (maxX - minX)) * (p.getX() - minX)) + aY) for me.please help me.
a part of code :(I have four points which aY means the "y" of point "a")
int aY = minXY[0];
int bY = maxXY[0];
int cY = minXY[minXY.length - 1];
int dY = maxXY[maxXY.length - 1];
for (Point p : pointList) {
if ((p.getY() > ((((bY - aY) / (maxX - minX)) * (p.getX() - minX)) + aY)) && (p.getY() < ((((dY - cY) / (maxX - minX)) * (p.getX() - minX)) + cY))) {
list.add(p);
} else if (p.getY() < ((((bY - aY) / (maxX - minX)) * (p.getX() - minX)) + aY)) {
upperHull.add(p);
int m = ((((bY - aY) / (maxX - minX)) * (p.getX() - minX)) + aY);
System.out.println(m);
} else if (p.getY() > ((((dY - cY) / (maxX - minX)) * (p.getX() - minX)) + cY)) {
lowerHull.add(p);
}
}
the output will be an integer which is the "y" of point "a".
totally ,my specific arithmetic statement doesn't work.please help me why?also I have checked each value in the arithmetic statement and they are OK but can not compute the result for me.
specific arithmetic statement : ((((bY - aY) / (maxX - minX)) * (p.getX() - minX)) + aY)
It would be more helpful if you explained what result you are getting and what you don't like about it. "can not compute the result" implies you are getting an exception of some kind, but I don't think that's what you mean.
What you probably mean is that the result it is getting is not what you expect, and the most likely reason for this is that all your variables are int so it is doing integer arithmetic, but you probably want to get floating-point results from your divisions.
I suggest changing your local variables to float or double, so the ints will be automatically converted before doing the arithmetic.
Also, it looks like you have two complex arithmetic expressions in there multiple times. I would calculate them once and assign them to local variables (within the loop, before the if).
Related
Im trying to work out how to smooth a big list of longitude latitude points i currently draw a line from point to point and interpolate each point in-between but as i change direction it shows a sharp turn where i would like to smooth this out to a neat curve, but i dont know exactly where the turns are so i need an algorithm to smooth over an average of points.
I have done some research into this already and had a look at the Bezier curve but does not work as i expect, i took the implementation that i found here.
http://sourcecodesforfree.blogspot.co.uk/2013/05/33-bezier-curve.html
I cant really find much documentation on how the Bezier Curve algorithm works, or at least not fully understanding how it works.
function bspline(lats, lons) {
var i, t, ax, ay, bx, by, cx, cy, dx, dy, lat, lon, points;
points = [];
// For every point
for (i = 2; i < lats.length - 2; i++) {
for (t = 0; t < 1; t += 0.2) {
ax = (-lats[i - 2] + 3 * lats[i - 1] - 3 * lats[i] + lats[i + 1]) / 6;
ay = (-lons[i - 2] + 3 * lons[i - 1] - 3 * lons[i] + lons[i + 1]) / 6;
bx = (lats[i - 2] - 2 * lats[i - 1] + lats[i]) / 2;
by = (lons[i - 2] - 2 * lons[i - 1] + lons[i]) / 2;
cx = (-lats[i - 2] + lats[i]) / 2;
cy = (-lons[i - 2] + lons[i]) / 2;
dx = (lats[i - 2] + 4 * lats[i - 1] + lats[i]) / 6;
dy = (lons[i - 2] + 4 * lons[i - 1] + lons[i]) / 6;
lat = ax * Math.pow(t + 0.1, 3) + bx * Math.pow(t + 0.1, 2) + cx * (t + 0.1) + dx;
lon = ay * Math.pow(t + 0.1, 3) + by * Math.pow(t + 0.1, 2) + cy * (t + 0.1) + dy;
points.push(new google.maps.LatLng(lat, lon));
}
}
return points;
}
Above code in JavaScript else logic are same just change bit variables and method as JAVA then check
Or
please find an Algorithm from this link en.wikipedia.org/wiki/Interpolation#Linear_interpolation
public void move(){
double angle;
for(int i = 0; i < planets.size(); i++){
if(Math.abs(Math.sqrt(Math.pow(ship.locX - (planets.get(i).locX + planets.get(i).radi), 2) + Math.pow(ship.locY - (planets.get(i).locY + planets.get(i).radi), 2))) < planets.get(i).gravrange){
//Distance formula between spaceship and planets to determine whether the ship is within the influence of the planet.
angle = ((Math.atan2((planets.get(i).locX + planets.get(i).radi) - ship.locX, (planets.get(i).locY + planets.get(i).radi) - ship.locY)) / Math.PI) + 1;
//The problematic math equation.
Produces a double from 0 to 2, 0 being when ship.locY < planets.get(i).locY && ship.locX == (planets.get(i).locX - planets.get(i).radi). (when relative X = 0 and relative Y < 0.)
if(ship.locX > (planets.get(i).locX + planets.get(i).radi)){xm += Math.cos(angle) * planets.get(i).gravrate;}
else{xm -= Math.cos(angle) * planets.get(i).gravrate;}
if(ship.locY > (planets.get(i).locY + planets.get(i).radi)){ym += Math.sin(angle) * planets.get(i).gravrate;}
else{ym -= Math.sin(angle) * planets.get(i).gravrate;}
}
}
This uses the data to modify the X and Y velocities of the spacecraft.
This equation works for the majority of an orbit, but under certain circumstances has an issue in which the spacecraft undergoes a retrograde force, slowing it. Shortly afterward it begins to be repelled by the planetary body, which after a short period begins attracting it again. When the spacecraft reaches the original position at which this occurred, it begins to move in the opposite direction of its original orbit.
This continues to occur until the spacecraft begins a wavelike motion.
Is there a way to solve this, or am I simply using the wrong equation? I've been attempting to fix this for about two weeks now. I have no education in physics nor calculus at this point in time, so my understanding is limited.
Edit: The comments had questions about my math, so I'll attempt to answer them here. From what I know about atan2, it produces a number from -pi to pi. I divide by pi to produce a number from -1 to 1, then add 1 to produce 0 to 2. I then use this number as a radian measurement. My knowledge of radians (unit circle) is that a circle's radian measure is 0 to 2pi.
Edit 2: The following code has very different math but produces the desired results, save for issues of repelling rather than attracting when approaching the North and South 'poles' of the planet.
public void move(){
double angle;
double x1, x2, y1, y2;
for(int i = 0; i < planets.size(); i++){
x1 = ship.locX;
y1 = ship.locY;
x2 = planets.get(i).locX + planets.get(i).radi;
y2 = planets.get(i).locY + planets.get(i).radi;
if(Math.abs(Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2))) < planets.get(i).gravrange){
//Distance formula between spaceship and planets
angle = (y2 - y1)/(x2 - x1); //Gets slope of line between points.
if(angle > 0){
if(y1 > y2){
xm += Math.cos(angle) * planets.get(i).gravrate;
ym += Math.sin(angle) * planets.get(i).gravrate;
}else{
xm -= Math.cos(angle) * planets.get(i).gravrate;
ym -= Math.sin(angle) * planets.get(i).gravrate;
}
}
else{
if(y1 > y2){
xm -= Math.cos(angle) * planets.get(i).gravrate;
ym -= Math.sin(angle) * planets.get(i).gravrate;
}else{
xm += Math.cos(angle) * planets.get(i).gravrate;
ym += Math.sin(angle) * planets.get(i).gravrate;}
}
}
}
I wrote it up very quickly to see if using the slope of the line rather than that strange atan2 equation would help. Apparently it did. I also made the code a bit more readable in this section.
The following code fixed my issue. I was overcomplicating my math equations as I usually do. Took me three weeks of Googling, asking people with degrees in physics and mathematics, and reading Javadocs before I figured that one out. Turns out how atan2 works is simply different from how I thought it worked and I was improperly using it.
The solution was simplifying the atan2 equation beforehand and removing the unnecessary additions.
x1 = ship.locX;
y1 = ship.locY;
x2 = planets.get(i).locX + planets.get(i).radi;
y2 = planets.get(i).locY + planets.get(i).radi;
if(Math.abs(Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2))) < planets.get(i).gravrange){
//Distance formula between spaceship and planets
angle = Math.atan2((y2 - y1),(x2 - x1)); //Converts the difference to polar coordinates and returns theta.
xm -= Math.cos(angle) * planets.get(i).gravrate; //Converts theta to X/Y
ym -= Math.sin(angle) * planets.get(i).gravrate; //velocity values.
}
I have two equations (Distance and slope of a line formula)
d = sqrt( (x2 - x1)^2 + (y2 - y1)^2 )
m = (y2 - y1) / (x2 - x1)
Known: d, m, x1, y1
Unknown: x2, y2
The problem is the distance equation isn't linear...
Is there a way to code this in java (using Android compatible libraries) to solve? I tried doing simple guessing but it is too slow.
Thanks
EDIT: Code for triangle
Point p1 = new Point();
Point p2 = new Point();
projection.toPixels(gp1, p1);
projection.toPixels(gp2, p2);
Point p3 = new Point();
double slope = (p2.y - p1.y) / (p2.x - p1.x);
double x = 0;
if (p2.y - p1.y >= 0 && p2.x - p1.x >= 0) {
x = - Math.sqrt(600 / (1 + slope*slope)) + p2.x;
} else if (p2.y - p1.y >= 0 && p2.x - p1.x < 0) {
x = Math.sqrt(600 / (1 + slope*slope)) + p2.x;
} else if (p2.y - p1.y < 0 && p2.x - p1.x >= 0) {
x = - Math.sqrt(600 / (1 + slope*slope)) + p2.x;
} else if (p2.y - p1.y < 0 && p2.x - p1.x < 0) {
x = Math.sqrt(600 / (1 + slope*slope)) + p2.x;
}
double y = -slope*p2.x + slope*x + p2.y;
p3.set((int) x, (int) y);
double inverseSlope = 0;
if (slope == 0) {
inverseSlope = Double.MAX_VALUE;
} else {
inverseSlope = -1 / slope;
}
x = -Math.sqrt(300 / (1 + inverseSlope*inverseSlope)) + p3.x;
y = -Math.sqrt(300 / (1 + inverseSlope*inverseSlope))*inverseSlope + p3.y;
Point p4 = new Point();
p4.set((int) x, (int) y);
x = Math.sqrt(300 / (1 + inverseSlope*inverseSlope)) + p3.x;
y = Math.sqrt(300 / (1 + inverseSlope*inverseSlope))*inverseSlope + p3.y;
Point p5 = new Point();
p5.set((int)x, (int) y);
Path path = new Path();
path.moveTo(p2.x, p2.y);
path.lineTo(p4.x, p4.y);
path.moveTo(p4.x, p4.y);
path.lineTo(p5.x, p5.y);
path.moveTo(p5.x, p5.y);
path.lineTo(p2.x, p2.y);
path.moveTo(p2.x, p2.y);
canvas.drawPath(path, mPaint);
It appears it is being caused by slope is always an integer so when it is < 1 it is 0 which is not good...
Please review the algebra below:
Define
x = x2-x1
and
y = y2-y1
Then
m * x = y
and
d^2 = x^2 + m^2 * x^2 = (1 + m^2) * x^2
Therefore
x = sqrt(d^2 / (1 + m^2))
then
x2 - x1 = sqrt(d^2 / (1 + m^2))
so
x2 = sqrt(d^2 / (1 + m^2) + x1
Similarly
y = sqrt(d^2 / (1 + m^2)) * m
y2 = sqrt(d^2 / (1 + m^2)) * m + y1
So the answer is:
x2 = sqrt(d^2 / (1 + m^2)) + x1
y2 = sqrt(d^2 / (1 + m^2)) * m + y1
the "library" you're looking for is called mathematics :)
you can ask wolfram alpha:
http://www.wolframalpha.com/input/?i=solve+d+%3D+sqrt%28+%28x2+-+x1%29%5E2+%2B+%28y2+-+y1%29%5E2+%29%2C+m+%3D+%28y2+-+y1%29+%2F+%28x2+-+x1%29+over+the+reals
(don't ask me why it knows x2 and y2 are interresting)
you can do such things by hand, but be very careful not to drop signs. even though this stuff is taught in school they have little respect for detail. note that in this case there are two possible solutions and you need ensure that d > 0 in all cases!
I have the following code to determine the intersection of two 2D lines. It's not working and I'm not sure why; I've been copying code from multiple sources without much change.
The lines extend infinitely from a given midpoint. The "vector()" referenced in the below code is a 2D vector of magnitude 1.0 that indicates the direction in which the line extends (the line also extends in the negative direction, it's not a ray).
Earlier, I was using this method for determining the intersection: Intersection point of two lines (2 dimensions)
After that gave me wrong results, I went with the method on Wikipedia.
float x1 = line1.location().x();
float y1 = line1.location().y();
float x2 = x1 + line1.vector().x();
float y2 = y1 + line1.vector().y();
float x3 = line2.location().x();
float y3 = line2.location().y();
float x4 = x3 + line2.vector().x();
float y4 = y3 + line2.vector().y();
float d = ((x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4));
if(d == 0) // If parallel, defaults to the average location of the lines.
return new Vector2f((x1 + x3) * 0.5f, (y1 + y3) * 0.5f);
else
{
float a = (x1 * y2) - (y1 * x2);
float b = (x3 * y4) - (y3 * x4);
return new Vector2f(((a * (x3 - x4)) - ((x1 - x2) * b)) / d,
((a * (y3 - y4)) - ((y1 - y2) * b)) / d);
}
Two examples of how it is wrong (The orange dot is the returned point of intersection):q
It returns a point along the primary line, but it doesn't return a point on the second. the same bugs happen in both methods, so I'm really not sure what I'm doing wrong.
How can I fix this?
EDIT: Actually, this code works fine; my visualization code had an error.
I believe you've confused what the location() and vector() methods return. Unless i'm mistaken the location().x() gives you a point on the line created by the vector while vector().x() gives you the magnitude of the x value of the vector. (e.g. a horizontal has a vector().y() of 0)
You are adding the magnitude of the vector to the starting point. Instead try multiplying the starting point by the magnitude
float x2 = x1 * line1.vector().x();
float y2 = y1 * line1.vector().y();
As it turns out, the code was working; my visualization code had a bug in it. My bad.
Hi
this a part of my code :
I have a list which its size is three and I consider that 2 last items are in one line (p and q) I need to get the angle between the first item of this list and these two points (p,q)
private Point partition(List<Point> list, Point p, Point q) {
double x1 = p.getX();
double x2 = q.getX();
double y1 = p.getY();
double y2 = q.getY();
double pQ = Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
for (int i = 0; i < list.size()-2; i++) {
double pointX = list.get(i).getX();
double pointY = list.get(i).getY();
double pointQ = Math.sqrt((x2 - pointX) * (x2 - pointX) + (y2 - pointY) * (y2 - pointY));
double pointP = Math.sqrt((pointX - x1) * (pointX - x1) + (pointY - y1) * (pointY - y1));
double angle = Math.acos((pQ * pQ - pointP * pointP - pointQ * pointQ) /(- 2 * pointP * pointQ));
System.out.println(angle);
}
but instead of printing an angle for the first item it will print :(first item is not in the line of two last items).
1.6288442476732894
those points that print this result are :
[X :143.0 Y: 217.0, X :93.0 Y: 163.0, X :193.0 Y: 165.0]
please help me thanks.
EDITED : really it makes me confused .in such a way it will print this value ,sorry all !!!
Your arccos is bad, you need a parenthesis and a - :
arccos((pQ^2 - pointP^2 - pointQ^2)/(-2 * pointP * pointQ))
see How to calculate an angle from three points?
NaN is a special double value meaning "not a number". It is generated as a result of some "bad" calculations:
dividing 0 by 0
dividing infinity by infinity (any combination of positive and negative)
multiplying 0 by infinity, either positive and negative, and vice-versa
adding negative and positive infinity
subtracting negative from positive infinity, and vice-versa
the square root of a negative number
the logarithm of a negative number
the inverse sine or cosine of a number not between -1 and 1
any calculation involving one or more NaN values.
Check the result of the argument to the inverse cosine function (acos). I bet it is off the valid range.