Find a point along an angled line - math

I'm trying to figure out how to find a point along a line (half way, to be precice).
I need this to put a particle emitter in the correct location to leave a smoke-trail after bullets.
I've got point A and point C. Point A is the barrel-muzzle, and point C is found using ray-cast. Now, in order to put the emitter in the right location I need to find point D. How do one do this? I attached a picure to make it more visual.
No, I could not attach the picture, but here's a link.
Thanks in advance.
-Pimms

If your point is half way along a line between two points then you can just average their x and y co-ordinates to get the x and y for the midpoint (works in any number of dimensions).
If you want a point a certain proportion (ie 1/10th) along then you would do 1/10th of one point plus 9/10th of the other point.
In your example Point D is mid way between point A and C. This means the co-ordinates of D would be:
X = (0+10)/2 = 5
Y = (0+7)/2 = 3.5

Do I get you right? D is halfway between A and C ?
Solution:
D = (A + C) / 2
or:
D.x = (A.x + C.x) / 2
D.y = (A.y + C.y) / 2

Related

How can I find where a point will touch a line given a vector?

Here, line segment ab is cast upward on arbitrary vector n where I do somethings to find the black point on the line segment cd. My question is, how do I find the point on ab that intersects with the inverted n vector coming down from the new point?
Looks like it will have the same x-coordinate as the black point (call this x). The slope of ab is m = (by - ay) / (bx - ax), so the y coordinate is mx + ay.
If the projection is parallel, by the Thales theorem the ratios are preserved.
|ae| / |ab| = |cf| / |cd| = r
which is known.
The searched point is, vectorially
e = a + r.ab = a + |cf|/|cd|.ab

Finding out if a point is within a line with a given width

I'm a little lost on the math aspect if what I need to do.
I have 3 points: Point A, Point B, Point C.
I need to find out if point C is on the line segment from Point A to Point B. But... the caveat is that I at the same time need to make the line "wider" from Point A to B.
I'm guessing I need to first create a bounding box that surrounds A->B? Then check if I'm within the bounding box?
How do I go about creating that box..
A and B can negative or positive on the grid, and the "distance" of the box is changeable as well.
I'm hoping this picture illustrates it better. The distance is the total given, so half would go 1 direction, half the other.
Even if I can just get those 4 points of the box, I can do the simple check to see if C is within.
Define u = normalize(b - a) and v = (-u.y, u.x).
Your point c is inside the line segment from a to b of width w if and only if both of the following hold:
0 <= dot(c - a, u) <= length(b - a)
abs(dot(c - a, v)) < w / 2
You have to (web picture with another point names P0=A, P1=B, P=C):
1) make orthogonal projection of C onto AB line (point D)
2) find distance CD and check if it is less than half-width
3) check that D lies between A and B (inside the segment)
for 2: distance d = |AC x uAB| - norm of cross product of AC vector and unit direction vector uAB = AB/|AB|
for 3: true if DotProduct(AC, AB) >= 0 and DotProduct(BC, BA) >= 0
Here you have all necessary formulas https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
First you find shortest distance btw point C and the line and check that it is less equal that width/2. Then you find coordinates of point D - closest to C point on the line and check that this point is between A and B. Than can be done by checking that distance(A,D) + distance(B,D) == distance( A,B )

Triangulating coordinates with an equation

