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
-
Economics & Finance
Selected Reading
Projection Area of 3D Shapes
When we place cubes on a grid, we can calculate the projection area by viewing the 3D structure from three different angles: top view (xy-plane), front view (yz-plane), and side view (xz-plane). Each projection shows the shadow cast by the cubes onto that plane.
Algorithm
The solution calculates each projection separately ?
- XY projection (top view): Count non-zero cells in the grid
- YZ projection (front view): Sum of maximum height in each row
- XZ projection (side view): Sum of maximum height in each column
Example
class Solution:
def projectionArea(self, grid):
xy = 0 # Top view projection
yz = 0 # Front view projection
xz = 0 # Side view projection
# Calculate YZ projection (front view) and XY projection (top view)
for r, row in enumerate(grid):
yz += max(row) # Max height in this row
for c, col in enumerate(row):
if grid[r][c] > 0:
xy += 1 # Count non-zero cells
# Calculate XZ projection (side view)
for col in zip(*grid): # Transpose to get columns
xz += max(col) # Max height in this column
return xy + yz + xz
# Test the solution
solution = Solution()
grid = [[1, 2], [3, 4]]
result = solution.projectionArea(grid)
print(f"Grid: {grid}")
print(f"Projection area: {result}")
Grid: [[1, 2], [3, 4]] Projection area: 17
How It Works
Let's trace through the example step by step ?
def detailed_projection(grid):
print(f"Grid: {grid}")
# XY projection (top view)
xy = sum(1 for row in grid for cell in row if cell > 0)
print(f"XY (top view): {xy} non-zero cells")
# YZ projection (front view)
yz = sum(max(row) for row in grid)
print(f"YZ (front view): {[max(row) for row in grid]} = {yz}")
# XZ projection (side view)
columns = list(zip(*grid))
xz = sum(max(col) for col in columns)
print(f"XZ (side view): {[max(col) for col in columns]} = {xz}")
total = xy + yz + xz
print(f"Total area: {xy} + {yz} + {xz} = {total}")
return total
# Test with the example
detailed_projection([[1, 2], [3, 4]])
Grid: [[1, 2], [3, 4]] XY (top view): 4 non-zero cells YZ (front view): [2, 4] = 6 XZ (side view): [3, 4] = 7 Total area: 4 + 6 + 7 = 17
Key Points
- XY projection: Shows which grid positions have cubes (binary view)
- YZ projection: Shows the tallest cube in each row
- XZ projection: Shows the tallest cube in each column
- zip(*grid): Transposes the grid to access columns easily
Conclusion
The projection area is the sum of three 2D views: top (XY), front (YZ), and side (XZ). Each projection captures different geometric properties of the 3D structure formed by the cubes.
Advertisements
