rubysketch 0.3.4 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e9914f7b869e2dfeb80a8ba58a8c6678c0a995a974124b2b88974e477af3fdd8
4
- data.tar.gz: 2b7d45e918f60be716bc9edc8f5bfbb7ceb800954965af32163431e681709250
3
+ metadata.gz: 5a3690207d8b2a8c6bfe3b556e9e1267e3508bcd757952fc738ce2594844a6a5
4
+ data.tar.gz: '0112280fc01e89633adff2ebbccc012ceab1bdcd9271dc9d6f14d8c9969ca586'
5
5
  SHA512:
6
- metadata.gz: 5f1192007cfb3a80c6335dd8a881358a9fa2b9dac8b6a0931836f295f2658ac0d4723b9a9d51800e6923ccfcc625f6fd92fd9444732339a338773681d6921299
7
- data.tar.gz: afefa27c322e08e0be3c1c6099b3ef3a3c86d0fadfc3140e4ec4c26c4686573d1a733ce0791f94241a4e73a67cda5998029fcfd4ebc6e7f3a1de6c8994ecdb0e
6
+ metadata.gz: bad81197d43586840b8f6828dd3f267a131017796d988bb958969b4fcc3cae7bcf41eca4123ce9f38039dab69195859cd3bb96a8afe1eda650a3abacd92a6944
7
+ data.tar.gz: de7e3c7bb04cdf8ac3f90893277e20b671073ebe03197c9a31987cdb5324e7e0ce07659064bce787115713c95471bdacec4323a29ce0a2bf22ad8d2b7cff6937
@@ -0,0 +1,62 @@
1
+ name: Release
2
+
3
+ on:
4
+ push:
5
+ tags: ['v[0-9]*']
6
+
7
+ jobs:
8
+ release:
9
+ runs-on: macos-latest
10
+
11
+ steps:
12
+ - name: checkout
13
+ uses: actions/checkout@v2
14
+
15
+ - name: ruby 2.6
16
+ uses: actions/setup-ruby@v1
17
+ with:
18
+ ruby-version: 2.6.x
19
+
20
+ - name: install gems
21
+ run: |
22
+ gem install bundler
23
+ bundle install --jobs 4 --retry 3
24
+
25
+ - name: version
26
+ id: version
27
+ run: |
28
+ echo ::set-output name=value::$(ruby -e 'print "${{ github.ref }}"[/\/v([\w\.\-]+)/, 1]')
29
+
30
+ - name: create gem
31
+ run: rake clean gem
32
+
33
+ - name: create release
34
+ id: create_release
35
+ uses: actions/create-release@v1
36
+ env:
37
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
38
+ with:
39
+ tag_name: ${{ github.ref }}
40
+ release_name: ${{ github.ref }}
41
+ draft: false
42
+ prerelease: false
43
+
44
+ - name: upload to releases
45
+ uses: actions/upload-release-asset@v1
46
+ env:
47
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
48
+ with:
49
+ upload_url: ${{ steps.create_release.outputs.upload_url }}
50
+ asset_path: ./rubysketch-${{ steps.version.outputs.value }}.gem
51
+ asset_name: rubysketch-${{ steps.version.outputs.value }}.gem
52
+ asset_content_type: application/zip
53
+
54
+ - name: upload to rubygems
55
+ env:
56
+ GEM_HOST_API_KEY: ${{ secrets.RUBYGEMS_AUTH_TOKEN }}
57
+ run: |
58
+ mkdir -p $HOME/.gem
59
+ touch $HOME/.gem/credentials
60
+ chmod 0600 $HOME/.gem/credentials
61
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
62
+ rake upload
@@ -0,0 +1,27 @@
1
+ name: Test
2
+
3
+ on:
4
+ push:
5
+ branches: [master]
6
+ pull_request:
7
+ branches: [master]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: macos-latest
12
+
13
+ steps:
14
+ - uses: actions/checkout@v2
15
+
16
+ - name: ruby 2.6
17
+ uses: actions/setup-ruby@v1
18
+ with:
19
+ ruby-version: 2.6.x
20
+
21
+ - name: install gems
22
+ run: |
23
+ gem install bundler
24
+ bundle install --jobs 4 --retry 3
25
+
26
+ - name: test
27
+ run: rake test
@@ -1,6 +1,35 @@
1
1
  # RubySketch ChangeLog
2
2
 
3
3
 
