Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
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 −
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 } Advertisements
