Class: Vector

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/mageo/vector.rb,
lib/mageo/polar2d.rb,
lib/mageo/vector3d.rb

Overview

! /usr/bin/env ruby coding: utf-8

Direct Known Subclasses

Mageo::Vector3D

Defined Under Namespace

Classes: SizeError, ZeroOperationError

Instance Method Summary collapse

Instance Method Details

#floorObject



16
17
18
# File 'lib/mageo/vector.rb', line 16

def floor
  self.class[* self.map {|val| val.floor}.to_a]
end

#to_p2dObject

Mageo::Polar2D クラスインスタンスへの変換。

Raises:



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/mageo/polar2d.rb', line 8

def to_p2d
  raise Vector::SizeError if self.size != 2
  x, y = *self
  r = Math::sqrt( x**2 + y**2 )

  theta = 0.0
  #ゼロ割り対策
  if ( ( x == 0 ) && ( y == 0 ) )
    theta = 0.0 * PI
  elsif ( ( x == 0 ) && ( y > 0 ) )
    theta = 0.5 * PI
  elsif ( ( x == 0 ) && ( y < 0 ) )
    theta = -0.5 * PI
  elsif ( ( x > 0 ) && ( y == 0 ) )
    theta = 0.0 * PI
  elsif ( ( x < 0 ) && ( y == 0 ) )
    theta = 1.0 * PI
  else
    theta = Math::atan( y/x )
    theta += PI if ( x < 0) 
  end
  Mageo::Polar2D.new( r, theta ).minimize_theta
end

#to_v3dObject

Return a new instance converted to Mageo::Vector3D class.



17
18
19
20
# File 'lib/mageo/vector3d.rb', line 17

def to_v3d
  Mageo::Vector3D[*self]
  #要素数チェックは Mageo::Vector3D.[] 側でやっている。
end

#unit_vectorObject

Get a unit vector.



9
10
11
12
13
14
# File 'lib/mageo/vector.rb', line 9

def unit_vector
  len = self.r
  raise Vector::ZeroOperationError if (len == 0.0)
  self * (1.0/len)
  # Mageo::Vector3D.new(@x*(1.0/len), @y*(1.0/len), @z*(1.0/len))
end