Mirror of a point through a 3 D plane - GeeksforGeeks

28 Apr, 2021
Given a point(x, y, z) in 3-D and coefficients of the equation of a plane, the task is to find the mirror image of that point through the given plane. Examples:  
Input: a = 1, b = -2, c = 0, d = 0, x = -1, y = 3, z = 4 Output: x3 = 1.7999999999999998, y3 = -2.5999999999999996, z3 = 4.0Input: a = 2, b = -1, c = 1, d = 3, x = 1, y = 3, z = 4 Output: x3 = -3.0, y3 = 5.0, z3 = 2.0
 
 
Approach: Equation of plane is as ax + by + cz + d = 0. Therefore, direction ratios of the normal to the plane are (a, b, c). Let N be the foot of perpendicular from a given point to the given plane so, line PN has directed ratios (a, b, c) and it passes through P(x1, y1, z1). The equation of line PN will be as:-  
(x - x1) / a = (y - y1) / b = (z - z1) / c = k
Hence any point on line PN can be written as:-  
x = a*k + x1
y = b*k + y1
z = c*k + z1
since N lies in both line and plane so will satisfy(ax + by + cz + d = 0).  
=>a * (a * k + x1) + b * (b * k + y1) + c * (c * k + z1) + d = 0.
=>a * a * k + a * x1 + b * b * k + b * y1 + c * c * k + c * z1 + d = 0.
=>(a * a + b * b + c * c)k = -a * x1 - b * y1 - c * z1 - d.
=>k = (-a * x1 - b * y1 - c * z1 - d) / (a * a + b * b + c * c).
Now, the coordinates of Point N in terms of k will be:-  
x2 = a * k + x1
y2 = b * k + y1
z2 = c * k + z1
Since, Point N(x2, y2, z2) is midpoint of point P(x1, y1, z1) and point Q(x3, y3, z3), coordinates of Point Q are:-  
=> x3 = 2 * x2 - x1
=> y3 = 2 * y2 - y1
=> z3 = 2 * z2 - z1
 
