Reputation: 718
The problem is as follows:
Given two different points P1 = (x1,y1), P2 = (x2,y2) and point G=(a,b), find c and d such that G'=(c,d) is the reflection of G about the line P1P2
What I am looking for is a method to do this quickly. Since I am working on floating point numbers, I'd also like to use a method which minimizes the absolute value of the exponent in scientific notation, but that is second priority.
What I have tried: let R be the vector which is the projection of vector (G-P1) onto vector (P2-P1). Then, the reflection is achieved by taking Q = P1 + R, which is the projection of G onto the line, and then G' = 2Q-G. Now this is all cool and dandy, but calculating the projection is the hard part here.
How I calculate the projection of vector A onto B:
The scalar product of A and B is |A|*|B|*cos(theta), where theta is the directed angle from A to B. You can obtain the value of the scalar product by taking xAxB + yAyB. But the projection is of length |A|*cos(theta), so we have to divide the scalar product by |B|. Now, we have the length, but not the direction. The direction is along vector B, so we must multiply by the unit vector along B, which is B/|B|. Ultimately, we get the formula (xAxB + yAyB)*B/|B|2.
The actual problem:
This is kind of a roundabout way to do this, and I am looking for a more direct formula from the coordinates. Additionally (although less important), calculating the length of a vector as I need to do in computing the projection and scalar product is problematic, when the numbers I am working on are big, because I may get a floating point overflow or something like that.
If this is of any significance, I am working in OCaml.
Thanks in advance
Upvotes: 0
Views: 437
Reputation: 80285
Formulas are really very simple.
Projection (for line AB and point P) seems similar to yours:
L = A + AB * ScalarProduct(AB, AP) / ScalarProduct(AB, AB)
Reflection point
P' = P + 2*(L-P) = 2*L-P
Working Python example:
def refl(x1, y1, x2, y2, xp, yp):
x12 = x2 - x1
y12 = y2 - y1
xxp = xp - x1
yyp = yp - y1
dotp = x12 * xxp + y12 * yyp
dot12 = x12 * x12 + y12 * y12
coeff = dotp / dot12
lx = x1 + x12 * coeff
ly = y1 + y12 * coeff
return 2*lx-xp, 2*ly-yp
print(refl(0, 0, 2, 2, 0, 1))
>>> (1.0, 0.0)
Upvotes: 1