Simple 3D Projection

Blog post

# Project 3D points onto a 2D screen with simple perspective  

function project_points(points, camera_z, screen_width, screen_height, fov):  

projected_points = []  

for each (x, y, z) in points:  

# Step 1: Avoid division by zero (point too close to camera)  
if z == camera_z:  
continue  

# Step 2: Perspective scale factor  
scale = fov / (z - camera_z)  

# Step 3: Project into 2D  
screen_x = x * scale  
screen_y = y * scale  

# Step 4: Convert to screen coordinates (centered on screen)  
screen_x = screen_x + screen_width / 2  
screen_y = screen_y + screen_height / 2  

projected_points.append((screen_x, screen_y))  

return projected_points

Key ideas: - Objects farther away (larger z) get smaller because scale decreases. - fov controls how strong the perspective effect is. - camera_z is the camera position along the Z axis. - Assumes camera is looking along the Z direction.