Class: EpiMath::Vector

Inherits:
Object
  • Object
show all
Defined in:
lib/epimath100/vector.class.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(par1, par2 = nil, par3 = nil, verbose = true) ⇒ Vector

Parameters:

par1

The first parameter may be a point coordonate x, or a other Vector. If it’s a Vector, it will be copied. Else if it is anything else, it will be converted as Float and stored as a abcissa.

par2,par3

Theses optionals parameters are used if the first parameter is not a Vector. If it occure, par2 is mandatory (but not par3). They will be stored in @y and @z.

verbose

verbose turn on/off the messages with translate, rotate, …

Returns:

himself

Errors::

If a parameter is invalid, it may be crash the programm with an ERR_HIGH



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
# File 'lib/epimath100/vector.class.rb', line 26

def initialize par1, par2=nil, par3=nil, verbose=true
  if par1.is_a?Vector #and par2 == nil and par3 == nil
    @x = par1.x
    @y = par1.y
    @z = par1.z

  elsif par1 != nil and par2 != nil
    if par3 == nil
      par3 = 1.0
    end

    if !Error.isnum?par1 or !Error.isnum?par2 or !Error.isnum?par3
      Error.call "Vector::new : a passed argument is not a valid number"
    end

    @x = par1.to_f
    @y = par2.to_f
    @z = par3.to_f
  else
    Error.call "Vector::new : The vector couldn't be initialisze with theses parameters : :par1 => '#{par1}', :par2 => '#{par2}'"
  end

  @verbose = verbose
  @matrix_op = init_matrix_op
  return self
end

Instance Attribute Details

#matrix_opObject (readonly)

Returns the value of attribute matrix_op.



9
10
11
# File 'lib/epimath100/vector.class.rb', line 9

def matrix_op
  @matrix_op
end

#verboseObject

Returns the value of attribute verbose.



8
9
10
# File 'lib/epimath100/vector.class.rb', line 8

def verbose
  @verbose
end

#xObject

Returns the value of attribute x.



8
9
10
# File 'lib/epimath100/vector.class.rb', line 8

def x
  @x
end

#yObject

Returns the value of attribute y.



8
9
10
# File 'lib/epimath100/vector.class.rb', line 8

def y
  @y
end

#zObject

Returns the value of attribute z.



8
9
10
# File 'lib/epimath100/vector.class.rb', line 8

def z
  @z
end

Class Method Details

.collinear?(u, v) ⇒ Boolean

Returns:

  • (Boolean)


359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/epimath100/vector.class.rb', line 359

def self.collinear? u, v
  Error.call "Vector::col : invalid parameters" if !u.is_a?Vector or !v.is_a?Vector
  Error.call "Vector::col : vector v1 null", Error::ERR_LOW if u.nil?
  Error.call "Vector::col : vector v2 null", Error::ERR_LOW if v.nil?

  x = u.y * v.z - u.z * v.y
  y = u.z * v.x - u.x * v.z
  z = u.x * v.y - u.y * v.x

  if x == 0 and y == 0 and z == 0
    true
  else
    false
  end
end

Instance Method Details

#*(par1) ⇒ Object

Parameters:

par1:

This parameter may be a Vector or a number. If it's a Number, it will multiply all coponents of the Vector.
If it's an other vector, we will multiplie their components 2by2

Returns:

Vector



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
# File 'lib/epimath100/vector.class.rb', line 113

def *(par1)
  out = Vector.new(self)
  if par1.is_a?Numeric
    out.x *= par1
    out.y *= par1
    if out.z != nil
      out.z *= par1
    end
  elsif par1.is_a?Matrix
    return (self.to_matrix * par1).to_vector
  elsif par1.is_a?Vector
    ary1 = [self.x, self.y]
    if out.z != nil
      ary1 << out.z
    end

    ary2 = [par1.x, par1.y]
    if out.z != nil
      ary2 << par1.z
    end

    aryr = Matrix.mult_array(ary1, ary2)
    out.x = aryr[0]
    out.y = aryr[1]
    if aryr[2] != nil
      out.z = aryr[2]
    end
  else
    Error.call "Unable to add '#{par1} to this vector", Error::ERR_HIGH
  end
  return out
end

#+(par1) ⇒ Object

Parameters:

par1

It may be a point coordonate x, or a other Vector. If it’s a Vector, it will be used as a couple of coordonates. Else if it is anything else, it will be converted as Float added to @x

