|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +title: Vectors in Bitbybit |
| 4 | +sidebar_label: Vectors in Bitbybit |
| 5 | +description: An overview of the Vector class in Bitbybit, explaining its core functionalities for vector math in simple terms. |
| 6 | +tags: [code, vector] |
| 7 | +--- |
| 8 | + |
| 9 | +import Tabs from '@theme/Tabs'; |
| 10 | +import TabItem from '@theme/TabItem'; |
| 11 | + |
| 12 | +<img |
| 13 | + src="https://s.bitbybit.dev/assets/icons/white/vector-icon.svg" |
| 14 | + alt="Vector category icon" |
| 15 | + title="Vector category icon" |
| 16 | + width="100" /> |
| 17 | + |
| 18 | +The `Vector` class in Bitbybit provides a comprehensive suite of tools for performing vector mathematics. In Bitbybit, a **vector is fundamentally represented as an array of numbers**. For 3D operations, this typically takes the form `[x, y, z]`, where `y` is considered the 'up' direction by convention in many Bitbybit contexts. This simple array representation means that vectors can often be used interchangeably with points, which also follow the `[x, y, z]` structure. |
| 19 | + |
| 20 | +[Full Source on GitHub](https://github.com/bitbybit-dev/bitbybit/blob/master/packages/dev/base/lib/api/services/vector.ts) |
| 21 | + |
| 22 | + |
| 23 | +The `Vector` class in Bitbybit is your toolkit for working with vectors and points in your 3D designs. |
| 24 | + |
| 25 | +**What is a Vector in Bitbybit?** |
| 26 | + |
| 27 | +At its heart, a vector (or a point) in Bitbybit is just a simple **array of numbers**. |
| 28 | +* For 2D, it might look like `[x, y]`. |
| 29 | +* For 3D, it's typically `[x, y, z]`. In many Bitbybit contexts, `y` represents the "up" direction. |
| 30 | + |
| 31 | +This straightforward representation makes it easy to pass vector data around and use it in various calculations. |
| 32 | + |
| 33 | +## What Can You Do With the Vector Class? |
| 34 | + |
| 35 | +The `Vector` class helps you perform a wide range of operations. Here's a look at the main categories of features it supports. For the exact input parameters, default values, and more advanced functions, please consult the detailed [Vector API Documentation](https://docs.bitbybit.dev/classes/Bit.Vector-1.html) (or the GitHub source linked above). |
| 36 | + |
| 37 | +### 1. Creating Vectors and Number Sequences |
| 38 | + |
| 39 | +Need to make a new vector or a list of numbers? |
| 40 | +* **Specific Vectors:** Create 2D (`[x,y]`) or 3D (`[x,y,z]`) vectors directly from their components (e.g., `vectorXYZ()`, `vectorXY()`). |
| 41 | +* **Number Ranges:** Generate sequences of numbers. |
| 42 | + * `range()`: Get a simple list of integers (e.g., `[0, 1, 2, 3, 4]`). |
| 43 | + * `span()`: Create evenly spaced numbers between a minimum and maximum (e.g., `[0, 0.5, 1.0, 1.5, 2.0]`). |
| 44 | + * `spanLinearItems()`: Like `span()`, but you specify the total number of items you want. |
| 45 | + * `spanEaseItems()`: Create a sequence where the spacing follows an "easing" curve (e.g., numbers bunch up at the start and spread out at the end). This is great for animations or non-uniform distributions. |
| 46 | + |
| 47 | +### 2. Basic Vector Math (The Essentials) |
| 48 | + |
| 49 | +These are the bread-and-butter operations: |
| 50 | +* **Addition & Subtraction:** Add two vectors (`add()`) or subtract one from another (`sub()`). You can also sum up a whole list of vectors (`addAll()`). |
| 51 | +* **Scaling (Multiplication/Division):** Make a vector longer or shorter by multiplying (`mul()`) or dividing (`div()`) its components by a single number (a scalar). |
| 52 | +* **Dot Product (`dot()`):** A fundamental operation that tells you about the relationship between two vectors' directions. |
| 53 | +* **Cross Product (`cross()`):** For 3D vectors, this gives you a new vector that's perpendicular to the two original vectors. Essential for finding normals or rotational axes. |
| 54 | +* **Negation (`neg()`):** Flip the direction of a vector. |
| 55 | + |
| 56 | +### 3. Measuring and Analyzing Vectors |
| 57 | + |
| 58 | +Understand your vectors' properties: |
| 59 | +* **Length (Magnitude/Norm):** Find out how long a vector is (`length()`, `norm()`). There are also versions for squared length (`lengthSq()`, `normSquared()`), which can be faster if you only need to compare lengths. |
| 60 | +* **Normalization (`normalized()`):** Create a "unit vector" – a vector with a length of 1 that points in the same direction as the original. Very useful for representing directions. |
| 61 | +* **Distance:** Calculate the distance between two points (which are just vectors) (`dist()`, `distSquared()`). |
| 62 | +* **Angles:** |
| 63 | + * Measure the angle between two vectors (`angleBetween()`). |
| 64 | + * Get signed angles or positive angles relative to a reference direction (`signedAngleBetween()`, `positiveAngleBetween()`, `angleBetweenNormalized2d()`). This helps determine orientation (e.g., clockwise vs. counter-clockwise). |
| 65 | +* **Interpolation (`lerp()`):** Find a point (vector) that's part-way between two other points, based on a fraction. |
| 66 | + |
| 67 | +### 4. Validating and Checking Vectors |
| 68 | + |
| 69 | +Perform checks on your vector data: |
| 70 | +* **Are Vectors the Same? (`vectorsTheSame()`):** Check if two vectors are equal, within a small tolerance (important for floating-point numbers). |
| 71 | +* **Is it Zero? (`isZero()`):** See if a vector has zero length. |
| 72 | +* **Is it Finite? (`finite()`):** Check if all numbers in a vector are valid (not Infinity or NaN). |
| 73 | + |
| 74 | +### 5. Working with Lists of Vectors |
| 75 | + |
| 76 | +Manage collections of vectors: |
| 77 | +* **Removing Duplicates:** Clean up lists by removing identical vectors, either all duplicates (`removeAllDuplicateVectors()`) or only those that are next to each other (`removeConsecutiveDuplicateVectors()`). |
| 78 | +* **Finding Min/Max Values (`min()`, `max()`):** Get the smallest or largest number within a vector's components. |
| 79 | +* **Domain (`domain()`):** For an ordered list of numbers, find the difference between the last and first values. |
| 80 | + |
| 81 | +### 6. Utility Functions |
| 82 | + |
| 83 | +* **Point on Ray (`onRay()`):** Given a starting point, a direction vector, and a distance, find the coordinates of the point along that ray. |
| 84 | +* **Boolean List Check (`all()`):** Check if a list of boolean values are all `true`. |
| 85 | + |
| 86 | +## How to Use |
| 87 | + |
| 88 | +Typically, you'll interact with these methods by providing an "inputs" object. For example, to add two vectors: |
| 89 | + |
| 90 | +```typescript |
| 91 | +// In TypeScript (conceptual) |
| 92 | +const vec1 = [1, 2, 3]; |
| 93 | +const vec2 = [4, 5, 6]; |
| 94 | + |
| 95 | +const sumVector = bitbybit.vector.add({ first: vec1, second: vec2 }); |
| 96 | +// sumVector will be [5, 7, 9] |
| 97 | +``` |
0 commit comments