{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# perspective projection\n",
"\n",
"```\n",
"x' = x / z\n",
"y' = y / z\n",
"```\n",
"\n",
"divide by distance. that's it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# a cube: 8 vertices, 12 edges\n",
"\n",
"vertices = [\n",
" (-1, -1, -1),\n",
" ( 1, -1, -1),\n",
" ( 1, 1, -1),\n",
" (-1, 1, -1),\n",
" (-1, -1, 1),\n",
" ( 1, -1, 1),\n",
" ( 1, 1, 1),\n",
" (-1, 1, 1),\n",
"]\n",
"\n",
"edges = [\n",
" (0, 1), (1, 2), (2, 3), (3, 0), # front\n",
" (4, 5), (5, 6), (6, 7), (7, 4), # back\n",
" (0, 4), (1, 5), (2, 6), (3, 7), # connecting\n",
"]\n",
"\n",
"print(f\"{len(vertices)} vertices, {len(edges)} edges\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# the formula\n",
"\n",
"def project(x, y, z):\n",
" return (x / z, y / z)\n",
"\n",
"# example: a point at (2, 1, 4)\n",
"x, y, z = 2, 1, 4\n",
"x_proj, y_proj = project(x, y, z)\n",
"\n",
"print(f\"3d point: ({x}, {y}, {z})\")\n",
"print(f\"2d projection: ({x_proj}, {y_proj})\")\n",
"print(f\"\\nfarther away = smaller on screen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# static cube\n",
"\n",
"from IPython.display import HTML\n",
"\n",
"HTML(\"\"\"\n",
"\n",
"\n",
"\"\"\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# spinning cube\n",
"\n",
"from IPython.display import HTML\n",
"\n",
"HTML(\"\"\"\n",
"\n",
"\n",
"\"\"\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 4
}