Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/akicho8/stylet_support

GUIに依存しない数学関連ライブラリ
https://github.com/akicho8/stylet_support

library mathematics vector

Last synced: 3 days ago
JSON representation

GUIに依存しない数学関連ライブラリ

Awesome Lists containing this project

README

        

* GUIに依存しない数学関連ライブラリ

#+BEGIN_SRC ruby
Point.new # => [0.0, 0.0]
Point[1, 2] # => [1, 2]
Point[1, 2].members # => [:x, :y]
Point[1, 2].values # => [1, 2]
Point[1, 2] # => [1, 2]

Vector.superclass # => Stylet::Point2

Point.new(Point[1,2]) # => [1, 2]
Point[Point[1,2]] # => [1, 2]

Vector.new # => [0.0, 0.0]
Vector.new # => [0.0, 0.0]
Vector.new(1, 2) # => [1, 2]

Vector.zero # => [0.0, 0.0]
Vector.one # => [1.0, 1.0]

a = Vector[1, 2]
b = Vector[3, 4]

a + b # => [4.0, 6.0]
a - b # => [-2.0, -2.0]
a * 2 # => [2.0, 4.0]
a / 2 # => [0.5, 1.0]

a.add(b) # => [4.0, 6.0]
a.sub(b) # => [-2.0, -2.0]
a.scale(2) # => [2.0, 4.0]
a.mul(2) # => [2.0, 4.0]
a.div(2) # => [0.5, 1.0]

a + [3, 4] # => [4.0, 6.0]
a - [3, 4] # => [-2.0, -2.0]

Vector.one.reverse # => [-1.0, -1.0]
-Vector.one # => [-1.0, -1.0]

Vector[3, 4].normalize # => [0.6, 0.8]

Vector.one.normalize # => [0.7071067811865475, 0.7071067811865475]
Vector.one.magnitude # => 1.4142135623730951
Vector.one.magnitude_sq # => 2.0

v = Vector.rand
v.round(2) # => [-1.0, -0.88]
v.round # => [-1, -1]
v.floor # => [-1, -1]
v.ceil # => [0, 0]
v.truncate # => [0, 0]

Vector.rand # => [-0.5758140223421724, -0.23276547457672714]
Vector.rand(3) # => [1, 1]
Vector.rand(3..4) # => [4, 4]
Vector.rand(3.0..4) # => [3.9402342251571714, 3.550203689124972]
Vector.rand(-2.0..2.0) # => [-1.181268220930995, 1.4942116900421252]

Vector[1, 0].dot_product(Vector[1, 0]) # => 1
Vector[1, 0].dot_product(Vector[-1, 0]) # => -1

Vector.cross_product(Vector.rand, Vector.rand) # => -0.7348374986070235

Vector.rand.distance_to(Vector.rand) # => 1.7226903872525836

v = Vector.new
v.object_id # => 70298637530620
v.replace(Vector.rand) # => [-0.2619785178209082, -0.4396795163426983]
v.object_id # => 70298637530620

Vector.zero.distance_to(Vector.one) # => 1.4142135623730951

Vector.zero.zero? # => true
Vector.one.nonzero? # => true

Vector.zero.inspect # => "[0.0, 0.0]"
Vector.zero.to_s # => "[0.0, 0.0]"

Vector[1,2].prep # => [-2, 1]
#+END_SRC

** TIPS

*** 当たり判定を高速化するには?

当たり判定を次のようにしているとき

#+BEGIN_SRC ruby
if v.magnitude < r
end
#+END_SRC

次のようにすると sqrt を省略できる

#+BEGIN_SRC ruby
if v.magnitude_sq < r ** 2
end
#+END_SRC

*** a地点からb地点へのベクトルを求めるには?

#+BEGIN_SRC ruby
b - a
#+END_SRC