4
+ ## [0.3.8] - 2020-11-27
5
+
6
+ - Capture#initialize() can take requestWidth, requestHeight and cameraName
7
+ - add Capture#width and Capture#height
8
+
9
+
10
+ ## [0.3.7] - 2020-11-18
11
+
12
+ - add Capture class
13
+ - add log(), exp(), sqrt() and PI
14
+ - add examples/camera.rb
15
+ - add examples/breakout.rb
16
+ - fix error on calling image()
17
+
18
+
19
+ ## [0.3.6] - 2020-08-02
20
+
21
+ - random() can take array or nothing
22
+ - use github actions to release gem package
23
+
24
+
25
+ ## [0.3.5] - 2020-08-02
26
+
27
+ - add random()
28
+ - add sin(), cos(), tan(), asin(), acos(), atan() and atan2()
29
+ - make Vector class accessible from user script context
30
+ - fix error on calling rotate()
31
+
32
+
4
33
  ## [0.3.4] - 2020-08-02
5
34
 
6
35
  - delete Utility module
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'yard'
4
+ gem 'reflexion'
data/Rakefile CHANGED
@@ -23,3 +23,28 @@ generate_documents
23
23
  build_ruby_gem
24
24
 
25
25
  task :default => :test
26
+
27
+
28
+ namespace :version do
29
+
30
+ namespace :bump do
31
+
32
+ task :major do
33
+ update_and_tag_version 0
34
+ end
35
+
36
+ task :minor do
37
+ update_and_tag_version 1
38
+ end
39
+
40
+ task :patch do
41
+ update_and_tag_version 2
42
+ end
43
+
44
+ task :build do
45
+ update_and_tag_version 3
46
+ end
47
+
48
+ end# bump
49
+
50
+ end# version
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.4
1
+ 0.3.9
@@ -0,0 +1,212 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+
8
+ PADDING = 50
9
+ BRICK_COUNT = 10
10
+
11
+ $objs = []
12
+ $bar = nil
13
+ $gameover = false
14
+
15
+ setup do
16
+ addWalls
17
+ addBricks
18
+
19
+ colorMode HSB, 360, 100, 100
20
+ ellipseMode CORNER
21
+ background 0
22
+ noStroke
23
+ fill 0, 0, 100
24
+ end
25
+
26
+ draw do
27
+ background 0
28
+ $objs.each do |o|
29
+ o.update
30
+ o.draw
31
+ end
32
+ drawTexts
33
+ end
34
+
35
+ mousePressed do
36
+ start unless started?
37
+ end
38
+
39
+ mouseDragged do
40
+ $bar.pos.x = mouseX - $bar.w / 2 if $bar
41
+ end
42
+
43
+ def start()
44
+ $bar = Bar.new
45
+ $objs += [$bar, Ball.new($bar)]
46
+ end
47
+
48
+ def started?()
49
+ $bar
50
+ end
51
+
52
+ def gameover?()
53
+ started? &&
54
+ $objs.count {|o| o.kind_of? Ball} == 0
55
+ end
56
+
57
+ def cleared?()
58
+ started? && !gameover? &&
59
+ $objs.count {|o| o.kind_of? Brick} == 0
60
+ end
61
+
62
+ def addWalls()
63
+ left = Obj.new 0, 0, 10, height
64
+ top = Obj.new 0, 0, width, 10
65
+ right = Obj.new width - 10, 0, 10, height
66
+ bottom = Bottom.new 0, height - 10, width, 10
67
+ $objs += [top, bottom, left, right]
68
+ end
69
+
70
+ def addBricks()
71
+ brickW = (width - PADDING * 2) / BRICK_COUNT
72
+ 5.times do |y|
73
+ BRICK_COUNT.times do |x|
74
+ xx = PADDING + brickW * x
75
+ yy = PADDING + 30 * y
76
+ $objs.push Brick.new(xx, yy, brickW - 5, 20, y * 60)
77
+ end
78
+ end
79
+ end
80
+
81
+ def drawTexts()
82
+ push do
83
+ textAlign CENTER, CENTER
84
+
85
+ if !started?
86
+ fill 50, 100, 100
87
+ textSize 50
88
+ text "BREAKOUT", 0, 0, width, height
89
+ textSize 20
90
+ translate 0, 100
91
+ text "Tap to start!", 0, 0, width, height
92
+ elsif cleared?
93
+ fill 100, 80, 100
94
+ textSize 50
95
+ text "CLEAR!", 0, 0, width, height
96
+ elsif gameover?
97
+ fill 0, 20, 100
98
+ textSize 50
99
+ text "GAMEOVER", 0, 0, width, height
100
+ end
101
+ end
102
+ end
103
+
104
+ class Obj
105
+ attr_reader :pos, :w, :h, :vel
106
+
107
+ def initialize(x, y, w, h, vx = 0, vy = 0)
108
+ @pos = createVector x, y
109
+ @w, @h = w, h
110
+ @vel = createVector vx, vy
111
+ end
112
+
113
+ def update()
114
+ @pos.add @vel
115
+ end
116
+
117
+ def draw()
118
+ rect @pos.x, @pos.y, @w, @h
119
+ end
120
+
121
+ def bounds()
122
+ x, y = @pos.x, @pos.y
123
+ return x, y, x + @w, y + @h
124
+ end
125
+
126
+ def center()
127
+ createVector @pos.x + @w / 2, @pos.y + @h / 2
128
+ end
129
+ end
130
+
131
+ class Ball < Obj
132
+ def initialize(bar)
133
+ super bar.pos.x, bar.pos.y - bar.h, 20, 20
134
+ self.vel = createVector random(-1, 1), -1
135
+ end
136
+
137
+ def vel=(v)
138
+ @vel = v.dup.normalize.mult 8
139
+ end
140
+
141
+ def update()
142
+ b = bounds.dup
143
+ super
144
+ checkHit b
145
+ end
146
+
147
+ def checkHit(prevBounds)
148
+ x1, y1, x2, y2 = prevBounds
149
+ hitH = hitV = false
150
+ hits = []
151
+ $objs.each do |o|
152
+ next if o == self
153
+ next unless intersect? o
154
+ hits.push o
155
+ ox1, oy1, ox2, oy2 = o.bounds
156
+ hitH ||= !overlap?(x1, x2, ox1, ox2)
157
+ hitV ||= !overlap?(y1, y2, oy1, oy2)
158
+ end
159
+ vel.x *= -1 if hitH
160
+ vel.y *= -1 if hitV
161
+
162
+ hits.each {|o| hit o}
163
+ end
164
+
165
+ def intersect?(o)
166
+ x1, y1, x2, y2 = bounds
167
+ ox1, oy1, ox2, oy2 = o.bounds
168
+ overlap?(x1, x2, ox1, ox2) && overlap?(y1, y2, oy1, oy2)
169
+ end
170
+
171
+ def overlap?(a1, a2, b1, b2)
172
+ a1 <= b2 && b1 <= a2
173
+ end
174
+
175
+ def hit(o)
176
+ case o
177
+ when Bar then self.vel = center.sub o.center
178
+ when Brick then $objs.delete o
179
+ when Bottom then $objs.delete self unless cleared?
180
+ end
181
+ end
182
+ end
183
+
184
+ class Bar < Obj
185
+ def initialize()
186
+ w = 100
187
+ super (width - w) / 2, height - 50, w, 20
188
+ end
189
+ end
190
+
191
+ class Brick < Obj
192
+ def initialize(x, y, w, h, hue)
193
+ super x, y, w, h
194
+ @hue = hue
195
+ end
196
+
197
+ def draw()
198
+ push do
199
+ fill @hue, 50, 100
200
+ super
201
+ end
202
+ end
203
+ end
204
+
205
+ class Bottom < Obj
206
+ def draw()
207
+ push do
208
+ fill 0, 0, 50
209
+ super
210
+ end
211
+ end
212
+ end
@@ -0,0 +1,14 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+
8
+ cam = Capture.new 300, 300
9
+ cam.start
10
+
11
+ draw do
12
+ background 0
13
+ image cam, 0, 0
14
+ end
@@ -0,0 +1,33 @@
1
+ %w[xot rays reflex rubysketch]
2
+ .map {|s| File.expand_path "../../#{s}/lib", __dir__}
3
+ .each {|s| $:.unshift s if !$:.include?(s) && File.directory?(s)}
4
+
5
+ require 'rubysketch-processing'
6
+
7
+
8
+ w, h = width, height
9
+
10
+ cam = Capture.new w, h, Capture.list.last
11
+ cam.start
12
+
13
+ images = 60.times.map {
14
+ Graphics.new w, h
15
+ }
16
+
17
+ draw do
18
+ if frameCount % 2 == 0
19
+ images.unshift images.pop
20
+ images.first.tap do |image|
21
+ image.beginDraw {
22
+ image.image cam, 0, 0
23
+ }
24
+ end
25
+ end
26
+
27
+ background 0
28
+ segment_h= h / images.size
29
+ images.each.with_index do |image, i|
30
+ y = i * segment_h
31
+ copy image, 0, y, w, segment_h, 0, y, w, segment_h
32
+ end
33
+ end
@@ -2,10 +2,18 @@ require 'rubysketch'
2
2
 