C++
C
Java
Python
C#
PHP
Javascript
// C++ program to find// Mirror of a point // through a 3 D plane#include <bits/stdc++.h>#include<math.h>#include <iostream>#include <iomanip> using namespace std; // Function to mirror imagevoid mirror_point(float a, float b,                   float c, float d,                   float x1, float y1,                  float z1){    float k = (-a * x1 - b *                 y1 - c * z1 - d) /         (float)(a * a + b * b + c * c);    float x2 = a * k + x1;    float y2 = b * k + y1;    float z2 = c * k + z1;    float x3 = 2 * x2 - x1;    float y3 = 2 * y2 - y1;    float z3 = 2 * z2 - z1;           std::cout << std::fixed;    std::cout << std::setprecision(1);    cout << " x3 = " << x3;     cout << " y3 = " << y3;     cout << " z3 = " << z3;} // Driver Codeint main(){    float a = 1;    float b = -2;    float c = 0;    float d = 0;    float x1 = -1;    float y1 = 3;    float z1 = 4;       // function call    mirror_point(a, b, c, d,                  x1, y1, z1);    return 0;}// This code is contributed// by Amber_Saxena.
// C program to find// Mirror of a point// through a 3 D plane#include<stdio.h>     // Function to mirror imagevoid mirror_point(float a, float b,                  float c, float d,                  float x1, float y1,                  float z1){    float k = (-a * x1 - b *                y1 - c * z1 - d) /        (float)(a * a + b * b + c * c);    float x2 = a * k + x1;    float y2 = b * k + y1;    float z2 = c * k + z1;    float x3 = 2 * x2 - x1;    float y3 = 2 * y2 - y1;    float z3 = 2 * z2 - z1;         printf("x3 = %.1f ", x3);    printf("y3 = %.1f ", y3);    printf("z3 = %.1f ", z3);} // Driver Codeint main(){    float a = 1;    float b = -2;    float c = 0;    float d = 0;    float x1 = -1;    float y1 = 3;    float z1 = 4;     // function call    mirror_point(a, b, c, d,                 x1, y1, z1);} // This code is contributed// by Amber_Saxena.
// Java program to find// Mirror of a point// through a 3 D planeimport java.io.*; class GFG{     // Function to mirror imagestatic void mirror_point(int a, int b,                          int c, int d,                         int x1, int y1,                         int z1){    float k = (-a * x1 - b * y1 - c * z1 - d) /        (float)(a * a + b * b + c * c);    float x2 = a * k + x1;    float y2 = b * k + y1;    float z2 = c * k + z1;    float x3 = 2 * x2 - x1;    float y3 = 2 * y2 - y1;    float z3 = 2 * z2 - z1;         System.out.print("x3 = " + x3 + " ");    System.out.print("y3 = " + y3 + " ");    System.out.print("z3 = " + z3 + " ");} // Driver Codepublic static void main(String[] args){    int a = 1;    int b = -2;    int c = 0;    int d = 0;    int x1 = -1;    int y1 = 3;    int z1 = 4;     // function call    mirror_point(a, b, c, d,                 x1, y1, z1) ;}} // This code is contributed// by inder_verma
# Function to mirror imagedef mirror_point(a, b, c, d, x1, y1, z1):          k =(-a * x1-b * y1-c * z1-d)/float((a * a + b * b + c * c))    x2 = a * k + x1    y2 = b * k + y1    z2 = c * k + z1    x3 = 2 * x2-x1    y3 = 2 * y2-y1    z3 = 2 * z2-z1    print "x3 =", x3,    print "y3 =", y3,    print "z3 =", z3,  # Driver Codea = 1b = -2c = 0d = 0x1 = -1y1 = 3z1 = 4 # function callmirror_point(a, b, c, d, x1, y1, z1) 
// C# program to find Mirror of// a point through a 3 D planeusing System; class GFG{         // Function to mirror imagestatic void mirror_point(int a, int b,                         int c, int d,                         int x1, int y1,                         int z1){    float k = (-a * x1 - b * y1 - c * z1 - d) /              (float)(a * a + b * b + c * c);    float x2 = a * k + x1;    float y2 = b * k + y1;    float z2 = c * k + z1;    float x3 = 2 * x2 - x1;    float y3 = 2 * y2 - y1;    float z3 = 2 * z2 - z1;         Console.Write("x3 = " + x3 + " ");    Console.Write("y3 = " + y3 + " ");    Console.Write("z3 = " + z3 + " ");} // Driver Codestatic public void Main (){    int a = 1;    int b = -2;    int c = 0;    int d = 0;    int x1 = -1;    int y1 = 3;    int z1 = 4;         // function call    mirror_point(a, b, c, d,                 x1, y1, z1);}} // This code is contributed by jit_t
<?php// PHP program to find Mirror of// a point through a 3 D plane // Function to mirror imagefunction mirror_point($a, $b, $c, $d,                      $x1, $y1, $z1){    $k = (-$a * $x1 - $b *           $y1 - $c * $z1 - $d) /          ($a * $a + $b *           $b + $c * $c);    $x2 = $a * $k + $x1;    $y2 = $b * $k + $y1;    $z2 = $c * $k + $z1;    $x3 = 2 * $x2 - $x1;    $y3 = 2 * $y2 - $y1;    $z3 = 2 * $z2 - $z1;    echo sprintf("x3 = %.1f ", $x3);    echo sprintf("y3 = %.1f ", $y3);    echo sprintf("z3 = %.1f ", $z3);} // Driver Code$a = 1;$b = -2;$c = 0;$d = 0;$x1 = -1;$y1 = 3;$z1 = 4;// function callmirror_point($a, $b, $c, $d,             $x1, $y1, $z1); // This code is contributed// by Amber_Saxena.?>
<script>       // JavaScript program to find      // Mirror of a point      // through a 3 D plane       // Function to mirror image      function mirror_point(a, b, c, d, x1, y1, z1)      {        var k = parseFloat(          (-a * x1 - b * y1 -          c * z1 - d) / parseFloat(a * a + b * b +          c * c)        );        var x2 = parseFloat(a * k + x1);        var y2 = parseFloat(b * k + y1);        var z2 = parseFloat(c * k + z1);        var x3 = parseFloat(2 * x2 - x1).toFixed(1);        var y3 = parseFloat(2 * y2 - y1).toFixed(1);        var z3 = parseFloat(2 * z2 - z1).toFixed(1);         document.write(" x3 = " + x3);        document.write(" y3 = " + y3);        document.write(" z3 = " + z3);      }       // Driver Code      var a = 1;      var b = -2;      var c = 0;      var d = 0;      var x1 = -1;      var y1 = 3;      var z1 = 4;       // function call      mirror_point(a, b, c, d, x1, y1, z1);       </script>
x3 = 1.8 y3 = -2.6 z3 = 4.0
 
inderDuMCA
Amber_Saxena
jit_t
rdtank
school-programming
Geometric
School Programming
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program for distance between two points on earth
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Line Clipping | Set 1 (Cohen–Sutherland Algorithm)
Convex Hull | Set 2 (Graham Scan)
Check whether a given point lies inside a triangle or not
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects