summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/pgf/graphdrawing/lua/pgf/gd/deprecated/Vector.lua
blob: 8af7378154e396983c084defe3e14a0e1b892513 (plain)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
-- Copyright 2012 by Till Tantau
--
-- This file may be distributed an/or modified
--
-- 1. under the LaTeX Project Public License and/or
-- 2. under the GNU Public License
--
-- See the file doc/generic/pgf/licenses/LICENSE for more information

-- @release $Header$


--- Vector class
--
-- This class augments a normal array so that:
--
-- 1) Several functions like "plus" or "normalize" become available.
-- 2) You can access the ".x" and ".y" fields to get the fields [1] and [2].

local Vector = {}


-- Namespace:
local lib = require "pgf.gd.lib"
lib.Vector = Vector


-- Class setup
Vector.__index =
  function (t, k)
    if k == "x" then
      return rawget(t,1)
    elseif k == "y" then
      return rawget(t,2)
    else
      return rawget(Vector,k)
    end
  end
Vector.__newindex =
  function (t, k, v)
    if k == "x" then
      rawset(t,1,v)
    elseif k == "y" then
      rawset(t,2,v)
    else
      rawset(t,k,v)
    end
  end



--- Creates a new vector with \meta{n} values using an optional \meta{fill\_function}.
--
-- @param n             The number of elements of the vector. (If omitted, then 2.)
-- @param fill_function Optional function that takes a number between 1 and \meta{n}
--                      and is expected to return a value for the corresponding element
--                      of the vector. If omitted, all elements of the vector will
--                      be initialized with 0.
--
-- @return A newly-allocated vector with \meta{n} elements.
--
function Vector.new(n, fill_function)
  -- create vector
  local vector = { }
  setmetatable(vector, Vector)

  local n = n or 2

  if type(n) == 'table' then
    for k,v in pairs(n) do
      vector[k] = v
    end
  else
    -- fill vector elements with values
    if not fill_function then
      for i = 1,n do
        rawset(vector,i,0)
      end
    else
      for i = 1,n do
        rawset(vector,i,fill_function(i))
      end
    end
  end

  return vector
end



--- Creates a copy of the vector that holds the same elements as the original.
--
-- @return A newly-allocated copy of the vector holding exactly the same elements.
--
function Vector:copy()
  return Vector.new(#self, function (n) return self[n] end)
end



--- Performs a vector addition and returns the result in a new vector.
--
-- @param other The vector to add.
--
-- @return A new vector with the result of the addition.
--
function Vector:plus(other)
  assert(#self == #other)

  return Vector.new(#self, function (n) return self[n] + other[n] end)
end



--- Subtracts two vectors and returns the result in a new vector.
--
-- @param other Vector to subtract.
--
-- @return A new vector with the result of the subtraction.
--
function Vector:minus(other)
  assert(#self == #other)

  return Vector.new(#self, function (n) return self[n] - other[n] end)
end



--- Divides a vector by a scalar value and returns the result in a new vector.
--
-- @param scalar Scalar value to divide the vector by.
--
-- @return A new vector with the result of the division.
--
function Vector:dividedByScalar(scalar)
  return Vector.new(#self, function (n) return self[n] / scalar end)
end



--- Multiplies a vector by a scalar value and returns the result in a new vector.
--
-- @param scalar Scalar value to multiply the vector with.
--
-- @return A new vector with the result of the multiplication.
--
function Vector:timesScalar(scalar)
  return Vector.new(#self, function (n) return self[n] * scalar end)
end



--- Performs the dot product of two vectors and returns the result in a new vector.
--
-- @param other Vector to perform the dot product with.
--
-- @return A new vector with the result of the dot product.
--
function Vector:dotProduct(other)
  assert(#self == #other)

  local product = 0
  for n = 1,#self do
    product = product + self[n] * other[n]
  end
  return product
end



--- Computes the Euclidean norm of the vector.
--
-- @return The Euclidean norm of the vector.
--
function Vector:norm()
  return math.sqrt(self:dotProduct(self))
end



--- Normalizes the vector and returns the result in a new vector.
--
-- @return Normalized version of the original vector.
--
function Vector:normalized()
  local norm = self:norm()
  if norm == 0 then
    return Vector.new(#self)
  else
    return self:dividedByScalar(self:norm())
  end
end



--- Updates the values of the vector in-place.
--
-- @param update_function A function that is called for each element of the
--                        vector. The elements are replaced by the values
--                        returned from this function.
--
function Vector:update(update_function)
  for i=1,#self do
    self[i] = update_function(self[i])
  end
end



--- Limits all elements of the vector in-place.
--
-- @param limit_function A function that is called for each index/element
--                       pair. It is supposed to return minimum and maximum
--                       values for the element. The element is then clamped
--                       to these values.
--
function Vector:limit(limit_function)
  for i=1,#self do
    local min, max = limit_function(i, self[i])
    self[i] = math.max(min, math.min(max, value))
  end
end


--- Tests whether all elements of two vectors are the same
--
-- @param other The other vector
--
-- @return true or false
--
function Vector:equals(other)
  if #self ~= #other then
    return false
  end

  for n = 1, #self do
    if self[n] ~= other[n] then
      return false
    end
  end

  return true
end


function Vector:__tostring()
  return '(' .. table.concat(self, ', ') .. ')'
end





-- Done

return Vector