3
3
 
4
4
  begin
5
- include RubySketch::Processing::Context
5
+ window = RubySketch::Window.new {start}
6
+ context = RubySketch::Processing::Context.new window
6
7
 
7
- window = RubySketch::Window.new {start}
8
- setup__ window
8
+ (context.methods - Object.instance_methods).each do |method|
9
+ define_method method do |*args, &block|
10
+ context.__send__ method, *args, &block
11
+ end
12
+ end
13
+
14
+ context.class.constants.each do |const|
15
+ self.class.const_set const, context.class.const_get(const)
16
+ end
9
17
 
10
18
  window.__send__ :begin_draw
11
19
  at_exit do
@@ -728,25 +728,127 @@ module RubySketch
728
728
  end# Touch
729
729
 
730
730
 
731
+ # Camera object.
732
+ #
733
+ class Capture
734
+
735
+ # Returns a list of available camera device names
736
+ #
737
+ # @return [Array] device name list
738
+ #
739
+ def self.list ()
740
+ Rays::Camera.device_names
741
+ end
742
+
743
+ # Initialize camera object.
744
+ #
745
+ # @overload Capture.new()
746
+ # @overload Capture.new(cameraName)
747
+ # @overload Capture.new(requestWidth, requestHeight)
748
+ # @overload Capture.new(requestWidth, requestHeight, cameraName)
749
+ #
750
+ # @param requestWidth [Integer] captured image width
751
+ # @param requestHeight [Integer] captured image height
752
+ # @param cameraName [String] camera device name
753
+ #
754
+ def initialize (*args)
755
+ width, height, name =
756
+ if args.empty?
757
+ [-1, -1, nil]
758
+ elsif args[0].kind_of?(String)
759
+ [-1, -1, args[0]]
760
+ elsif args[0].kind_of?(Numeric) && args[1].kind_of?(Numeric)
761
+ [args[0], args[1], args[2]]
762
+ else
763
+ raise ArgumentError
764
+ end
765
+ @camera = Rays::Camera.new width, height, device_name: name
766
+ end
767
+
768
+ # Start capturing.
769
+ #
770
+ # @return [nil] nil
771
+ #
772
+ def start ()
773
+ raise "Failed to start capture" unless @camera.start
774
+ nil
775
+ end
776
+
777
+ # Stop capturing.
778
+ #
779
+ # @return [nil] nil
780
+ #
781
+ def stop ()
782
+ @camera.stop
783
+ nil
784
+ end
785
+
786
+ # Returns is the next captured image available?
787
+ #
788
+ # @return [Boolean] true means object has next frame
789
+ #
790
+ def available ()
791
+ @camera.active?
792
+ end
793
+
794
+ # Reads next frame image
795
+ #
796
+ def read ()
797
+ @camera.image
798
+ end
799
+
800
+ # Returns the width of captured image
801
+ #
802
+ # @return [Numeric] the width of captured image
803
+ #
804
+ def width ()
805
+ @camera.image&.width || 0
806
+ end
807
+
808
+ # Returns the height of captured image
809
+ #
810
+ # @return [Numeric] the height of captured image
811
+ #
812
+ def height ()
813
+ @camera.image&.height || 0
814
+ end
815
+
816
+ # @private
817
+ def getInternal__ ()
818
+ @camera.image || dummyImage__
819
+ end
820
+
821
+ # @private
822
+ private def dummyImage__ ()
823
+ @dummy ||= Rays::Image.new 1, 1
824
+ end
825
+
826
+ end# Capture
827
+
828
+
731
829
  # Drawing context
