Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I have a math problem. I have location (x,y) of point A, B, and a number (x). I want to calculate the location of point C, D. CD is perpendicular with AB and AC = AD = x.
This is the picture describe my problem:
Can anyone help me on this?
Thanks
You did not specify a programming language, but from your previous questions it seems that
you have some experience with (Objective-)C and Core Graphics on iOS, so this would be
a solution using C and Core Graphics data structures:
// Your input data:
CGPoint A = ...
CGPoint B = ...
CGFloat x = ...
// Vector from A to B:
CGPoint vecAB = CGPointMake(B.x - A.x, B.y - A.y);
// Length of that vector:
CGFloat lenAB = hypotf(vecAB.x, vecAB.y);
// Perpendicular vector, normalized to length 1:
CGPoint perp = CGPointMake(-vecAB.y/lenAB, vecAB.x/lenAB);
CGPoint C = CGPointMake(A.x + x * perp.x, A.y + x * perp.y);
CGPoint D = CGPointMake(A.x - x * perp.x, A.y - x * perp.y);
Your question is similar to How do you find a point at a given perpendicular distance from a line? . If you want further mathemetical details, you can refer: http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have two tensors a & b of shape (m,n), and I would like to compute a distance matrix m using some distance metric d. That is, I want m[i][j] = d(a[i], b[j]). This is somewhat like cdist(a,b) but assuming a generic distance function d which is not necessarily a p-norm distance. Is there a generic way to implement this in PyTorch?
And a more specific side question: Is there an efficient way to perform this with the following metric
d(x,y) = 1 - cos(x,y)
edit
I've solved the specific case above using this answer:
def metric(a, b, eps=1e-8):
a_norm, b_norm = a.norm(dim=1)[:, None], b.norm(dim=1)[:, None]
a_norm = a / torch.max(a_norm, eps * torch.ones_like(a_norm))
b_norm = b / torch.max(b_norm, eps * torch.ones_like(b_norm))
similarity_matrix = torch.mm(a_norm, b_norm.transpose(0, 1))
return 1 - similarity_matrix
I'd suggest using broadcasting: since a,b both have shape (m,n) you can compute
m = d( a[None, :, :], b[:, None, :])
where d needs to operate on the last dimension, so for instance
def d(a,b): return 1 - (a * b).sum(dim=2) / a.pow(2).sum(dim=2).sqrt() / b.pow(2).sum(dim=2).sqrt()
(here I assume that cos(x,y) represents the normalized inner product between x and y)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Suppose we have the equation of the parabola (y = x^2). See the figure below:
suppose we have a point P on this parabola, where P=(-2,4). We know that the distance between any point on parabola (e.g. P) and the focus is equal to the distance
between the point P and the directrix.
My question is, What the corresponding point (e.g. P') of P on the directrix of the parabola, where the distance between the focus and P = distance between P and P'? What is the equation which takes a point on the parabola and return the corresponding point on the directrix
Since focus and directrix have equal distance from any point on the parabola, they in particular have equal distance from the origin (0,0). You may assume that distance fo be d, so the focus would be at (0,d) and the directrix would be the line y=-d.
For any other point on the parabola, the squared distance to the line is
(y+d)2 =
(x2+d)2 =
x4 + 2dx2 + d2
and the squared distance to the focus is
x2 + (y-d)2 = x2 + (x2-d)2 = x2 + x4 - 2dx2 + d2
These two expressions should be equal, so their difference should be zero:
(x4 + 2dx2 + d2) -
(x2 + x4 - 2dx2 + d2) =
(4d-1)x2
From this you can read d=1/4. So your directrix is y=-1/4 and your focus is (0,1/4).
The point on the directrix which corresponds to a point on the parabola is simply its orthogonal projection. So if you have P=(x,y)=(x,x2) on the parabola, then you get P'=(x,-d)=P(x,-1/4).
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I need to write a reliable method to retrieve the answer to the following scenario...
Given a line segment AB and an arbitrary point C, how would I find the closest point to A on a line parallel to AB that passes through point C? (Reliable mentioned above refers to the algorithms ability to find D while allowing the coordinates for A, B, and C to be completely arbitrary and unpredictable. I've ran in to a quite a few solutions that I was not able to adapt to all possible scenarios, sadly...)
In the case of the data displayed in the picture below, how would I reliably find the x,y coordinates of D?
A = <425, 473>
B = <584, 533>
C = <371, 401>
D = <???, ???>
Knowing that AB and CD are parallel, that obviously means the slopes are the same.
I have tried many different formulas to no avail and have been working on this for weeks now. I'm stumped!
It's a minimization problem.
In general, the Euclidean distance between two points (A and B) in N dimensional space is given by
Dist(A,B) = sqrt((A1-B1)^2 + ... + (AN-BN)^2)
If you need to find the minimum distance between a space curve A(t) (a 1-dimensional object embedded in some N dimensional space) and a point B, then you need to solve this equation:
d Dist(A(t),B) / dt = 0 // (this is straightforward calculus: we're at either a minimum or maximum when the rate of change is 0)
and test that set of roots (t1, t2, etc) against the distance function to find which one yields the smallest value of D.
Now to find the equation for the parallel line passing through C in y=mx+b form:
m = (Ay - By)/(Ax-Bx)
b = Cy - mCx
Let's write this in space-curve form as and plug it into our formula from part 1:
Dist(D(t),A) = sqrt((t-Ax)^2 + (m*t+b-Ay)^2)
taking our derivative:
d Dist(D(t),A)/ dt = d sqrt((t-Ax)^2 + (m*t+b-Ay)^2) / dt
= (t + (m^2)*t - Ax + m*b - m*Ay)/sqrt(t^2 + (m^2)t^2 - 2*t*Ax + 2*m*t*b - 2*m*t*Ay + (Ax)^2 + (Ay)^2 + b^2 - 2*b*Ay )
= ((1+m^2)*t - Ax + m*b - m*Ay)/sqrt((1+m^2)*(t^2) + 2*t*(m*b - Ax - m*Ay) + (Ax)^2 + (Ay)^2 + b^2 - 2*b*Ay )
Settings this equal to 0 and solving for t yields:
t = (Ax-m*b+m*Ay)/(1+m^2)
as the only root (you can check this for yourself by substituting back in and verifying that everything cancels as desired).
Plugging this value of t back in to our space curve yields the following:
D=<(Ax-m*b+m*Ay)/(1+m^2),b+m*(Ax-m*b+m*Ay)/(1+m^2)>
You can then plug in your expressions for m and b if you want an explicit solution in terms A,B,C, or if you only want the numerical solution you can just compute it as a three step process:
m = (Ay - By)/(Ax-Bx)
b = Cy - mCx
D=<(Ax-m*b+m*Ay)/(1+m^2),b+m*(Ax-m*b+m*Ay)/(1+m^2)>
This will be valid for all cases with parallel straight lines. One caveat when implementing it as a numerical (rather than analytical) code: if the lines are oriented vertically, calculating m = (Ay-By)/(Ax-Bx) will result in division by 0, which will make your code not work. You can throw in a safety valve as follows:
if( Ax == Bx) {
D = <Cx,Ay>
} else {
// normal calculation here
}
For serious numerical work, you probably want to implement that in terms of tolerances rather than a direct comparison due to roundoff errors and all that fun stuff (i.e., abs(Ax-Bx) < epsilon, rather than Ax==Bx)
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a line with 2 points. I know the distance between the 2 points. I also calculated the angle of the line.
My target is to get a point that lies at 25% ot the line.
I calculate the y of this point with (dist/100)*25.
My only problem is calculating the x of the point. I suspect i have all the variables needed i only can't seem to find how to calculate the x. Does anybody know this?
You have a segment (not line) with endpoints P0 (coordinates x0,y0) and P1(x1,y1). New point P lies at this segment and distance |P0P| = 0.25 * |P0P1|, if their coordinates are:
x = x0 + 0.25 * (x1-x0)
y = y0 + 0.25 * (y1-y0)
It's just simple vector maths, no need for any angles or trig here.
startPos = (0,0)
endPos = (10,10)
fratcion = 0.25
distX = endPos.x - startPos.x
distY = endPos.y - startPos.y
pos.x = startPos.x + fraction*distX
pos.y = startPos.y + fraction*distY
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
Given:
(x1,y1) = (0,0)
(x2,y2) = (0,-10)
Using the angle to C, how are the coordinates at C calculated?
Let A be the point (x1,y1) and B be the point (x2,y2).
AC must have length 10 since it is isosceles.
Let X the point on AB which a perpendicular line passes through C. AXC is a right angled triangle with hypotenuse AC. C has co-ordinates (-length(AX),length(XC)).
length(AX) = length(AC)*cos(theta) = 10*cos(theta)
length(XC) = length(AC)*sin(theta) = 10*sin(theta)
Therefore C has co-ordinates: (-10*cos(theta),10*sin(theta))
There are multiple valid answers to this question. The following coordinates all produce isosceles triangles:
(-10, 0)
(10, 0)
(-10, -10)
(10, -10)
(6, -8)
(-6, -8)
(8, -6)
(-8, -6)
(x, -5) | x != 0
And, as a matter of fact, this isn't a complete solution.
Without some hint as to what programming platform you intend to implement a solution in, we cannot help much more.