103 lines
843 B
Markdown
103 lines
843 B
Markdown
|
---
|
||
|
title: Vector
|
||
|
tags: note vector math
|
||
|
created: 2023-08-29T11:59:28Z
|
||
|
published: true
|
||
|
---
|
||
|
|
||
|
## 2D
|
||
|
|
||
|
### Angle
|
||
|
|
||
|
```go
|
||
|
atan2(y, x)
|
||
|
```
|
||
|
|
||
|
### Cross
|
||
|
|
||
|
```go
|
||
|
x * y2 - y * x2
|
||
|
```
|
||
|
|
||
|
### Distance
|
||
|
|
||
|
```go
|
||
|
(a - b).Length()
|
||
|
```
|
||
|
|
||
|
### Dot
|
||
|
|
||
|
```go
|
||
|
x * x2 + y * y2
|
||
|
```
|
||
|
|
||
|
### From angle
|
||
|
|
||
|
```go
|
||
|
x = cos(angle)
|
||
|
y = sin(angle)
|
||
|
```
|
||
|
|
||
|
### Length
|
||
|
|
||
|
```go
|
||
|
sqrt(x * x + y * y)
|
||
|
```
|
||
|
|
||
|
### Normalize
|
||
|
|
||
|
```go
|
||
|
length := x * x + y * y
|
||
|
|
||
|
if length == 0 {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
length = sqrt(length)
|
||
|
x /= length
|
||
|
y /= length
|
||
|
```
|
||
|
|
||
|
## 3D
|
||
|
|
||
|
### Cross
|
||
|
|
||
|
```go
|
||
|
( y * z2 - z * y2,
|
||
|
z * x2 - x * z2,
|
||
|
x * y2 - y * x2 )
|
||
|
```
|
||
|
|
||
|
### Distance
|
||
|
|
||
|
```go
|
||
|
(a - b).Length()
|
||
|
```
|
||
|
|
||
|
### Dot
|
||
|
|
||
|
```go
|
||
|
x * x2 + y * y2 + z * z2
|
||
|
```
|
||
|
|
||
|
### Length
|
||
|
|
||
|
```go
|
||
|
sqrt(x * x + y * y + z * z)
|
||
|
```
|
||
|
|
||
|
### Normalize
|
||
|
|
||
|
```go
|
||
|
length := x * x + y * y + z * z
|
||
|
|
||
|
if length == 0 {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
length = sqrt(length)
|
||
|
x /= length
|
||
|
y /= length
|
||
|
z /= length
|
||
|
```
|