732
830
  #
733
831
  module GraphicsContext
734
832
 
833
+ # PI
834
+ #
835
+ PI = Math::PI
836
+
735
837
  # PI / 2
736
838
  #
737
- HALF_PI = Math::PI / 2
839
+ HALF_PI = PI / 2
738
840
 
739
841
  # PI / 4
740
842
  #
741
- QUARTER_PI = Math::PI / 4
843
+ QUARTER_PI = PI / 4
742
844
 
743
845
  # PI * 2
744
846
  #
745
- TWO_PI = Math::PI * 2
847
+ TWO_PI = PI * 2
746
848
 
747
849
  # PI * 2
748
850
  #
749
- TAU = Math::PI * 2
851
+ TAU = PI * 2
750
852
 
751
853
  # RGB mode for colorMode().
752
854
  #
@@ -815,7 +917,7 @@ module RubySketch
815
917
  @painter__ = painter
816
918
  @painter__.miter_limit = 10
817
919
 
818
- @drawing = false
920
+ @drawing__ = false
819
921
  @hsbColor__ = false
820
922
  @colorMaxes__ = [1.0] * 4
821
923
  @angleScale__ = 1.0
@@ -838,14 +940,16 @@ module RubySketch
838
940
  stroke 0
839
941
  end
840
942
 
