Finding reflection of a point relative to another point in JavaScript


Point Of Symmetry

"Point reflection" or "point symmetry" is a basic concept in geometry where a given point, P, at a given position relative to a midpoint, Q has a corresponding point, P1, which is the same distance from Q but in the opposite direction.

Problem

We are required to write a JavaScript function that takes in two objects P and Q specifying two points in a 2-D plane.

Our function should output the symmetric point of point P about Q.

Example

Following is the code −

 Live Demo

const p = {
   x: 6, y: -4
};
const q = {
   x: 11, y: 5
};
const findReflection = (p = {}, q = {}) => {
   const res = {};
   const Xdistance = p['x'] - q['x'];
   res['x'] = q['x'] - Xdistance;
   let Ydistance = p['y'] - q['y'];
   res['y'] = q['y'] - Ydistance;
   return res;
};
console.log(findReflection(p, q));

Output

Following is the console output −

{ x: 16, y: 14 }

Updated on: 19-Apr-2021

206 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements