Reading — step 1 of 5
Read
~2 min readMath & Integration
2D Vectors
A 2D vector represents position, velocity, or direction. (x, y) tuple.
python
Common operations:
- Length:
sqrt(x² + y²)(Pythagorean). - Length squared:
x² + y²(cheaper; use for comparisons). - Normalize:
v / length(v)→ unit vector. - Dot product:
v · w = vx*wx + vy*wy. Measures alignment. 0 = perpendicular. - Cross product (2D scalar):
v × w = vx*wy - vy*wx. Sign tells side.
Geometric meanings:
dot(a, b) = |a| * |b| * cos(θ): angle between.dot(a, b) > 0: same direction.dot(a, b) = 0: perpendicular.dot(a, b) < 0: opposite directions.cross(a, b) > 0: b is counterclockwise from a.
Projection: proj_b(a) = (a · b̂) * b̂ (a's component along b).
Reflection: r = v - 2 * (v · n̂) * n̂ (v reflected off surface with normal n).
python
Used in: bouncing balls.
Performance:
- Avoid sqrt when possible (use squared lengths).
- Inline simple ops.
- For large simulations, use SIMD or GPU.
NumPy works for batched vectors:
python
For 1000+ bodies: NumPy vectorization is essential.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…