841
- def beginDraw ()
943
+ # @private
944
+ def beginDraw__ ()
842
945
  @matrixStack__.clear
843
946
  @styleStack__.clear
844
- @drawing = true
947
+ @drawing__ = true
845
948
  end
846
949
 
847
- def endDraw ()
848
- @drawing = false
950
+ # @private
951
+ def endDraw__ ()
952
+ @drawing__ = false
849
953
  end
850
954
 
851
955
  def width ()
@@ -930,7 +1034,7 @@ module RubySketch
930
1034
  end
931
1035
 
932
1036
  # @private
933
- protected def toAngle__ (angle)
1037
+ def toAngle__ (angle)
934
1038
  angle * @angleScale__
935
1039
  end
936
1040
 
@@ -1409,8 +1513,9 @@ module RubySketch
1409
1513
  #
1410
1514
  def image (img, a, b, c = nil, d = nil)
1411
1515
  assertDrawing__
1412
- x, y, w, h = toXYWH__ @imageMode__, a, b, c || img.width, d || img.height
1413
- @painter__.image img.getInternal__, x, y, w, h
1516
+ i = img.getInternal__
1517
+ x, y, w, h = toXYWH__ @imageMode__, a, b, c || i.width, d || i.height
1518
+ @painter__.image i, x, y, w, h
1414
1519
  nil
1415
1520
  end
1416
1521
 
@@ -1586,13 +1691,13 @@ module RubySketch
1586
1691
  end
1587
1692
 
1588
1693
  # @private
1589
- private def getInternal__ ()
1694
+ def getInternal__ ()
1590
1695
  @image__
1591
1696
  end
1592
1697
 
1593
1698
  # @private
1594
1699
  private def assertDrawing__ ()
1595
- raise "call beginDraw() before drawing" unless @drawing
1700
+ raise "call beginDraw() before drawing" unless @drawing__
1596
1701
  end
1597
1702
 
1598
1703
  end# GraphicsContext
@@ -1604,20 +1709,30 @@ module RubySketch
1604
1709
 
1605
1710
  include GraphicsContext
1606
1711
 
1712
+ # Initialize graphics object.
1713
+ #
1607
1714
  def initialize (width, height)
1608
1715
  @image__ = Rays::Image.new width, height
1609
1716
  setup__ @image__.painter
1610
1717
  end
1611
1718
 
1612
- def beginDraw ()
1719
+ # Start drawing.
1720
+ #
1721
+ def beginDraw (&block)
1613
1722
  @painter__.__send__ :begin_paint
1614
- super
1723
+ beginDraw__
1615
1724
  push
1725
+ if block
1726
+ block.call
1727
+ endDraw
1728
+ end
1616
1729
  end
1617
1730
 
1731
+ # End drawing.
1732
+ #
1618
1733
  def endDraw ()
1619
1734
  pop
1620
- super
1735
+ endDraw__
1621
1736
  @painter__.__send__ :end_paint
1622
1737
  end
1623
1738
 
@@ -1626,9 +1741,13 @@ module RubySketch
1626
1741
 
1627
1742
  # Processing context
1628
1743
  #
1629
- module Context
1744
+ class Context
1745
+
1746
+ include GraphicsContext
1630
1747
 
1631
- include GraphicsContext, Math
1748
+ Vector = Processing::Vector
1749
+ Capture = Processing::Capture
1750
+ Graphics = Processing::Graphics
1632
1751
 
