Home > Back-end >  How to find the third point coordinates of reflected points?
How to find the third point coordinates of reflected points?

Time:01-09

example questition:

P, at a given position relative to a mid-point, Q has a corresponding point, P1, which is the same distance from Q but in the opposite direction. Given two points P and Q, output the symmetric point of point P about Q. find p1 coordinates.

I know the answer is :

public class Reflections {

    public static int[] reflectPoint(int[] p, int[] q) {
        return new int[]{ 2 * q[0] - p[0], 2 * q[1] - p[1] };
    }

}

but I don't know the rule using in this solution , can anyone explain it to me ?

CodePudding user response:

Suppose P = (a, b) and Q = (x, y). First, you find the vector v that takes you from P to Q:

<x - a, y - b>.

Next, you add that vector onto point Q:

(x, y) <x - a, y - b> = (2x - a, 2y - b)

and that is P1. In the solution code, you can see how they have the same format of 2q[0] - p[0], 2q[1] - p[1]. Using the same variable names: q[0] is x, q[1] is y; p[0] is a, p[1] is b.

Here is a diagram to visualize the process: enter image description here

CodePudding user response:

It is using the concept of reflection. In the question, it is mentioned that Q is midpoint of P and P1. in the answer you mentioned, P is an array of length 2 whose index 0 represents the x coordinate and index 1 represents the y coordinate.

Q[0] = (P[0]  P1[0])/2; -> P1[0] = 2*Q[0] - P[0]

// similarly for the Q[1]

CodePudding user response:

If the x-distance between p and q is X then the x-distance between p and p1 is 2X.
The same applies to the y-distance.
In other words, you get the reflected point (p1) by adding twice the p - q distance to p.
A verbose and commented version of reflectPoint(int[] p, int[] q) with a better choice of names makes it easier to follow:

/**
 * @param point the point to be reflected in {x,y} format
 * @param midPoint the middle point in {x,y} format
 * @return a point on the point-midPoint line in {x,y} format, symmetric to point around midPoint
 */
public static int[] verboseReflectPoint(int[] point, int[] midPoint) {

    //calculate the x distance and y distance from point to mid-point 
    int deltaX = midPoint[0] - point[0];
    int deltaY = midPoint[1] - point[1];
    
    //find the symmetric point coordinates by adding twice the distance to point 
    int reflectedPointX = point[0]   2*deltaX;
    int reflectedPointY = point[1]   2*deltaY;

    int[] reflectedPoint = {reflectedPointX, reflectedPointY};
    return reflectedPoint;
}

point[0] 2*(midPoint[0] - point[0]) is equivalent to 2*midPoint[0] -point[0]

  •  Tags:  
  • Related