/
vpvec4i32.go
161 lines (124 loc) · 3.33 KB
/
vpvec4i32.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Vapor is a toolkit designed to support Liquid War 7.
// Copyright (C) 2015, 2016 Christian Mauduit <ufoot@ufoot.org>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Vapor homepage: https://github.com/ufoot/vapor
// Contact author: ufoot@ufoot.org
package vpvec4
import (
"encoding/json"
"github.com/ufoot/vapor/go/vpnumber"
)
// I32 is a vector containing 4 int32 values.
// Can hold the values of a point in a plane.
type I32 [Size]int32
// I32New creates a new vector containing 4 int32 values.
func I32New(i1, i2, i3, i4 int32) *I32 {
return &I32{i1, i2, i3, i4}
}
// ToI64 converts the vector to an int64 vector.
func (vec *I32) ToI64() *I64 {
var ret I64
for i, v := range vec {
ret[i] = int64(v)
}
return &ret
}
// ToX32 converts the vector to a fixed point number vector on 32 bits.
func (vec *I32) ToX32() *X32 {
var ret X32
for i, v := range vec {
ret[i] = vpnumber.I32ToX32(v)
}
return &ret
}
// ToX64 converts the vector to a fixed point number vector on 64 bits.
func (vec *I32) ToX64() *X64 {
var ret X64
for i, v := range vec {
ret[i] = vpnumber.I32ToX64(v)
}
return &ret
}
// ToF32 converts the vector to a float32 vector.
func (vec *I32) ToF32() *F32 {
var ret F32
for i, v := range vec {
ret[i] = float32(v)
}
return &ret
}
// ToF64 converts the vector to a float64 vector.
func (vec *I32) ToF64() *F64 {
var ret F64
for i, v := range vec {
ret[i] = float64(v)
}
return &ret
}
// String returns a readable form of the vector.
func (vec *I32) String() string {
buf, err := json.Marshal(vec)
if err != nil {
// Catching & ignoring error
return ""
}
return string(buf)
}
// Add adds operand to the vector.
// It modifies the vector, and returns a pointer on it.
func (vec *I32) Add(op *I32) *I32 {
for i, v := range op {
vec[i] += v
}
return vec
}
// Sub substracts operand from the vector.
// It modifies the vector, and returns a pointer on it.
func (vec *I32) Sub(op *I32) *I32 {
for i, v := range op {
vec[i] -= v
}
return vec
}
// Neg changes the sign of all vector members.
// It modifies the vector, and returns a pointer on it.
func (vec *I32) Neg() *I32 {
for i, v := range vec {
vec[i] = -v
}
return vec
}
// I32Add adds two vectors.
// Args are left untouched, a pointer on a new object is returned.
func I32Add(veca, vecb *I32) *I32 {
var ret = *veca
_ = ret.Add(vecb)
return &ret
}
// I32Sub substracts vector b from vector a.
// Args are left untouched, a pointer on a new object is returned.
func I32Sub(veca, vecb *I32) *I32 {
var ret = *veca
_ = ret.Sub(vecb)
return &ret
}
// I32Neg changes the sign of all vector members.
// Arg is left untouched, a pointer on a new object is returned.
func I32Neg(vec *I32) *I32 {
var ret = *vec
_ = ret.Neg()
return &ret
}