Skip to content

Commit

Permalink
Add vector library documentation and syntax highlighting support. (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviralg authored Nov 1, 2024
1 parent 1318d7f commit 4439109
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
96 changes: 96 additions & 0 deletions _pages/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -962,3 +962,99 @@ function buffer.fill(b: buffer, offset: number, value: number, count: number?):
Sets the 'count' bytes in the buffer starting at the specified 'offset' to the 'value'.

If 'count' is 'nil' or is omitted, all bytes from the specified offset until the end of the buffer are set.

## vector library

This library implements functionality for the vector type in addition to the built-in primitive operator support.
Default configuration uses vectors with 3 components (`x`, `y`, and `z`).
If the _4-wide mode_ is enabled by setting the `LUA_VECTOR_SIZE` VM configuration to 4, vectors get an additional `w` component.

Individual vector components can be accessed using the fields `x` or `X`, `y` or `Y`, `z` or `Z`, and `w` or `W` in 4-wide mode.
Since vector values are immutable, writes to individual components are not supported.

```
vector.zero
vector.one
```

Constant vectors with all components set to 0 and 1 respectively. Includes the fourth component in _4-wide mode_.

```
vector.create(x: number, y: number, z: number): vector
vector.create(x: number, y: number, z: number, w: number): vector
```

Creates a new vector with the given component values. The first constructor sets the fourth (`w`) component to 0.0 in _4-wide mode_.

```
vector.magnitude(vec: vector): number
```

Calculates the magnitude of a given vector. Includes the fourth component in _4-wide mode_.

```
vector.normalize(vec: vector): vector
```

Computes the normalized version (unit vector) of a given vector. Includes the fourth component in _4-wide mode_.

```
vector.cross(vec1: vector, vec2: vector): vector
```

Computes the cross product of two vectors. Ignores the fourth component in _4-wide mode_ and returns the 3-dimensional cross product.

```
vector.dot(vec1: vector, vec2: vector): number
```

Computes the dot product of two vectors. Includes the fourth component in _4-wide mode_.

```
vector.angle(vec1: vector, vec2: vector, axis: vector?): number
```

Computes the angle between two vectors in radians. The axis, if specified, is used to determine the sign of the angle. Ignores the fourth component in _4-wide mode_ and returns the 3-dimensional angle.

```
vector.floor(vec: vector): vector
```

Applies `math.floor` to every component of the input vector. Includes the fourth component in _4-wide mode_.

```
vector.ceil(vec: vector): vector
```

Applies `math.ceil` to every component of the input vector. Includes the fourth component in _4-wide mode_.

```
vector.abs(vec: vector): vector
```

Applies `math.abs` to every component of the input vector. Includes the fourth component in _4-wide mode_.

```
vector.sign(vec: vector): vector
```

Applies `math.sign` to every component of the input vector. Includes the fourth component in _4-wide mode_.

```
vector.clamp(vec: vector, min: vector, max: vector): vector
```

Applies `math.clamp` to every component of the input vector. Includes the fourth component in _4-wide mode_.

```
vector.max(...: vector): vector
```

Applies `math.max` to the corresponding components of the input vectors. Includes the fourth component in _4-wide mode_. Equivalent to `vector.create(math.max((...).x), math.max((...).y), math.max((...).z), math.max((...).w))`.

```
vector.min(...: vector): vector
```

Applies `math.min` to the corresponding components of the input vectors. Includes the fourth component in _4-wide mode_. Equivalent to `vector.create(math.min((...).x), math.min((...).y), math.min((...).z), math.min((...).w))`.

5 changes: 4 additions & 1 deletion assets/js/luau_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@

"table.clear","table.clone","table.concat","table.create","table.find","table.freeze","table.insert","table.isfrozen","table.maxn","table.move","table.pack","table.remove","table.sort","table.unpack",

"utf8.char","utf8.charpattern","utf8.codepoint","utf8.codes","utf8.len","utf8.offset"
"utf8.char","utf8.charpattern","utf8.codepoint","utf8.codes","utf8.len","utf8.offset",

"vector.create", "vector.magnitude", "vector.normalize", "vector.cross", "vector.dot", "vector.angle",
"vector.floor", "vector.ceil", "vector.abs", "vector.sign", "vector.clamp", "vector.max", "vector.min"
]);
var keywords = wordRE(["and","break","elseif","false","nil","not","or","return",
"true","function", "end", "if", "then", "else", "do",
Expand Down

0 comments on commit 4439109

Please sign in to comment.