141 lines
3.1 KiB
Markdown
141 lines
3.1 KiB
Markdown
# Parametric Surface Plotter
|
||
|
||
This project provides a simple way to **define and plot parametric 3D surfaces** (or curves) using Python.
|
||
|
||
* A **surface** is defined with **two parameters**:
|
||
|
||
$$
|
||
x = x(t, u), \quad y = y(t, u), \quad z = z(t, u)
|
||
$$
|
||
|
||
* A **curve** is defined with a **single parameter**:
|
||
|
||
$$
|
||
x = x(t), \quad y = y(t), \quad z = z(t)
|
||
$$
|
||
|
||
The script will:
|
||
|
||
* Generate a mesh of points (for surfaces) or a set of points (for curves)
|
||
* Plot the result in 3D with Matplotlib
|
||
* Save the plot as a high-definition `.svg` file, named after the parent directory of the script
|
||
|
||
---
|
||
|
||
## Requirements
|
||
|
||
Install dependencies:
|
||
|
||
```bash
|
||
pip install -r requirements.txt
|
||
```
|
||
|
||
---
|
||
|
||
## Usage
|
||
|
||
Run the script:
|
||
|
||
```bash
|
||
python plot_surface.py
|
||
```
|
||
|
||
A window with the 3D plot will open, and an `.svg` will be saved in the same directory.
|
||
|
||
---
|
||
|
||
## Defining Your Own Geometry
|
||
|
||
### 1. Surfaces (two parameters: t, u)
|
||
|
||
Surfaces require two parameters because they are 2D objects.
|
||
You define three functions in `plot_surface.py`:
|
||
|
||
```python
|
||
def x_func(t, u): return ...
|
||
def y_func(t, u): return ...
|
||
def z_func(t, u): return ...
|
||
```
|
||
|
||
Examples:
|
||
|
||
* **Cone**
|
||
|
||
$$
|
||
x = 4t\cos(u), \quad y = 4t\sin(u), \quad z = 10t
|
||
$$
|
||
|
||
```python
|
||
def x_func(t, u): return 4 * t * np.cos(u)
|
||
def y_func(t, u): return 4 * t * np.sin(u)
|
||
def z_func(t, u): return 10 * t
|
||
```
|
||
|
||
* **Sphere** (radius 1)
|
||
|
||
$$
|
||
x = \cos(u)\sin(t), \quad y = \sin(u)\sin(t), \quad z = \cos(t)
|
||
$$
|
||
|
||
```python
|
||
def x_func(t, u): return np.cos(u) * np.sin(t)
|
||
def y_func(t, u): return np.sin(u) * np.sin(t)
|
||
def z_func(t, u): return np.cos(t)
|
||
```
|
||
|
||
You can adjust parameter ranges in `generate_mesh()`:
|
||
|
||
```python
|
||
def generate_mesh(t_range=(0, 5), u_range=(0, 2*np.pi), n_t=50, n_u=100):
|
||
...
|
||
```
|
||
|
||
* Sphere → `t_range=(0, np.pi)`, `u_range=(0, 2*np.pi)`
|
||
* Cone → `t_range=(0, 5)`, `u_range=(0, 2*np.pi)`
|
||
|
||
---
|
||
|
||
### 2. Curves (one parameter: t)
|
||
|
||
If your object is not a surface but a **curve**, you only need one parameter $t$.
|
||
|
||
Example: **Helix**
|
||
|
||
$$
|
||
x = \cos(t), \quad y = \sin(t), \quad z = t
|
||
$$
|
||
|
||
```python
|
||
def x_func(t): return np.cos(t)
|
||
def y_func(t): return np.sin(t)
|
||
def z_func(t): return t
|
||
```
|
||
|
||
Here you don’t need `u` at all. The code can be simplified to generate a 1D array of points and plot them as a line in 3D.
|
||
|
||
---
|
||
|
||
## Which Should I Use?
|
||
|
||
* **Two parameters (t, u):** Use this for **surfaces** (sphere, torus, cone, paraboloid, etc.).
|
||
* **One parameter (t):** Use this for **curves** (helix, circle, line, parametric trajectory).
|
||
|
||
👉 A quick rule:
|
||
|
||
* If your equation is like $z = f(x, y)$, or implicit in 3 variables, you usually need **two parameters**.
|
||
* If your equation is like $x = f(t), y = g(t), z = h(t)$, then **one parameter** is enough.
|
||
|
||
---
|
||
|
||
## Notes
|
||
|
||
* Always use `numpy` functions (`np.sin`, `np.cos`, etc.), not Python’s built-ins, since the parameters are arrays.
|
||
* Some surfaces (like cones, hyperboloids) have two branches; you can add an additional `z_func_neg` if needed.
|
||
* The SVG is automatically named after the script’s parent directory.
|
||
|
||
---
|
||
|
||
✅ With this, you can parametrize and visualize **both surfaces (2D)** and **curves (1D)** by editing only a few functions.
|
||
|
||
|