ray 0.0.1 → 0.1.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/.yardopts +4 -0
- data/README.md +17 -21
- data/Rakefile +18 -139
- data/VERSION +1 -1
- data/ext/audio.cpp +723 -0
- data/ext/{color.c → color.cpp} +25 -13
- data/ext/drawable.cpp +91 -0
- data/ext/event.cpp +460 -0
- data/ext/extconf.rb +5 -104
- data/ext/font.cpp +190 -0
- data/ext/image.cpp +733 -0
- data/ext/input.cpp +74 -0
- data/ext/ray.cpp +168 -0
- data/ext/ray.hpp +356 -0
- data/ext/{rect.c → rect.cpp} +51 -37
- data/ext/shader.cpp +169 -0
- data/ext/shape.cpp +409 -0
- data/ext/sprite.cpp +306 -0
- data/ext/text.cpp +181 -0
- data/ext/vector.cpp +215 -0
- data/guide.md +619 -0
- data/lib/ray/audio.rb +0 -41
- data/lib/ray/color.rb +32 -10
- data/lib/ray/drawable.rb +16 -0
- data/lib/ray/dsl/event_listener.rb +25 -2
- data/lib/ray/dsl/event_runner.rb +33 -5
- data/lib/ray/dsl/event_translator.rb +66 -30
- data/lib/ray/dsl/handler.rb +3 -2
- data/lib/ray/dsl/matcher.rb +58 -14
- data/lib/ray/font.rb +38 -96
- data/lib/ray/font_set.rb +8 -8
- data/lib/ray/game.rb +87 -66
- data/lib/ray/helper.rb +105 -10
- data/lib/ray/image.rb +150 -24
- data/lib/ray/image_set.rb +3 -1
- data/lib/ray/input.rb +10 -0
- data/lib/ray/music_set.rb +5 -3
- data/lib/ray/ray.rb +21 -9
- data/lib/ray/rect.rb +48 -7
- data/lib/ray/rmagick.rb +41 -0
- data/lib/ray/scene.rb +99 -43
- data/lib/ray/scene_list.rb +67 -0
- data/lib/ray/shape.rb +132 -0
- data/lib/ray/sound_set.rb +4 -2
- data/lib/ray/sprite.rb +49 -111
- data/lib/ray/text.rb +101 -0
- data/lib/ray/text_helper.rb +37 -0
- data/lib/ray/turtle.rb +215 -0
- data/lib/ray/vector.rb +226 -0
- data/samples/audio/spacial.rb +44 -0
- data/samples/hello_world/hello.rb +9 -13
- data/samples/hello_world/hello_dsl.rb +8 -12
- data/samples/hello_world/text.rb +15 -0
- data/samples/opengl/binding.rb +38 -0
- data/samples/opengl/image.rb +32 -0
- data/samples/opengl/opengl.rb +34 -0
- data/samples/opengl/shader.rb +42 -0
- data/samples/pong/pong.rb +14 -10
- data/samples/run_scene.rb +53 -0
- data/samples/shaders/scene.rb +40 -0
- data/samples/shaders/shaders.rb +42 -0
- data/samples/shaders/shape.rb +34 -0
- data/samples/sokoban/sokoban.rb +18 -18
- data/samples/test/actual_scene.rb +41 -0
- data/samples/test/scene_riot.rb +39 -0
- data/samples/test/scene_spec.rb +32 -0
- data/samples/test/scene_test_unit.rb +25 -0
- data/samples/turtle/byzantium.rb +45 -0
- data/samples/turtle/hilbert.rb +48 -0
- data/samples/turtle/koch.rb +55 -0
- data/samples/turtle/mandala.rb +61 -0
- data/samples/turtle/tree.rb +57 -0
- data/test/audio_test.rb +69 -0
- data/test/color_test.rb +77 -0
- data/test/drawable_test.rb +19 -0
- data/test/dsl_test.rb +93 -0
- data/test/font_test.rb +57 -0
- data/test/helpers.rb +94 -0
- data/test/image_test.rb +82 -0
- data/test/ray_test.rb +25 -0
- data/test/rect_test.rb +121 -0
- data/{spec → test}/res/VeraMono.ttf +0 -0
- data/{spec → test}/res/aqua.bmp +0 -0
- data/{spec → test}/res/aqua.png +0 -0
- data/{spec → test}/res/aqua2.bmp +0 -0
- data/{spec → test}/res/not_a_jpeg.jpeg +0 -0
- data/{spec → test}/res/pop.wav +0 -0
- data/test/resource_set_test.rb +99 -0
- data/test/run_all.rb +7 -0
- data/test/shape_test.rb +101 -0
- data/test/sprite_test.rb +89 -0
- data/test/text_test.rb +78 -0
- data/test/turtle_test.rb +176 -0
- data/test/vector_test.rb +111 -0
- data/yard_ext.rb +0 -28
- metadata +95 -139
- data/.gitignore +0 -23
- data/.gitmodules +0 -3
- data/.rspec +0 -3
- data/ext/audio.c +0 -473
- data/ext/event.c +0 -557
- data/ext/font.c +0 -287
- data/ext/image.c +0 -933
- data/ext/joystick.c +0 -145
- data/ext/ray.c +0 -489
- data/ext/ray.h +0 -245
- data/ext/ray_osx.m +0 -161
- data/lib/ray/joystick.rb +0 -30
- data/psp/SDL_psp_main.c +0 -84
- data/psp/bigdecimal/README +0 -60
- data/psp/bigdecimal/bigdecimal.c +0 -4697
- data/psp/bigdecimal/bigdecimal.h +0 -216
- data/psp/bigdecimal/lib/bigdecimal/jacobian.rb +0 -85
- data/psp/bigdecimal/lib/bigdecimal/ludcmp.rb +0 -84
- data/psp/bigdecimal/lib/bigdecimal/math.rb +0 -235
- data/psp/bigdecimal/lib/bigdecimal/newton.rb +0 -77
- data/psp/bigdecimal/lib/bigdecimal/util.rb +0 -65
- data/psp/digest/bubblebabble/bubblebabble.c +0 -142
- data/psp/digest/defs.h +0 -20
- data/psp/digest/digest.c +0 -643
- data/psp/digest/digest.h +0 -32
- data/psp/digest/lib/digest.rb +0 -50
- data/psp/digest/lib/md5.rb +0 -27
- data/psp/digest/lib/sha1.rb +0 -27
- data/psp/digest/md5/md5.c +0 -420
- data/psp/digest/md5/md5.h +0 -80
- data/psp/digest/md5/md5init.c +0 -40
- data/psp/digest/rmd160/rmd160.c +0 -457
- data/psp/digest/rmd160/rmd160.h +0 -56
- data/psp/digest/rmd160/rmd160init.c +0 -40
- data/psp/digest/sha1/sha1.c +0 -269
- data/psp/digest/sha1/sha1.h +0 -39
- data/psp/digest/sha1/sha1init.c +0 -40
- data/psp/digest/sha2/lib/sha2.rb +0 -73
- data/psp/digest/sha2/sha2.c +0 -919
- data/psp/digest/sha2/sha2.h +0 -109
- data/psp/digest/sha2/sha2init.c +0 -52
- data/psp/enumerator/enumerator.c +0 -298
- data/psp/etc/etc.c +0 -559
- data/psp/ext.c +0 -289
- data/psp/fcntl/fcntl.c +0 -187
- data/psp/lib/rbconfig.rb +0 -178
- data/psp/nkf/lib/kconv.rb +0 -367
- data/psp/nkf/nkf-utf8/config.h +0 -88
- data/psp/nkf/nkf-utf8/nkf.c +0 -6040
- data/psp/nkf/nkf-utf8/utf8tbl.c +0 -8500
- data/psp/nkf/nkf-utf8/utf8tbl.h +0 -34
- data/psp/nkf/nkf.c +0 -654
- data/psp/socket/addrinfo.h +0 -173
- data/psp/socket/getaddrinfo.c +0 -676
- data/psp/socket/getnameinfo.c +0 -270
- data/psp/socket/pspsocket.c +0 -71
- data/psp/socket/pspsocket.h +0 -28
- data/psp/socket/socket.c +0 -4662
- data/psp/socket/sockport.h +0 -76
- data/psp/stringio/stringio.c +0 -1306
- data/psp/strscan/strscan.c +0 -1320
- data/psp/syck/bytecode.c +0 -1166
- data/psp/syck/emitter.c +0 -1242
- data/psp/syck/gram.c +0 -1894
- data/psp/syck/gram.h +0 -79
- data/psp/syck/handler.c +0 -174
- data/psp/syck/implicit.c +0 -2990
- data/psp/syck/node.c +0 -408
- data/psp/syck/rubyext.c +0 -2367
- data/psp/syck/syck.c +0 -504
- data/psp/syck/syck.h +0 -456
- data/psp/syck/token.c +0 -2725
- data/psp/syck/yaml2byte.c +0 -257
- data/psp/syck/yamlbyte.h +0 -170
- data/psp/thread/thread.c +0 -1175
- data/psp/zlib/zlib.c +0 -3547
- data/script.rb +0 -10
- data/spec/ray/audio_spec.rb +0 -146
- data/spec/ray/color_spec.rb +0 -57
- data/spec/ray/event_spec.rb +0 -80
- data/spec/ray/font_spec.rb +0 -93
- data/spec/ray/image_set_spec.rb +0 -48
- data/spec/ray/image_spec.rb +0 -162
- data/spec/ray/joystick_spec.rb +0 -21
- data/spec/ray/matcher_spec.rb +0 -50
- data/spec/ray/ray_spec.rb +0 -88
- data/spec/ray/rect_spec.rb +0 -154
- data/spec/ray/resource_set_spec.rb +0 -105
- data/spec/ray/sprite_spec.rb +0 -163
- data/spec/spec.opts +0 -4
- data/spec/spec_helper.rb +0 -8
data/lib/ray/text.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
module Ray
|
4
|
+
class Text
|
5
|
+
include Ray::TextHelper
|
6
|
+
|
7
|
+
# @param [String] content The content of the text
|
8
|
+
# @option opts :encoding ("utf-8") The encoding for the text. Unneeded in
|
9
|
+
# 1.9.
|
10
|
+
# @option opts :size (12) The character size.
|
11
|
+
# @option opts :style (Font::STYLE_NORMAL) Style of the text.
|
12
|
+
# @option opts :at ((0, 0)) Position of the text.
|
13
|
+
# @option opts :scale ((1, 1)) Scale applied to the text.
|
14
|
+
# @option opts :zoom Same as :scale
|
15
|
+
# @option opts :angle (0) Rotation applied to the text.
|
16
|
+
# @option opts :color (Ray::Color.white) The color used to draw the text
|
17
|
+
# @option opts :font [Ray::Font, String] (Ray::Font.default) Font used to
|
18
|
+
# draw the text.
|
19
|
+
def initialize(content, opts = {})
|
20
|
+
opts = {
|
21
|
+
:encoding => content.respond_to?(:encoding) ? content.encoding : "utf-8",
|
22
|
+
:size => 12,
|
23
|
+
:style => 0,
|
24
|
+
:at => [0, 0],
|
25
|
+
:angle => 0,
|
26
|
+
:color => Ray::Color.white
|
27
|
+
}.merge(opts)
|
28
|
+
|
29
|
+
@encoding = opts[:encoding].to_s
|
30
|
+
|
31
|
+
self.content = content
|
32
|
+
self.character_size = opts[:size]
|
33
|
+
self.style = opts[:style]
|
34
|
+
self.pos = opts[:at]
|
35
|
+
self.scale = opts[:scale] || opts[:zoom] || [1, 1]
|
36
|
+
self.angle = opts[:angle]
|
37
|
+
self.color = opts[:color]
|
38
|
+
|
39
|
+
if font = opts[:font]
|
40
|
+
self.font = font.is_a?(String) ? Ray::FontSet[font] : font
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Draws the text.
|
45
|
+
# @option opts [Ray::Image] :on (Ray.screen) The image to draw on.
|
46
|
+
def draw(opts = {})
|
47
|
+
draw_on(opts[:on] || Ray.screen)
|
48
|
+
end
|
49
|
+
|
50
|
+
def x; pos.x; end
|
51
|
+
def y; pos.y; end
|
52
|
+
|
53
|
+
def x=(val); self.pos = [val, y]; end
|
54
|
+
def y=(val); self.pos = [x, val]; end
|
55
|
+
|
56
|
+
def scale_x; scale.x; end
|
57
|
+
def scale_y; scale.y; end
|
58
|
+
|
59
|
+
def scale_x=(val); self.scale = [val, scale_y]; end
|
60
|
+
def scale_y=(val); self.scale = [scale_x, val]; end
|
61
|
+
|
62
|
+
alias :to_rect :rect
|
63
|
+
|
64
|
+
# @return [Ray::Vector2] The size of the text, taking the scale
|
65
|
+
# in account.
|
66
|
+
def size
|
67
|
+
rect.size
|
68
|
+
end
|
69
|
+
|
70
|
+
## @param [Integer, Array<Symbol>] style Flags for the font style.
|
71
|
+
# Valid symbols are :normal, :italic, :bold, and :underline.
|
72
|
+
def style=(style)
|
73
|
+
basic_set_style parse_style(style)
|
74
|
+
end
|
75
|
+
|
76
|
+
# @return [String] The string used by the text, using @encoding as the
|
77
|
+
# encoding.
|
78
|
+
def content
|
79
|
+
Iconv.conv(@encoding, INTERNAL_ENCODING, basic_content)
|
80
|
+
end
|
81
|
+
|
82
|
+
# Sets the string used by the text.
|
83
|
+
# In 1.9, @encoding is changed approprietly. In 1.8, it is defaulted
|
84
|
+
# to utf-8. Change it to whatever is the right encoding.
|
85
|
+
def content=(string)
|
86
|
+
if string.respond_to? :encoding # Change encoding in 1.9
|
87
|
+
@encoding = string.encoding.to_s
|
88
|
+
end
|
89
|
+
|
90
|
+
basic_set_content internal_string(string, @encoding)
|
91
|
+
end
|
92
|
+
|
93
|
+
attr_accessor :encoding
|
94
|
+
|
95
|
+
def inspect
|
96
|
+
"text(#{content.inspect})"
|
97
|
+
end
|
98
|
+
|
99
|
+
alias :to_s :content
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Ray
|
2
|
+
module TextHelper
|
3
|
+
module_function
|
4
|
+
def parse_style(style)
|
5
|
+
case style
|
6
|
+
when Integer
|
7
|
+
style
|
8
|
+
when Array
|
9
|
+
style.inject(0) do |last, e|
|
10
|
+
last | case e
|
11
|
+
when :normal then Font::STYLE_NORMAL
|
12
|
+
when :italic then Font::STYLE_ITALIC
|
13
|
+
when :bold then Font::STYLE_BOLD
|
14
|
+
when :underline then Font::STYLE_UNDERLINE
|
15
|
+
else
|
16
|
+
raise ArgumentError, "Unknown style #{e.inspect}"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
when nil
|
20
|
+
Font::STYLE_NORMAL
|
21
|
+
else
|
22
|
+
raise ArgumentError, "Can't convert #{style.class} into Integer"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def internal_string(string, enc)
|
27
|
+
# Hackish. Use to make the string null-terminated (a null character being,
|
28
|
+
# in UTF-32, 4 bytes set to 0)
|
29
|
+
null_character = "\0\0\0\0"
|
30
|
+
if null_character.respond_to? :force_encoding
|
31
|
+
null_character.force_encoding(INTERNAL_ENCODING)
|
32
|
+
end
|
33
|
+
|
34
|
+
Iconv.conv(INTERNAL_ENCODING, enc, string) + null_character
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/ray/turtle.rb
ADDED
@@ -0,0 +1,215 @@
|
|
1
|
+
module Ray
|
2
|
+
# Implementation of turtle graphics.
|
3
|
+
#
|
4
|
+
# A turtle keeps the following caracteristics:
|
5
|
+
# - An image which it is manipulating.
|
6
|
+
# - Its position (x, y), relative to the image. Set to (0, 0) by default.
|
7
|
+
# - An angle, in radians or in degrees (using degrees by default).
|
8
|
+
# Set to 0 by default, i.e. heading East.
|
9
|
+
# - Whether the turtle should draw when moved (defaults to true)
|
10
|
+
# - The color in which lines will be drawn. Defaults to white.
|
11
|
+
class Turtle
|
12
|
+
include Math
|
13
|
+
|
14
|
+
# @param [Ray::Image] img the image on which the turtle should be
|
15
|
+
# drawing on.
|
16
|
+
def initialize(img)
|
17
|
+
@img = img
|
18
|
+
@drawing = true
|
19
|
+
|
20
|
+
@x = @y = 0
|
21
|
+
@angle = 0 # in rad
|
22
|
+
|
23
|
+
@angle_unit = :deg
|
24
|
+
|
25
|
+
@pen_width = 1
|
26
|
+
|
27
|
+
@color = Ray::Color.white
|
28
|
+
end
|
29
|
+
|
30
|
+
# Stops drawing
|
31
|
+
def pen_up
|
32
|
+
@drawing = false
|
33
|
+
end
|
34
|
+
|
35
|
+
# Starts drawing
|
36
|
+
def pen_down
|
37
|
+
@drawing = true
|
38
|
+
end
|
39
|
+
|
40
|
+
# @return Whether the turtle is drawing
|
41
|
+
def drawing?
|
42
|
+
@drawing
|
43
|
+
end
|
44
|
+
|
45
|
+
alias :pen_down? :drawing?
|
46
|
+
|
47
|
+
# @return The opposite of #pen_down?
|
48
|
+
def pen_up?
|
49
|
+
!@drawing
|
50
|
+
end
|
51
|
+
|
52
|
+
# Moves the turtle in the direction it's facing.
|
53
|
+
# Draws a line if the pen is down.
|
54
|
+
#
|
55
|
+
# @param [Float] dist Length of the line
|
56
|
+
# @see #backward
|
57
|
+
def forward(dist)
|
58
|
+
x = @x + dist * cos(@angle)
|
59
|
+
|
60
|
+
# The y-axis is reversed
|
61
|
+
y = @y - dist * sin(@angle)
|
62
|
+
|
63
|
+
go_to(x, y)
|
64
|
+
end
|
65
|
+
|
66
|
+
# Same as forward(-dist)
|
67
|
+
# @see #forward
|
68
|
+
def backward(dist)
|
69
|
+
forward(-dist)
|
70
|
+
end
|
71
|
+
|
72
|
+
# Turns the turtle counter-clockwise.
|
73
|
+
# @param [Float] angle The angle, in degrees if the angle unit
|
74
|
+
# is set to degrees. In radians otherwise.
|
75
|
+
# @see #right
|
76
|
+
def left(angle)
|
77
|
+
self.angle += angle
|
78
|
+
end
|
79
|
+
|
80
|
+
# Turns the turtle clockwise.
|
81
|
+
# @param [Float] angle The angle, in degrees if the angle unit
|
82
|
+
# is set to degrees. In radians otherwise.
|
83
|
+
# @see #left
|
84
|
+
def right(angle)
|
85
|
+
self.angle -= angle
|
86
|
+
end
|
87
|
+
|
88
|
+
# Makes the turtle move to (x, y). Draws if the pen is down.
|
89
|
+
def go_to(x, y)
|
90
|
+
if pen_down?
|
91
|
+
@img.draw_line([@x, @y], [x, y], @color, @pen_width)
|
92
|
+
end
|
93
|
+
|
94
|
+
self.pos = [x, y]
|
95
|
+
end
|
96
|
+
|
97
|
+
# Makes the turtle go to the center. Draws if the pen is down.
|
98
|
+
def center
|
99
|
+
go_to(@img.w / 2, @img.h / 2)
|
100
|
+
end
|
101
|
+
|
102
|
+
# Resets the turtle's position, angle, and color but not
|
103
|
+
# the content of the image. Also sets the pen down.
|
104
|
+
# @see #clear
|
105
|
+
def reset
|
106
|
+
self.angle = 0
|
107
|
+
self.pos = [0, 0]
|
108
|
+
self.color = Ray::Color.white
|
109
|
+
self.pen_width = 1
|
110
|
+
|
111
|
+
pen_down
|
112
|
+
end
|
113
|
+
|
114
|
+
# Clears the content of the image and call reset.
|
115
|
+
# @see #reset
|
116
|
+
def clear
|
117
|
+
img.fill(Ray::Color.none)
|
118
|
+
reset
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns the distance between the turtle and a point.
|
122
|
+
# @see #dist_square
|
123
|
+
def dist(x, y)
|
124
|
+
sqrt(dist_square(x, y))
|
125
|
+
end
|
126
|
+
|
127
|
+
# Returns the square of the distance between the turtle and a point.
|
128
|
+
# Since this doesn't require to call sqrt, it is faster to use this
|
129
|
+
# when possible.
|
130
|
+
#
|
131
|
+
# @see #dist
|
132
|
+
def dist_square(x, y)
|
133
|
+
dx = (@x - x)
|
134
|
+
dy = (@y - y)
|
135
|
+
dx * dx + dy * dy
|
136
|
+
end
|
137
|
+
|
138
|
+
# The image the turtle is drawing on
|
139
|
+
attr_reader :img
|
140
|
+
|
141
|
+
# Position of the turtle
|
142
|
+
attr_accessor :x, :y
|
143
|
+
alias :set_x :x=
|
144
|
+
alias :set_y :y=
|
145
|
+
|
146
|
+
# @return [Ray::Vector2] The position of the turtle, as [x, y].
|
147
|
+
def pos
|
148
|
+
Vector2[@x, @y]
|
149
|
+
end
|
150
|
+
|
151
|
+
# Sets the position of the turtle.
|
152
|
+
# @example
|
153
|
+
# turtle.pos = [10, 20]
|
154
|
+
# turtle.pos = Ray::Vector2[10, 20]
|
155
|
+
def pos=(ary)
|
156
|
+
@x, @y = ary.to_a
|
157
|
+
end
|
158
|
+
|
159
|
+
alias :set_pos :pos=
|
160
|
+
|
161
|
+
# The unit used for angle. Either :deg (degrees) or :rad
|
162
|
+
# (radians)
|
163
|
+
attr_reader :angle_unit
|
164
|
+
|
165
|
+
# Sets the angle unit.
|
166
|
+
def angle_unit=(unit)
|
167
|
+
unless unit == :deg || unit == :rad
|
168
|
+
raise ArgumentError, "Unknown angle unit '#{unit}'"
|
169
|
+
end
|
170
|
+
|
171
|
+
@angle_unit = unit
|
172
|
+
end
|
173
|
+
|
174
|
+
# @return [Float] The angle, either in radians or in degrees,
|
175
|
+
# depending on angle_unit.
|
176
|
+
def angle
|
177
|
+
@angle_unit == :deg ? rad_to_deg(@angle) : @angle
|
178
|
+
end
|
179
|
+
|
180
|
+
# Sets the angle.
|
181
|
+
# @param [Float] val The angle, either in radians or in degrees,
|
182
|
+
# depending on angle_unit.
|
183
|
+
def angle=(val)
|
184
|
+
@angle = @angle_unit == :deg ? deg_to_rad(val) : val
|
185
|
+
end
|
186
|
+
|
187
|
+
alias :set_angle :angle=
|
188
|
+
|
189
|
+
# The color the turtle uses for drawing.
|
190
|
+
attr_accessor :color
|
191
|
+
alias :pen_color :color=
|
192
|
+
|
193
|
+
attr_accessor :pen_width
|
194
|
+
|
195
|
+
private
|
196
|
+
def deg_to_rad(deg)
|
197
|
+
(PI * deg) / 180
|
198
|
+
end
|
199
|
+
|
200
|
+
def rad_to_deg(rad)
|
201
|
+
(180 * rad) / PI
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
class Image
|
206
|
+
# Creates a turtle operating on the receiver.
|
207
|
+
# Instance evaluates the block it is given if any.
|
208
|
+
# @return [Ray::Turtle] The turtle it created.
|
209
|
+
def turtle(&block)
|
210
|
+
turtle = Ray::Turtle.new(self)
|
211
|
+
turtle.instance_eval(&block) if block
|
212
|
+
turtle
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
data/lib/ray/vector.rb
ADDED
@@ -0,0 +1,226 @@
|
|
1
|
+
module Ray
|
2
|
+
class Vector2
|
3
|
+
class << self
|
4
|
+
alias :[] :new
|
5
|
+
end
|
6
|
+
|
7
|
+
# @return [true, false] True if the receive is contained in the rect
|
8
|
+
def inside?(rect)
|
9
|
+
rect.to_rect.contain? self
|
10
|
+
end
|
11
|
+
|
12
|
+
# Opposite of #inside?
|
13
|
+
def outside?(rect)
|
14
|
+
!inside?(rect)
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [Ray::Vector2, #to_vector2] other
|
18
|
+
# @return [Ray::Vector2] (x + other.x, y + other.y)
|
19
|
+
def +(other)
|
20
|
+
other = other.to_vector2
|
21
|
+
Ray::Vector2[x + other.x, y + other.y]
|
22
|
+
end
|
23
|
+
|
24
|
+
# @param [Ray::Vector2, #to_vector2] other
|
25
|
+
# @return [Ray::Vector2] (x - other.x, y - other.y)
|
26
|
+
def -(other)
|
27
|
+
other = other.to_vector2
|
28
|
+
Ray::Vector2[x - other.x, y - other.y]
|
29
|
+
end
|
30
|
+
|
31
|
+
# @overload *(float)
|
32
|
+
# @param [Float] float
|
33
|
+
# @return [Ray::Vector2] (x * float, y * float)
|
34
|
+
#
|
35
|
+
# @overload *(vector)
|
36
|
+
# @param [Ray::Vector2, #to_vector2] vector
|
37
|
+
# @return [Ray::Vector2] (a.x * b.x, a.y * b.y)
|
38
|
+
def *(other)
|
39
|
+
if other.respond_to? :to_vector2
|
40
|
+
other = other.to_vector2
|
41
|
+
Ray::Vector2[x * other.x, y * other.y]
|
42
|
+
else
|
43
|
+
Ray::Vector2[x * other, y * other]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# @overload /(float)
|
48
|
+
# @param [Float] float
|
49
|
+
# @return [Ray::Vector2] (x / float, y / float)
|
50
|
+
#
|
51
|
+
# @overload /(vector)
|
52
|
+
# @param [Ray::Vector2, #to_vector2] vector
|
53
|
+
# @return [Ray::Vector2] (a.x / b.x, a.y / b.y)
|
54
|
+
def /(other)
|
55
|
+
if other.respond_to? :to_vector2
|
56
|
+
other = other.to_vector2
|
57
|
+
Ray::Vector2[x / other.x, y / other.y]
|
58
|
+
else
|
59
|
+
Ray::Vector2[x / other, y / other]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
# @param [Ray::Vector2, #to_vector2]
|
65
|
+
# @return [Float] a.x * b.x + a.y * b.y
|
66
|
+
def dot(vector)
|
67
|
+
vector = vector.to_vector2
|
68
|
+
x * vector.x + y * vector.y
|
69
|
+
end
|
70
|
+
|
71
|
+
# @return [Ray::Vector2] (-x, -y)
|
72
|
+
def -@; Ray::Vector2[-x, -y] end
|
73
|
+
|
74
|
+
# @return [Ray::Vector2] (x, y)
|
75
|
+
def +@; self; end
|
76
|
+
|
77
|
+
def ==(other)
|
78
|
+
if other.is_a? Vector2
|
79
|
+
x == other.x && y == other.y
|
80
|
+
elsif other.respond_to? :to_vector2
|
81
|
+
other = other.to_vector2
|
82
|
+
x == other.x && y == other.y
|
83
|
+
else
|
84
|
+
false
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def eql?(other)
|
89
|
+
self.class == other.class && self == other
|
90
|
+
end
|
91
|
+
|
92
|
+
def hash
|
93
|
+
to_a.hash
|
94
|
+
end
|
95
|
+
|
96
|
+
def to_a
|
97
|
+
[x, y]
|
98
|
+
end
|
99
|
+
|
100
|
+
def to_vector2
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
def to_s
|
105
|
+
"(%g, %g)" % [x, y]
|
106
|
+
end
|
107
|
+
|
108
|
+
alias :w :x
|
109
|
+
alias :h :y
|
110
|
+
|
111
|
+
alias :width :w
|
112
|
+
alias :height :h
|
113
|
+
|
114
|
+
alias :w= :x=
|
115
|
+
alias :h= :y=
|
116
|
+
|
117
|
+
alias :width= :x=
|
118
|
+
alias :height= :h=
|
119
|
+
end
|
120
|
+
|
121
|
+
class Vector3
|
122
|
+
class << self
|
123
|
+
alias :[] :new
|
124
|
+
end
|
125
|
+
|
126
|
+
# @param [Ray::Vector3, #to_vector3] other
|
127
|
+
# @return [Ray::Vector3] (x + other.x, y + other.y, z + other.z)
|
128
|
+
def +(other)
|
129
|
+
other = other.to_vector3
|
130
|
+
Ray::Vector3[x + other.x, y + other.y, z + other.z]
|
131
|
+
end
|
132
|
+
|
133
|
+
# @param [Ray::Vector3, #to_vector3] other
|
134
|
+
# @return [Ray::Vector3] (x - other.x, y - other.y, z - other.z)
|
135
|
+
def -(other)
|
136
|
+
other = other.to_vector3
|
137
|
+
Ray::Vector3[x - other.x, y - other.y, z - other.z]
|
138
|
+
end
|
139
|
+
|
140
|
+
# @overload *(float)
|
141
|
+
# @param [Float] float
|
142
|
+
# @return [Ray::Vector3] (x * float, y * float, z * float)
|
143
|
+
#
|
144
|
+
# @overload *(vector)
|
145
|
+
# @param [Ray::Vector3, #to_vector3] vector
|
146
|
+
# @return [Ray::Vector3] (a.x * b.x, a.y * b.y, a.z * b.z)
|
147
|
+
def *(other)
|
148
|
+
if other.respond_to? :to_vector3
|
149
|
+
other = other.to_vector3
|
150
|
+
Ray::Vector3[x * other.x, y * other.y, z * other.z]
|
151
|
+
else
|
152
|
+
Ray::Vector3[x * other, y * other, z * other]
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
# @overload /(float)
|
157
|
+
# @param [Float] float
|
158
|
+
# @return [Ray::Vector3] (x / float, y / float, z / float)
|
159
|
+
#
|
160
|
+
# @overload /(vector)
|
161
|
+
# @param [Ray::Vector3, #to_vector3] vector
|
162
|
+
# @return [Ray::Vector3] (a.x / b.x, a.y / b.y, a.z / b.z)
|
163
|
+
def /(other)
|
164
|
+
if other.respond_to? :to_vector3
|
165
|
+
other = other.to_vector3
|
166
|
+
Ray::Vector3[x / other.x, y / other.y, z / other.z]
|
167
|
+
else
|
168
|
+
Ray::Vector3[x / other, y / other, z / other]
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# @param [Ray::Vector3, #to_vector3] vector
|
173
|
+
# @return [Float] Dot product (i.e. x * vector.x + y * vector.y + z * vector.z)
|
174
|
+
def dot(vector)
|
175
|
+
vector = vector.to_vector3
|
176
|
+
x * vector.x + y * vector.y + z * vector.z
|
177
|
+
end
|
178
|
+
|
179
|
+
# @return [Ray::Vector3] (-x, -y, -z)
|
180
|
+
def -@; Ray::Vector3[-x, -y, -z] end
|
181
|
+
|
182
|
+
# @return [Ray::Vector3] (x, y, y)
|
183
|
+
def +@; self; end
|
184
|
+
|
185
|
+
def ==(other)
|
186
|
+
if other.is_a? Vector3
|
187
|
+
x == other.x && y == other.y && z == other.z
|
188
|
+
elsif other.respond_to? :to_vector3
|
189
|
+
other = other.to_vector3
|
190
|
+
x == other.x && y == other.y && z == other.z
|
191
|
+
else
|
192
|
+
false
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
def eql?(other)
|
197
|
+
self.class == other.class && self == other
|
198
|
+
end
|
199
|
+
|
200
|
+
def hash
|
201
|
+
to_a.hash
|
202
|
+
end
|
203
|
+
|
204
|
+
def to_s
|
205
|
+
"(%g, %g, %g)" % [x, y, z]
|
206
|
+
end
|
207
|
+
|
208
|
+
def to_a
|
209
|
+
[x, y, z]
|
210
|
+
end
|
211
|
+
|
212
|
+
def to_vector3
|
213
|
+
self
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
class Array
|
219
|
+
def to_vector2
|
220
|
+
Ray::Vector2[*self]
|
221
|
+
end
|
222
|
+
|
223
|
+
def to_vector3
|
224
|
+
Ray::Vector3[*self]
|
225
|
+
end
|
226
|
+
end
|