1633
1752
  @@context__ = nil
1634
1753
 
@@ -1637,12 +1756,12 @@ module RubySketch
1637
1756
  end
1638
1757
 
1639
1758
  # @private
1640
- def setup__ (window)
1759
+ def initialize (window)
1641
1760
  @@context__ = self
1642
1761
 
1643
1762
  @window__ = window
1644
1763
  @image__ = @window__.canvas
1645
- super @window__.canvas_painter.paint {background 0.8}
1764
+ setup__ @window__.canvas_painter.paint {background 0.8}
1646
1765
 
1647
1766
  @loop__ = true
1648
1767
  @redraw__ = false
@@ -1652,8 +1771,8 @@ module RubySketch
1652
1771
  @mousePressed__ = false
1653
1772
  @touches__ = []
1654
1773
 
1655
- @window__.before_draw = proc {beginDraw}
1656
- @window__.after_draw = proc {endDraw}
1774
+ @window__.before_draw = proc {beginDraw__}
1775
+ @window__.after_draw = proc {endDraw__}
1657
1776
 
1658
1777
  drawFrame = -> {
1659
1778
  @image__ = @window__.canvas
@@ -1864,29 +1983,9 @@ module RubySketch
1864
1983
  end
1865
1984
 
1866
1985
  #
1867
- # Utility functions
1986
+ # Utilities
1868
1987
  #
1869
1988
 
1870
- # Converts degree to radian.
1871
- #
1872
- # @param degree [Numeric] degree to convert
1873
- #
1874
- # @return [Numeric] radian
1875
- #
1876
- def radians (degree)
1877
- degree * DEG2RAD__
1878
- end
1879
-
1880
- # Converts radian to degree.
1881
- #
1882
- # @param radian [Numeric] radian to convert
1883
- #
1884
- # @return [Numeric] degree
1885
- #
1886
- def degrees (radian)
1887
- radian * RAD2DEG__
1888
- end
1889
-
1890
1989
  # Returns the absolute number of the value.
1891
1990
  #
1892
1991
  # @param value [Numeric] number
@@ -1927,6 +2026,26 @@ module RubySketch
1927
2026
  value.round
1928
2027
  end
1929
2028
 
2029
+ # Returns the natural logarithm (the base-e logarithm) of a number.
2030
+ #
2031
+ # @param value [Numeric] number (> 0.0)
2032
+ #
2033
+ # @return [Numeric] result number
2034
+ #
2035
+ def log (n)
2036
+ Math.log n
2037
+ end
2038
+
2039
+ # Returns Euler's number e raised to the power of value.
2040
+ #
2041
+ # @param value [Numeric] number
2042
+ #
2043
+ # @return [Numeric] result number
2044
+ #
2045
+ def exp (n)
2046
+ Math.exp n
2047
+ end
2048
+
1930
2049
  # Returns value raised to the power of exponent.
1931
2050
  #
1932
2051
  # @param value [Numeric] base number
@@ -1948,6 +2067,16 @@ module RubySketch
1948
2067
  value * value
1949
2068
  end
1950
2069
 
2070
+ # Returns squared value.
2071
+ #
2072
+ # @param value [Numeric] number
2073
+ #
2074
+ # @return [Numeric] squared value
2075
+ #
2076
+ def sqrt (value)
2077
+ Math.sqrt value
2078
+ end
2079
+
1951
2080
  # Returns the magnitude (or length) of a vector.
1952
2081
  #
1953
2082
  # @overload mag(x, y)
@@ -2080,6 +2209,97 @@ module RubySketch
2080
2209
  value < min ? min : (value > max ? max : value)
2081
2210
  end
2082
2211
 
2212
+ # Converts degree to radian.
2213
+ #
2214
+ # @param degree [Numeric] degree to convert
2215
+ #
2216
+ # @return [Numeric] radian
2217
+ #
2218
+ def radians (degree)
2219
+ degree * DEG2RAD__
2220
+ end
2221
+
2222
+ # Converts radian to degree.
2223
+ #
2224
+ # @param radian [Numeric] radian to convert
2225
+ #
2226
+ # @return [Numeric] degree
2227
+ #
2228
+ def degrees (radian)
2229
+ radian * RAD2DEG__
2230
+ end
2231
+
2232
+ # Returns the sine of an angle.
2233
+ #
2234
+ # @param angle [Numeric] angle in radians
2235
+ #
2236
+ # @return [Numeric] the sine
2237
+ #
2238
+ def sin (angle)
2239
+ Math.sin angle
2240
+ end
2241
+
2242
+ # Returns the cosine of an angle.
2243
+ #
2244
+ # @param angle [Numeric] angle in radians
2245
+ #
2246
+ # @return [Numeric] the cosine
2247
+ #
2248
+ def cos (angle)
2249
+ Math.cos angle
2250
+ end
2251
+
2252
+ # Returns the ratio of the sine and cosine of an angle.
2253
+ #
2254
+ # @param angle [Numeric] angle in radians
2255
+ #
2256
+ # @return [Numeric] the tangent
2257
+ #
2258
+ def tan (angle)
2259
+ Math.tan angle
2260
+ end
2261
+
2262
+ # Returns the inverse of sin().
2263
+ #
2264
+ # @param value [Numeric] value for calculation
2265
+ #
2266
+ # @return [Numeric] the arc sine
2267
+ #
2268
+ def asin (value)
2269
+ Math.asin value
2270
+ end
2271
+
2272
+ # Returns the inverse of cos().
2273
+ #
2274
+ # @param value [Numeric] value for calculation
2275
+ #
2276
+ # @return [Numeric] the arc cosine
2277
+ #
2278
+ def acos (value)
2279
+ Math.acos value
2280
+ end
2281
+
2282
+ # Returns the inverse of tan().
2283
+ #
2284
+ # @param value [Numeric] value for valculation
2285
+ #
2286
+ # @return [Numeric] the arc tangent
2287
+ #
2288
+ def atan (value)
2289
+ Math.atan value
2290
+ end
2291
+
2292
+ # Returns the angle from a specified point.
2293
+ #
2294
+ # @param y [Numeric] y of the point
2295
+ # @param x [Numeric] x of the point
2296
+ #
2297
+ # @return [Numeric] the angle in radians
2298
+ #
2299
+ def atan2 (y, x)
2300
+ Math.atan2 y, x
2301
+ end
2302
+
2083
2303
  # Returns the perlin noise value.
2084
2304
  #
2085
2305
  # @overload noise(x)
@@ -2096,6 +2316,25 @@ module RubySketch
2096
2316
  Rays.perlin(x, y, z) / 2.0 + 0.5
2097
2317
  end
2098
2318
 
2319
+ # Returns a random number in range low...high
2320
+ #
2321
+ # @overload random()
2322
+ # @overload random(high)
2323
+ # @overload random(low, high)
2324
+ # @overload random(choices)
2325
+ #
2326
+ # @param low [Numeric] lower limit
2327
+ # @param high [Numeric] upper limit
2328
+ # @param choices [Array] array to choose from
2329
+ #
2330
+ # @return [Float] random number
2331
+ #
2332
+ def random (*args)
2333
+ return args.first.sample if args.first.kind_of? Array
2334
+ high, low = args.reverse
2335
+ rand (low || 0).to_f...(high || 1).to_f
2336
+ end
2337
+
2099
2338
  # Creates a new vector.
2100
2339
  #
2101
2340
  # @overload createVector()
@@ -2112,6 +2351,14 @@ module RubySketch
2112
2351
  Vector.new *args
2113
2352
  end
2114
2353
 
2354
+ # Creates a camera object as a video input device.
2355
+ #
2356
+ # @return [Capture] camera object
2357
+ #
2358
+ def createCapture (*args)
2359
+ Capture.new *args
2360
+ end
2361
+
2115
2362
  # Creates a new off-screen graphics context object.
2116
2363
  #
2117
2364
  # @param width [Numeric] width of graphics image
@@ -28,17 +28,9 @@ Gem::Specification.new do |s|
28
28
  s.platform = Gem::Platform::RUBY
29
29
  s.required_ruby_version = '~> 2'
30
30
 
31
- s.add_runtime_dependency 'yard'
32
- s.add_runtime_dependency 'xot', '~> 0.1'
33
- s.add_runtime_dependency 'beeps', '~> 0.1'
34
- s.add_runtime_dependency 'rucy', '~> 0.1'
35
- s.add_runtime_dependency 'rays', '~> 0.1'
36
31
  s.add_runtime_dependency 'reflexion', '~> 0.1'
37
32
 
38
33
  s.files = `git ls-files`.split $/
39
34
  s.test_files = s.files.grep %r{^(test|spec|features)/}
40
35
  s.extra_rdoc_files = rdocs.to_a
41
- s.has_rdoc = true
42
-
43
- s.extensions << 'Rakefile'
44
36
  end
@@ -12,11 +12,6 @@ require 'rubysketch'
12
12
  include Xot::Test
13
13
 
14
14
 
15
- unless $RAYS_NOAUTOINIT
16
- #def Rays.fin! () end
17
- end
18
-
19
-
20
15
  def assert_equal_vector (v1, v2, delta = 0.000001)
21
16
  assert_in_delta v1.x, v2.x, delta
22
17
  assert_in_delta v1.y, v2.y, delta
@@ -0,0 +1,38 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+
4
+ require_relative '../helper'
5
+
6
+
7
+ class TestProcessingUtility < Test::Unit::TestCase
8
+
9
+ class Context
10
+ include RubySketch::Processing::Context
11
+ end
12
+
13
+ def context
14
+ Context.new
15
+ end
16
+
17
+ def test_random ()
18
+ c = context
19
+
20
+ assert_equal Float, c.random(1).class
21
+ assert_equal Float, c.random(1.0).class
22
+ assert_equal Symbol, c.random((:a..:z).to_a).class
23
+
24
+ assert_not_equal c.random, c.random
25
+
26
+ 10000.times do
27
+ n = c.random
28
+ assert 0 <= n && n < 1
29
+
30
+ n = c.random 1
31
+ assert 0 <= n && n < 1
32
+
33
+ n = c.random 1, 2
34
+ assert 1.0 <= n && n < 2.0
35
+ end
36
+ end
37
+
38
+ end# TestProcessingUtility
metadata CHANGED
@@ -1,85 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubysketch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.4
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - xordog
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-02 00:00:00.000000000 Z
11
+ date: 2020-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: yard
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
- - !ruby/object:Gem::Dependency
28
- name: xot
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '0.1'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '0.1'
41
- - !ruby/object:Gem::Dependency
42
- name: beeps
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '0.1'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '0.1'
55
- - !ruby/object:Gem::Dependency
56
- name: rucy
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '0.1'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '0.1'
69
- - !ruby/object:Gem::Dependency
70
- name: rays
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '0.1'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '0.1'
83
13
  - !ruby/object:Gem::Dependency
84
14
  name: reflexion
85
15
  requirement: !ruby/object:Gem::Requirement
@@ -97,18 +27,23 @@ dependencies:
97
27
  description: Creative Coding Framework have API compatible to Processing API or p5.js.
98
28
  email: xordog@gmail.com
99
29
  executables: []
100
- extensions:
101
- - Rakefile
30
+ extensions: []
102
31
  extra_rdoc_files: []
103
32
  files:
33
+ - ".github/workflows/release.yml"
34
+ - ".github/workflows/test.yml"
104
35
  - ".gitignore"
105
36
  - ".yardopts"
106
37
  - ChangeLog.md
38
+ - Gemfile
107
39
  - LICENSE
108
40
  - README.md
109
41
  - Rakefile
110
42
  - VERSION
43
+ - examples/breakout.rb
44
+ - examples/camera.rb
111
45
  - examples/clock.rb
46
+ - examples/delay_camera.rb
112
47
  - examples/glsl.rb
113
48
  - examples/hello.rb
114
49
  - examples/image.rb
@@ -121,11 +56,12 @@ files:
121
56
  - lib/rubysketch/window.rb
122
57
  - rubysketch.gemspec
123
58
  - test/helper.rb
59
+ - test/processing/test_utility.rb
124
60
  - test/processing/test_vector.rb
125
61
  homepage: https://github.com/xord/rubysketch
126
62
  licenses: []
127
63
  metadata: {}
128
- post_install_message:
64
+ post_install_message:
129
65
  rdoc_options: []
130
66
  require_paths:
131
67
  - lib
@@ -141,9 +77,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
77
  version: '0'
142
78
  requirements: []
143
79
  rubygems_version: 3.0.3
144
- signing_key:
80
+ signing_key:
145
81
  specification_version: 4
146
82
  summary: Processing like Creative Coding Framework.
147
83
  test_files:
148
84
  - test/helper.rb
85
+ - test/processing/test_utility.rb
149
86
  - test/processing/test_vector.rb