Ok, I know this sounds really daft to be asking here, but it is programming related.
I'm working on a game, and I'm thinking of implementing a system that allows users to triangulate their 3D coordinates to locate something (eg for a task).
I also want to be able to let the user make the coordinates of the points they are using for triangulation have user-determined coordinates (so the location's coordinate is relative, probably by setting up a beacon or something).
I have a method in place for calculating the distance between the points, so essentially I can calculate the lengths of the sides of the triangle/pyramid as well as all but the coordinate I am after.
It has been a long time since I have done any trigonometry and I am rusty with the sin, cos and tan functions, I have a feeling they are required but have no clue how to implement them.
Can anyone give me a demonstration as to how I would go about doing this in a mathematical/programatical way?
extra info:
My function returns the exact distance between the two points, so say you set two points to 0,0,0 and 4,4,0 respectively, and those points are set to scale(the game world is divided into a very large 3d grid, with each 'block' area being represented by a 3d coordinate) then it would give back a value at around 5.6.
The key point about it varying is that the user can set the points, so say they set a point to read 0,0,0, the actual location could be something like 52, 85, 93. However, providing they then count the blocks and set their other points correctly (eg, set a point 4,4,0 at the real point 56, 89, 93) then the final result will return the relative position (eg the object they are trying to locate is at real point 152, 185, 93, it will return the relative value 100,100,0). I need to be able to calculate it knowing every point but the one it's trying to locate, as well as the distances between all points.
Also, please don't ask why I can't just calculate it by using the real coordinates, I'm hoping to show the equation up on screen as it calculates the result.7
Example:
Here is a diagram
Imagine these are points in my game on a flat plain.
I want to know the point f.
I know the values of points d and e, and the sides A,B and C.
Using only the data I know, I need to find out how to do this.
Answered Edit:
After many days of working on this, Sean Kenny has provided me with his time, patience and intellect, and thus I have now got a working implementation of a triangulation method.
I hope to place the different language equivalents of the code as I test them so that future coders may use this code and not have the same problem I have had.
I spent a bit of time working on a solution but I think the implementer, i.e you, should know what it's doing, so any errors encountered can be tackled later on. As such, I'll give my answer in the form of strong hints.
First off, we have a vector from d to e which we can work out: if we consider the coordinates as position vectors rather than absolute coordinates, how can we determine what the vector pointing from d to e is? Think about how you would determine the displacement you had moved if you only knew where you started and where you ended up? Displacement is a straight line, point A to B, no deviation, not: I had to walk around that house so I walked further. A straight line. If you started at the point (0,0) it would be easy.
Secondly, the cosine rule. Do you know what it is? If not, read up on it. How can we rearrange the form given in the link to find the angle d between vectors DE and DF? Remember you need the angle, not a function of the angle (cos is a function remember).
Next we can use a vector 'trick' called the scalar product. Notice there is a cos function in there. Now, you may be thinking, we've just found the angle, why are we doing it again?
Define DQ = [1,0]. DQ is a vector of length 1, a unit vector, along the x-axis. Which other vector do we know? Do we know of two position vectors?
Once we have two vectors (I hope you worked out the other one) we can use the scalar product to find the angle; again, just the angle, not a function of it.
Now, hopefully, we have 2 angles. Could we take one from the other to get yet another angle to our desired coordinate DF? The choice of using a unit vector earlier was not arbitrary.
The scalar product, after some cancelling, gives us this : cos(theta) = x / r
Where x is the x ordinate for F and r is the length of side A.
The end result being:
theta = arccos( xe / B ) - arccos( ( (A^2) + (B^2) - (C^2) ) / ( 2*A*B ) )
Where theta is the angle formed between a unit vector along the line y = 0 where the origin is at point d.
With this information we can find the x and y coordinates of point f relative to d. How?
Again, with the scalar product. The rest is fairly easy, so I'll give it to you.
x = r.cos(theta)
y = r.sin(theta)
From basic trigonometry.
I wouldn't advise trying to code this into one value.
Instead, try this:
//pseudo code
dx = 0
dy = 0 //initialise coordinates somehow
ex = ex
ey = ey
A = A
B = B
C = C
cosd = ex / B
cosfi = ((A^2) + (B^2) - (C^2)) / ( 2*A*B)
d = acos(cosd) //acos is a method in java.math
fi = acos(cosfi) //you will have to find an equivalent in your chosen language
//look for a method of inverse cos
theta = fi - d
x = A cos(theta)
y = A sin(theta)
Initialise all variables as those which can take decimals. e.g float or double in Java.
The green along the x-axis represents the x ordinate of f, and the purple the y ordinate.
The blue angle is the one we are trying to find because, hopefully you can see, we can then use simple trig to work out x and y, given that we know the length of the hypotenuse.
This yellow line up to 1 is the unit vector for which scalar products are taken, this runs along the x-axis.
We need to find the black and red angles so we can deduce the blue angle by simple subtraction.
Hope this helps. Extensions can be made to 3D, all the vector functions work basically the same for 3D.
If you have the displacements from an origin, regardless of whether this is another user defined coordinate or not, the coordinate for that 3D point are simply (x, y, z).
If you are defining these lengths from a point, which also has a coordinate to take into account, you can simply write (x, y, z) + (x1, y1, z1) = (x2, y2, z2) where x2, y2 and z2 are the displacements from the (0, 0, 0) origin.
If you wish to find the length of this vector, i.e if you defined the line from A to B to be the x axis, what would the x displacement be, you can use Pythagoras for 3D vectors, it works just the same as with 2D:
Length l = sqrt((x^2) + (y^2) + (z^2))
EDIT:
Say you have a user defined point A (x1, y1, z1) and you want to define this as the origin (0,0,0). You have another user chosen point B (x2, y2, z2) and you know the distance from A to B in the x, y and z plane. If you want to work out what this point is, in relation to the new origin, you can simply do
B relative to A = (x2, y2, z2) - (x1, y1, z1) = (x2-x1, y2-y1, z2-z1) = C
C is the vector A>B, a vector is a quantity which has a magnitude (the length of the lines) and a direction (the angle from A which points to B).
If you want to work out the position of B relative to the origin O, you can do the opposite:
B relative to O = (x2, y2, z2) + (x1, y1, z1) = (x1+x2, y1+y2, z1+z2) = D
D is the vector O>B.
Edit 2:
//pseudo code
userx = x;
usery = y;
userz = z;
//move origin
for (every block i){
xi = xi-x;
yi = yi - y;
zi = zi -z;
}

Finding Projection and z distance

I have an image that represents a projection. I am going to explain the problem with an example:
In the screen, there is a line from one point E(100,200) to another point
H (150,100). A represent one point
that in the real world is at 200 cm of
distance while B is a point that in
real world is at 300 cm of distance.
The thing that I would like to know is this:
Given one point of the line that passes for these two points, is there a way to calculate the z distance data that it should have?
What if the z distance is not a linear function but is some logarithmic function?
If it's not clear ask me everything,
Cheers
I think what you're getting at is perspective correct interpolation. If you know the depth at E and a depth at H, and B is on the line (in the image) joining these two points, solve for the depth at B with:
1/Zb = s * 1/Ze + (1-s) * 1/Zh
where s is the normalized distance/interpolation parameter (between 0 and 1) along the line in screen space, meaning B = s * E + (1-s) * H
Use homogeneous coordinates, which can be linearly interpolated in screen space (for depth and texture): http://www.cs.unc.edu/~olano/papers/2dh-tri/

How to find angle of reflected Ray to match a Point

this is for a Tank game I am making
Please see pic for a clear idea :link text
I want to precompute the exacte angle to hit Point T2.
T1:point start
T2:point Target
V1(a,b):line
reflect point : this is what I m looking for :)
Edit:it would be cool to see some "Code" :p
It'd be useful to see what happens to lines/vectors during reflection. Wikipedia provides a nice picture for this:
Where, in this picture, in a proper reflection, both angles are the same.
Now, what does that have to do with you? Let's take a look again at your situation.
Note that, due to the laws of reflection, the angles a and b are equal. That's good for us, because if we know that, we know c and d are also equal! (They are right triangles)
So we know:
a = b
c = d
We soon realize that we have similar triangles. Meaning, the corresponding sides are proportional to eachother. Meaning, mathematically:
A / C = B / D
A / B = C / D
A / (A+B) = B / (A+B) = C / P = D / P
So, if you know A and B, which you should, you can find your reflection point by adding C to the x value of the intersection.
You can find C this way:
Given:
A (distance from shooting tank to wall)
B (distance from target tank to wall)
P (x distance between points)
Find:
C (x distance from shooting tank where wall is to be hit)
A / (A+B) = C / P
C = A*P / (A+B) <- here it is
For example, if your first tank is at (1,5) and your second tank is at (3,7), and your wall is the x axis:
A = 5
B = 7
P = 3-1 = 2
therefore:
C = 5*2 / (5+7)
= 10/12
= 5/6
So your tank should shoot towards (0,5/6) if it wants to hit a tank at (3,7).
For a more general solution:
if the wall is the X axis, and you have shooting tank at (s_x,s_y)
and hit tank at (h_x,h_y), the point to be shot at is:
[ s_x + s_y * (h_x-s_x) / (h_y + s_y), 0 ]
Alternative, with arbitrary wall placement/direction
The problem with the above solution is that your wall has to be your x axis. What if it's not?
First, you need to find the distance from each point to the wall -- A and B:
Find w, which is the unit vector in the direction of the wall.
From w, find v, which is the unit vector perpendicular to the wall. If w = [x,by], v = [-y,x].
Find r_s, which is the vector from your shooting tank to any known point on your wall.
Find r_h, which is the vector from your hit tank to any known point on your wall.
The distance A = | v . r_s |, where . is the dot product operator. This can be found by [l,m] . [n,o] = l*n + m*o
The distance B = | v . r_h |
Once you find A and B, find P, which is the distance parallel to the wall. To do that:
Find q, which is the vector from the hit tank to the shooting tank
The distance P = | w . q |
Now that you have A, B, and P, you have two ways to go:
Find the point on the wall to aim for, by first solving for C in the method above and then finding the intersection of v starting from your shooting tank and your wall, and adding C*w to that intersection point.
You can find the angle (from v) that you must shoot, and it's the inverse tangent of P/(A+B).
Reflect T2 on the other side of V1, using V1 as the axis of reflection (we'll call this new point T2'); The line between T1 and T2' will intersect V1 at the point you want. From that point it's a matter of simple trigonometry to figure out what any angles are.
http://en.wikipedia.org/wiki/Transformation_%28geometry%29#Reflection

Resources