Returns:

nothing

Errors::

If a parameter is invalid, it may be crash the programm with an ERR_HIGH If the vectors do not have the same dimensions, it will display a warning



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/epimath100/vector.class.rb', line 75

def +(par1)
  out = Vector.new(self)

  if par1 != nil and par1.is_a?Vector #and par2 == nil and par3 == nil
    out.x += par1.x
    out.y += par1.y

  elsif par1 != nil
    par1 = par1.to_f
    out.x += par1
    out.y += par1

    if out.z != nil and par1 == nil
      Error.call "The vector #{Vector.new(par1, par1, par1).to_s} do not have the same dimensions than #{out.to_s}", Error::ERR_HIGH
    elsif out.z != nil
      out.z += par1
    end
  else
    Error.call "The vector couldn't be added with this parameters : #{par1}", Error::ERR_HIGH
  end
  return out
end

#-(par1) ⇒ Object

Usage:

It is simply like + buf multiply by -1 par1



100
101
102
103
104
105
# File 'lib/epimath100/vector.class.rb', line 100

def -(par1)
  if par1 == nil
    Error.call "Can't sub nil from vector"
  end
  return (self.+(par1 * -1))
end

#collinear?(v) ⇒ Boolean

Parameters

type

Verify if two vectors are collinear

Return:

True if vector u and v are collinear else false

Returns:

  • (Boolean)


355
356
357
# File 'lib/epimath100/vector.class.rb', line 355

def collinear? v
  Vector::colineaire? @self, v
end

#homothétie(c1, c2, c3 = 1.0) ⇒ Object

Parameters:

c1,c2,c3

c1 and c2 are the coeficiens of the homothetie. c3 is optional



170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/epimath100/vector.class.rb', line 170

def homothétie c1, c2, c3=1.0
  if c1 == nil or c2 == nil
    Error.call "Coefficients are invalids"
  end

  s = Matrix.new [[c1.to_f, 0, 0], [0, c2.to_f, 0], [0, 0, c3.to_f]]
  @matrix_op = s
  cpy = self
  cpy.z = 1.0

  puts "homothétie de rapports #{c1.to_f}, #{c2.to_f}" if @verbose

  return (s * cpy.to_matrix).to_vector
end

#init_matrix_opObject

Returns:

Matrix.new [[1, 0, 0],[0, 1, 0], [0, 0, 1]]



60
61
62
# File 'lib/epimath100/vector.class.rb', line 60

def init_matrix_op
  @matrix_op = Matrix.new [[1, 0, 0],[0, 1, 0], [0, 0, 1]]
end

#nil?Boolean

Returns:

  • (Boolean)


342
343
344
345
346
347
348
# File 'lib/epimath100/vector.class.rb', line 342

def nil?
  if @x == 0 and @y == 0 and @z == 0
    true
  else
    false
  end
end

#normObject

Return to the norm of the currenet vector



54
55
56
# File 'lib/epimath100/vector.class.rb', line 54

def norm
  Math.sqrt(@x**2 + @y**2 + @z**2)
end

#proj_axe(axe = "x") ⇒ Object

Params:

axe

it must be “x” or “y” (case doesn’t checked)

Return:

The vector after the projection



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/epimath100/vector.class.rb', line 232

def proj_axe axe="x"
  if !axe.match(/[xy]/i)
    Error.call "Vector::proj_axe '#{axe} is not a valid axe", Error::ERR_HIGH
  end

  s = nil
  if axe.match(/x/i)
    s = Matrix.new [[1, 0, 0], [0, 0, 0], [0, 0, 1]]
  else
    s = Matrix.new [[0, 0, 0], [0, 1, 0], [0, 0, 1]]
  end

  @matrix_op = s
  cpy = self.to_matrix

  puts "projection sur un axe #{axe}." if @verbose

  return (s * cpy).to_vector
end

#rotate(a) ⇒ Object

Parameters:

a

The angle in degree

Return value:

It returns the vector after the translation.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/epimath100/vector.class.rb', line 191

def rotate a
  if a == nil
    Error.call "Angle is invalid"
  end

  rad = Math::PI * a.to_f / 180.0
  cpy = self # copy to have the same value in z
  cpy.z = 0.0
  s = Matrix.new [[ Math.cos(rad), -Math.sin(rad), 0], [Math.sin(rad), Math.cos(rad), 0], [0, 0, 1]]
  @matrix_op = s

  puts "rotation d'angle #{a.to_f}" if @verbose

  return (s * cpy.to_matrix).to_vector
