Function to translate end coordinates of a line - java

I have two points at the end of a line.
I need to get the end coordinates of a translation of this line.
The translation will be a parallel line that is a distance d from the original line.
here is an image of what i need:
So I need a function that I can pass these two points and the distance and get the two new coordinates in returns.
I have been stuck on this problem for a while. Any help will be appreciated!
Thanks you!

The new co-ordinates will be the resulting vector of
distance d multiplied by normalized vector of which direction it's moving, added to the original vector point.
EDIT:
Given the two points of the line, you will need to calculate the normal of the vector joining these points. Information on that is here..
Normalise this vector, multiply by d, add to each point.

Calculate the vector (x2-x1,y2-y1). This is a vector in the direction of your line. A normal vector is then given by
(-(y2-y1),-(x2-x1)) = (y1-y2,x1-x2).
Divide this vector by its size to get the unit vector in the direction you want
A = (y1-y2,x1-x2)/|(y1-y2,x1-x2)|
Now given your distance d your translated point will be given by
NewPoint = OldPoint + d * A

Related

Getting a point from begginning coordinates, angle and distance

I want to get a Vector containing a coordinate. I know my beginning coordinates, angle and distance. So far I've tried doing:
Vector2 pos = new Vector2(beginningX, beginningY).add(distance, distance).rotate(angle);
But it doesn't work as I expect it to. When the rotation isn't 0 the coordinates become big, and the ending point isn't where I expect it to be. I know this must be a simple problem, but I just can't solve it.
EDIT:
Tried doing:
Vector2 pos = new Vector2(beginningX, beginningY).add(distance, 0).rotate(angle);
(Adding distance to x only) Still no success.
I'd say you're doing it wrong: you need to rotate the distance vector and add it to the position vector:
Vector2 pos = new Vector2(beginningX, beginningY).add(new Vector2(distance, 0).rotate(angle) );
You might want to read up on vector math but basically it amounts to this (if I correctly understood what you're trying to do):
If you rotate a vector you're always rotating around point 0/0. Thus you'll want to create a vector that covers the distance from 0/0 to your distance on the x-axis:
0---------------->d
Now you rotate that vector by some angle:
d
/
/
/
/
/
0
Then you offset that vector by your starting point, i.e. you add the two vectors (for simplicity I assume your starting point lies on the y-axis):
d
/
/
/
/
/
s
|
|
|
0
You need to rotate only the distance vector, rather than a sum of beginning and distance. Addition is the same in either order (commutative), so you can try this way:
Vector2 pos = new Vector2(distance, 0).rotate(angle).add(beginningX, beginningY);
Advantage: This chained call does not create a temporary Vector2 for the beginning position that would immediately become garbage for the garbage collector. Conserving space and garbage collection time will be important when your code handles millions of vectors.
This is simple vector addition. I'm assuming 2D coordinates, with angles measured counterclockwise from x-axis:
x(new) = x(old) + distance*cos(angle)
y(new) = y(old) + distance*sin(angle)
Be sure that your angles are in radians when you plug them into trig functions.

Wraping of equispaced point set in a given direction

Let us consider a 2-D (latitude, longitude) point set. The points in the set are equispaced (approx.) with mutual distance 0.1 degree \times 0.1 degree . Each point in the set is the centre of a square grid of side length 0.1 degree (i.e., intersect point of two diagonals of the square). Each square is adjacent to the neighbouring squares.
Our goal is to get the coordinates of the outline polygon formed by the bounding sides of the square grids with given direction (will be illustrated with a figure). This polygon has no hole inside.
Let us consider a sample data set of size 10 (point set).
lat_x <- c(21.00749, 21.02675, 21.00396, 21.04602, 21.02317,
21.06524, 21.00008, 21.04247, 21.08454, 21.0192)
and
lon_y <- c(88.21993, 88.25369, 88.31292, 88.28740, 88.34669,
88.32118, 88.40608, 88.38045, 88.35494, 88.43984)
Here is the rough plot of the above points followed by some illustration,
The black points are the (lat,lon) points in the above sample.
The blue square boxes are the square grid.
The given directions (\theta) of the squares are $theta$=50 degree.
Our goal is to get the ordered (clockwise or counter clockwise) co-ordinates of the outline polygon (in yellow colour).
Note: This question is very similar to this with a nice answer given by #laune. There the goal is to get the outline polygon without direction (or 0 degree direction). But in the the present set up I need to include the direction (non-zero) while drawing the square grids and the resulted polygon.
I would gratefully appreciate any suggestion, java or R codes or helpful reference given by anyone solving the above problem.
I would do it like this:
may be some 2D array point grouping to match the grid
that should speed up all the following operations.
compute average grid sizes (img1 from left)
as two vectors
create blue points (img2)
as: gray_point (+/-) 0.5*blue_vector
create red points (img3)
as: blue_point (+/-) 0.5*red_vector
create list of gray lines (img4)
take all 2 original (gray) points that have distance close to average grid distance and add line for them
create list of red lines (img4)
take all 2 original (gray) points that have distance close to average grid distance and add line for them if it is not intersecting any line from gray lines
reorder line points to match polygon winding ...
angle
compute angle of red vector (via atan2)
compute angle of blue vector (via atan2)
return the one with smaller absolute value
[edit1] response to comments
grid size
find few points that are closest to each other so pick any point and find all closest points to it. The possible distances should be near:
sqrt(1.0)*d,sqrt(1+1)*d,sqrt(1+2)*d,sqrt(2+2)*d,...
where d is the grid size so compute d for few picked points. Remember the first smallest d found and throw away all that are not similar to smallest one. Make average of them and let call it d
grid vectors
Take any point A and find closest point B to it with distance near d. For example +/-10% comparison: |(|A-B|-d)|<=0.1*d Now the grid vector is (B-A). Find few of them (picking different A,B) and group them by sign of x,y coordinates into 4 groups.
Then join negative direction groups together by negating one group vectors so you will have 2 list of vectors (red,blue direction) and make average vectors from them (red,blue vectors)
shifting points
You take any point A and add or substract half of red or blue vector to it (not its size!!!) for example:
A.x+=0.5*red_vector.x;
A.y+=0.5*red_vector.y;
line lists
Make 2 nested fors per each 2 point combination A,B (original for gray lines,shifted red ones for red outline lines) and add condition for distance
|(|A-B|-d)|<=0.1*d
if it is true add line (A,B) to the list. Here pseudo C++ example:
int i,j,N=?; // N is number of input points in pnt[]
double x,y,d=?,dd=d*d,de=0.1*d; // d is the avg grid size
double pnt[N][2]=?; // your 2D points
for (i=0;i<N;i++) // i - all points
for (j=i+1;j<N;j++) // j - just the rest no need to test already tested combinations
{
x=pnt[i][0]-pnt[j][0];
y=pnt[i][1]-pnt[j][1];
if (fabs((x*x)+(y*y)-dd)<=de) ... // add line pnt[i],pnt[j] to the list...
}

How do i find if a set of coordinates are inside a 2D triangle?

Does anybody know how to find out if a set of coordinates are within a triangle for which you have the coordinates for. i know how to work out length of sides, area and perimeter, but i have no idea where to begin working out the whereabouts within the triangle of other points.
Any advice would be appreciated
You can create a Polygon object.
Polygon triangle = new Polygon();
Add the vertexes of your triangle with the addPoint(int x, int y) method.
And then, you just need to check if the set of coordinates is inside your triangle using contains(double x, double y) method.
Use the contains method of the Polygon class as documented here.
For a solution without using the Polygon-class:
Assume that you have giving three points A,B,C the vertices of your polygon. Let P be the point you want to check. First calculate the vectors representing the edges of your triangle. Let us call them AB, BC, CA. Also calculate the three vectors PA, PB, PC.
Now calculate the cross product between the first two of the vectors from above.
The cross product of the first pair gives you the sin(alpha), where alpha is the angle between AB and PA, multiplied with a vector pendenpicular to AB and PA. Ignore this vector because we are interested in the angle and take a look at the sine (in the case of 2D vectors you can imagine it as the vector standing perpendicular to your screen).
The sine can take values between (let's say for the ease) betwenn 0 and 2*Pi. It's 0 exactly at 0 and Pi. For every value in between the sine is positive and for every value between Pi and 2*Pi it's negative.
So let's say your Point p is on the left hand side of AB, so the sine would be positive.
By taking the cross product of each pair from above, you could easily guess that the point P is on the left hand side of each edge from the triangle. This just means that it has to be inside the triangle.
Of course this method can even be used from calculating whether a point P is in a polygon. Be aware of the fact, that this method only works if the sides of the polygon are directed.

how to discover an angle between two objects?

what I want to do is the following: I have an object (blue point) and I want to point it to other object no matter where it is located around it (green point). So I need to know the angle between these two objects to do what I want right?
http://s13.postimage.org/6jeuphcdj/android_angle.jpg
The problem is, I don't know what to do to achieve this. I've already used atan, math.tan and so many other functions but without any good results.
Could you help me? Thanks in advance.
Calculate a dot product of object vectors. Use Math.acos on the value you get. That will give you an angle in radians.
So, say your blue dot is at vec1 = (50, 100) and green one at vec2 = (100, 400).
A tuple (x, y) as a two dimensional vector describes object's position and distance from (0, 0) on your screen. To find the angle between these two vectors, you do a standard, binary dot product operation on them. This will get you a scalar (a value, cos(Theta)), but you want the inverse of it (acos) which is the angle you're looking for.
You can get a better understanding on the matter here
Suppose the coordinates of the blue and green points are (xblue, yblue) and (xgreen, ygreen) respectively.
The angle at which the blue point sees the green point is:
double angleRadians = Math.atan2(ygreen-yblue, xgreen-xblue);
If you want the angle in degrees:
double angleDegrees = Math.toDegrees(angleRadians);

Java Vector3d And Vector Logic

First of all I am having trouble with the concept of a mathematical Vector when being applied to a Vector3d. I understand a vector to be just like a line except it has a direction property.
Now, Vector3d only takes in 3 arguments (x,y,z) in its constructor. I assumed this to be because the vector initially is assumed to start at the origin and go through the designated point. How can we ever have a vector which does not intersect the origin if the only attributes contained in Vector3d are x,y,z?
For example, I am trying to program a function which calculates the distance between two points on a sphere:
//Returns the shortest distance between two points on a sphere
public static double getGreatCircleDistance(Point3d p1, Point3d p2){
Vector3d v1 = getVector(viewSphereOrigin, p1);
Vector3d v2 = getVector(viewSphereOrigin, p2);
v1.normalize();
v2.normalize();
return Math.acos(v1.dot(v2)*(viewSphereDiameter/2));
}
//Returns a vector through two given Points in 3d space
public static Vector3d getVector(Point3d start, Point3d terminal){
return new Vector3d(terminal.x-start.x, terminal.y-start.y, terminal.z-start.z);
}
I do not understand however how getVector() can return a vector which passes through the two given points. I researched how to obtain a vector connecting two points:
http://emweb.unl.edu/math/mathweb/vectors/vectors.html#vec6
however fundamentally I still do not understand this. Can someone clear up the logic behind Vector3d for me please and how it can represent an arbitrary vector in 3D space yet it only contains x,y,z?
Thanks!
A vector is not a line; it is an entity with a magnitude and a direction. What it does not have is location.
You can represent a vector as the line segment between any two points: the magnitude is the distance between the points, and the direction is given by the direction of the line. Every time the distance and direction of the line are the same you get the same vector.
Since a vector does not have a location, we can arbitrarily choose one of the points to be the origin. This way a triple «x,y,z» uniquely represent a vector with the direction of the line from the point (0,0,0) to the point (x,y,z). But you get the same vector also by picking the points (5,4,2) and (5+x, 4+y, 2+z), or any other two points that are at the same distance and direction from each other.
As I know Vector3D means a [x,y,z] starting from the origin. If you would like to store a vector in 3D space (called Ray) you should store two Vector3D:
1. the starting point and the end point of the vector
OR
2. the origin and the direction of the vector.

Categories