Below is an example of how you can illustrate the projection of 3D points onto a 2D plane using a projection matrix. In this example, we use Python with Matplotlib. The code does the following:
- Generates random 3D points.
- Uses a simple projection matrix (in this case, one that simply “drops” the z-coordinate) to map the 3D points onto a 2D plane.
- Plots the 3D points and their corresponding 2D projected points.
- Draws dashed lines connecting each 3D point to its projection to emphasize the effect of the projection.
You can run this code locally (make sure you have installed matplotlib and numpy) to generate the image.
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Fixing seed for reproducibility
np.random.seed(0)
# Generate random 3D points
num_points = 20
X = np.random.uniform(-10, 10, num_points)
Y = np.random.uniform(-10, 10, num_points)
Z = np.random.uniform(-10, 10, num_points)
points_3D = np.vstack((X, Y, Z)).T
# Define a simple orthogonal projection matrix that projects onto the XY plane.
# Essentially, it ignores the z-coordinate.
# If using homogeneous coordinates, the matrix would be 3x4, but here we use a simple 2x3 matrix.
P = np.array([[1, 0, 0],
[0, 1, 0]])
# Project 3D points to 2D
points_2D = (P @ points_3D.T).T # resulting shape is (num_points, 2)
# Create a figure with two subplots: one 3D and one 2D
fig = plt.figure(figsize=(14, 6))
# 3D plot showing original points and projection lines
ax1 = fig.add_subplot(121, projection='3d')
ax1.scatter(X, Y, Z, c='blue', label='3D Points', s=50)
ax1.set_title('3D Points with Projection Lines')
# For each 3D point, plot its projection line to the XY plane.
for i in range(num_points):
# The 2D projection placed on the XY plane
ax1.plot([X[i], X[i]], [Y[i], Y[i]], [Z[i], 0], 'r--', linewidth=1)
ax1.set_xlabel('X')
ax1.set_ylabel('Y')
ax1.set_zlabel('Z')
ax1.legend()
ax1.view_init(elev=20, azim=30)
# 2D plot showing the projected points on the XY plane
ax2 = fig.add_subplot(122)
ax2.scatter(points_2D[:, 0], points_2D[:, 1], c='green', label='Projected Points', s=50)
ax2.set_title('2D Projection (XY Plane)')
ax2.set_xlabel('X')
ax2.set_ylabel('Y')
ax2.grid(True)
ax2.legend()
plt.tight_layout()
plt.show()
Explanation
-
3D Points Generation:
We generate 20 random points in 3D space with coordinates between -10 and 10.
-
Projection Matrix:
We define a 2×3 projection matrix P = [[1, 0, 0], [0, 1, 0]]. This matrix “projects” a 3D point (x, y, z) onto (x, y), effectively discarding the z-coordinate.
-
Projection Operation:
The matrix multiplication P @ points_3D.T projects every 3D point to the 2D plane. Transposing the result gives an (n×2) array of 2D points.
-
Visualization:
- The left subplot shows the 3D points and draws dashed red lines from each 3D point vertically down (or up) to the XY plane (z = 0).
- The right subplot shows the 2D points resulting from the projection.
Running this code will display an image where you can visually understand how each 3D point is "projected" onto the 2D plane using the projection matrix. You can modify the projection matrix if you want to simulate different types of projections (e.g., perspective projections).