Class: Mageo::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/mageo/segment.rb

Overview

線分を表すクラス。

Defined Under Namespace

Classes: InitializeError, TypeError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vector0, vector1) ⇒ Segment

端点を2つ渡す。

Raises:



13
14
15
16
17
18
19
# File 'lib/mageo/segment.rb', line 13

def initialize(vector0, vector1)
  raise InitializeError if vector0.class != Mageo::Vector3D
  raise InitializeError if vector1.class != Mageo::Vector3D
  raise InitializeError if vector0 == vector1
  
  @endpoints = [vector0, vector1]
end

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



6
7
8
# File 'lib/mageo/segment.rb', line 6

def endpoints
  @endpoints
end

Instance Method Details

#==(other) ⇒ Object

Raises:



72
73
74
75
76
77
78
# File 'lib/mageo/segment.rb', line 72

def ==(other)
  raise TypeError if other.class != Mageo::Segment
  @endpoints.size.times do |i|
    return false unless other.endpoints[i] == @endpoints[i]
  end
  return true
end

#eql?(other) ⇒ Boolean

等価チェック。 uniq できるようにするため。

Returns:

  • (Boolean)

Raises:



64
65
66
67
68
69
70
# File 'lib/mageo/segment.rb', line 64

def eql?(other)
  raise TypeError if other.class != Mageo::Segment
  @endpoints.each do |point|
    return false unless other.endpoints.include?(point)
  end
  return true
end

#include?(position, tolerance) ⇒ Boolean

position で与えられた点が線分の中にある点か? tolerance = 0.0 では計算誤差のためにほとんど真とならない。 position は Mageo::Vector3D クラスインスタンスでなければならない。

Returns:

  • (Boolean)

Raises:



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
# File 'lib/mageo/segment.rb', line 24

def include?(position, tolerance)
  raise TypeError if position.class != Mageo::Vector3D

  vec_self = @endpoints[1] - @endpoints[0]
  vec_other = position - @endpoints[0]

  # 両端の点は計算誤差で失敗しやすいので true にしておく。
  return true if position == @endpoints[0]
  return true if position == @endpoints[1]

  # 長さ方向チェック
  inner_product = vec_self.inner_product(vec_other)
  return false if (inner_product < 0.0 )
  return false if ( vec_self.r ** 2 < inner_product)

  # 垂直方向チェック
  vec_outer = vec_other - vec_self * (inner_product / (vec_self.r)**2)
  return false if tolerance < vec_outer.r

  # ここまでチェックを通過すれば true
  return true

  ##ex_product = vec_self.exterior_product(vec_other)
  #あかんな。
  #normalize して、
  #この方向の成分を出さんと。
  #外積もおかしい。

  #return false if ( ex_product[2].abs > tolerance )

end

#to_v3dObject

endpoints で取り出せる座標2つのうち、最初のものから最後のものへのベクトルを表す Mageo::Vector3D クラスインスタンスを返す。



58
59
60
# File 'lib/mageo/segment.rb', line 58

def to_v3d
  return @endpoints[1] - @endpoints[0]
end