end

#symetric(angle) ⇒ Object

Parameters :

angle

It is the incline of the line.

Return value:

It returns the vector after the translation.



213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/epimath100/vector.class.rb', line 213

def symetric angle
  Error.call "Variable angle is not a number (#{angle})", Error::ERR_HIGH if !Error.isnum? angle.to_s

  rad = Math::PI * angle.to_f / 180.0
  s = Matrix.new [[Math.cos(2 * rad), Math.sin(2 * rad), 0], [Math.sin(2 * rad), -Math.cos(2 * rad), 0], [0, 0, 1]]
  @matrix_op = s
  cpy = self.to_matrix

  puts "symétrie par rapport à un axe incliné de #{angle.to_f} degrés" if @verbose

  return (s * cpy).to_vector
end

#symetric_pointoObject

Params:

nothing

Return:

The vector after the translation



256
257
258
# File 'lib/epimath100/vector.class.rb', line 256

def symetric_pointo
  return homothétie(-1, -1)
end

#to_a(type = String) ⇒ Object

Parameters:

type

It specify the return. It may be String or Array.

Returns:

String, Array, nil (see type::)

Errors:

nil return occures only if the parameter types

is not supported.



330
331
332
333
334
335
336
337
338
339
340
# File 'lib/epimath100/vector.class.rb', line 330

def to_a type=String
  if type == String
    return self.to_s
  elsif type == Array
    return self.to_ary
  elsif type == Matrix
    return self.to_matrix
  else
    return nil
  end
end

#to_ary(type = 0) ⇒ Object

Parameters:

type

Optionnal. It specify the format of the array returned. It may be “h” (1) or “w” (0).

  • If it’s “w” or 0, the Array will be [x,y]

  • If it’s “h” or 1, the Array returned will be [[x],]

Returns:

Array or an Array of Array



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/epimath100/vector.class.rb', line 296

def to_ary type=0
  array = []
  if type == 0 or type == "w" # everytime true... for the moment
    array << @x << @y
    if @z != nil
      array << @z
    end
  else
    array << [@x] << [@y]
    if @z != nil
      array << [@z]
    end
  end
  return array
end

#to_matrixObject

Parameters::

Nothing

Returns:

Matrix



317
318
319
# File 'lib/epimath100/vector.class.rb', line 317

def to_matrix
  return Matrix.new self.to_ary(1)
end

#to_s(dim = 3, type = String) ⇒ Object

Parameters:

dim

Option option to choose the desired number of dimension of the vector (if is it in 3d, it will be flattened)

type

Optional and not used yet. It specify the format of the string. It may only be String yet.

Returns:

String



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/epimath100/vector.class.rb', line 268

def to_s dim=3, type=String
  string = ""
  if type == String
    if @y == nil or dim == 1
      string = "(#{@x.to_f.round(3)})"
    elsif @z == nil or dim == 2
      string = "(#{@x.to_f.round(3)}; #{@y.to_f.round(3)})"
    elsif dim == 3
      string = "(#{@x.to_f.round(3)}; #{@y.to_f.round(3)}; #{@z.to_f.round(3)})"
    else
      Error.call "Vector::to_s : Invalid number of dimension specified"
    end

  else
    Error.call "Vector::to_s : Invalid type conversion", ERR_HIGH
  end

  return string
end

#translate(par1, par2, par3 = 0.0) ⇒ Object

Parameters:

par1, par2

They are the components of the vector to translate.

par3

Optional component (z)

see +


152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/epimath100/vector.class.rb', line 152

def translate par1, par2, par3=0.0
  if !Error.isnum? par1 or !Error.isnum? par2 or !Error.isnum? par3
    Error.call "A parameter to the translation is not a valid number", Error::ERR_HIGH
  end

  s = Matrix.new [[1.0, 0.0, par1.to_f], [0.0, 1.0, par2.to_f], [0.0, 0.0, 1.0]]
  @matrix_op = s
  cpy = self
  cpy.z = 1.0

  puts "translation de vecteur #{Vector.new(par1,par2,par3).to_s}" if @verbose

  return (s * cpy.to_matrix).to_vector
end