给定3-D中的点(x,y,z)和平面方程的系数,任务是通过给定平面找到该点的镜像。
例子:
Input: a = 1, b = -2, c = 0, d = 0, x = -1, y = 3, z = 4
Output: x3 = 1.7999999999999998, y3 = -2.5999999999999996, z3 = 4.0
Input: a = 2, b = -1, c = 1, d = 3, x = 1, y = 3, z = 4
Output: x3 = -3.0, y3 = 5.0, z3 = 2.0
方法:平面的等式为ax + x + cz + d =0。因此,法线与平面的方向比为(a,b,c) 。令N为从给定点到给定平面的垂直脚,因此,线PN的有向比(a,b,c)穿过P(x1,y1,z1)。
PN线的等式为:-
(x - x1) / a = (y - y1) / b = (z - z1) / c = k
因此,PN线上的任何点都可以写成:-
x = a*k + x1
y = b*k + y1
z = c*k + z1
由于N位于直线和平面中,因此将满足(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).
现在,以k表示的点N的坐标将是:
x2 = a * k + x1
y2 = b * k + y1
z2 = c * k + z1
因为点N(x2,y2,z2)是点P(x1,y1,z1)和点Q(x3,y3,z3)的中点,所以点Q的坐标为:-
=> x3 = 2 * x2 - x1
=> y3 = 2 * y2 - y1
=> z3 = 2 * z2 - z1
C++
// C++ program to find
// Mirror of a point
// through a 3 D plane
#include
#include
#include
#include
using namespace std;
// Function to mirror image
void 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 Code
int 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
// C program to find
// Mirror of a point
// through a 3 D plane
#include
// Function to mirror image
void 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 Code
int 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
// Java program to find
// Mirror of a point
// through a 3 D plane
import java.io.*;
class GFG
{
// Function to mirror image
static 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 Code
public 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
Python
# Function to mirror image
def 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 Code
a = 1
b = -2
c = 0
d = 0
x1 = -1
y1 = 3
z1 = 4
# function call
mirror_point(a, b, c, d, x1, y1, z1)
C#
// C# program to find Mirror of
// a point through a 3 D plane
using System;
class GFG
{
// Function to mirror image
static 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 Code
static 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
输出:
x3 = 1.8 y3 = -2.6 z3 = 4.0