beziercurve 0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/beziercurve.rb +105 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 74aa0dc585f1edb095064ba09657e27275be7cfc
4
+ data.tar.gz: 2723fda7a6e4b4a23c94c701da3b9760bac2e847
5
+ SHA512:
6
+ metadata.gz: 14cd524785cb7c3ba5c6cef5dfb5a8caf91f1e4d4e809e3358f29c1b7f2470374d433a44a0fbc7cdd959056c90acbe8902db5ccab9cc67185e181dc66520ee1b
7
+ data.tar.gz: 080d20f696ff4c346d4d5200d7abbc56a5bd3fa5d177aa526279b0911e4cd755121d3bb6f1b37dcd3e3520df2682a7af3fa4432a7c290638e24a49f647727df0
@@ -0,0 +1,105 @@
1
+ module Bezier
2
+
3
+ class ControlPoint
4
+ # maybe replace the accessors and initialization with Struct
5
+ attr_accessor :x, :y
6
+ def initialize(x,y)
7
+ @x = x
8
+ @y = y
9
+ end
10
+ def - (b)
11
+ self.class.new(self.x - b.x, self.y - b.y)
12
+ end
13
+ def + (b)
14
+ self.class.new(self.x + b.x, self.y + b.y)
15
+ end
16
+ def inspect
17
+ return @x, @y
18
+ end
19
+ def to_curve
20
+ CurvePoint.new(self.x, self.y)
21
+ end
22
+ end
23
+ class CurvePoint < ControlPoint # minimal type safety, but both class have the same functionality
24
+ def to_control
25
+ ControlPoint.new(self.x, self.y)
26
+ end
27
+ end
28
+ class Curve
29
+ attr_accessor :hullpoints
30
+
31
+ def initialize(*hullpoints)
32
+ # need at least 3 control points
33
+ if hullpoints.length < 3 then
34
+ raise "Cannot create Bézier curve with less than 3 control points"
35
+ end
36
+
37
+ # check for rogue value types
38
+ if hullpoints.find {|p| p.class != ControlPoint} == nil
39
+ @hullpoints = hullpoints
40
+ end
41
+ end
42
+
43
+ def add(point)
44
+ if point.class == ControlPoint
45
+ @hullpoints << point
46
+ else
47
+ raise TypeError, "Point should be type of ControlPoint"
48
+ end
49
+ end
50
+
51
+ def point_on_curve(t)
52
+
53
+ def point_on_hull(point1, point2, t) # making this local
54
+ if (point1.class != ControlPoint) or (point2.class != ControlPoint)
55
+ raise TypeError, "Both points should be type of ControlPoint"
56
+ end
57
+ new_x = (point1.x - point2.x) * t
58
+ new_y = (point1.y - point2.y) * t
59
+ return ControlPoint.new(new_x, new_y)
60
+ end
61
+
62
+ # imperatively ugly but works, refactor later. point_on_curve and point_on_hull should be one method
63
+ ary = @hullpoints
64
+ return ary if ary.length <= 1 # zero or one element as argument, return unmodified
65
+
66
+ while ary.length > 1
67
+ temp = []
68
+ 0.upto(ary.length-2) do |index|
69
+ memoize1 = point_on_hull(ary[index], ary[index+1], t)
70
+ temp << ary[index+0] - memoize1
71
+ end
72
+ ary = temp
73
+ end
74
+ return temp[0].to_curve
75
+ end
76
+
77
+ def display_points # just a helper, for quickly put CotrolPOints to STDOUT in a gnuplottable format
78
+ @hullpoints.map{|point| puts "#{point.x} #{point.y}"}
79
+ end
80
+
81
+ def enumerated(start_t, delta_t)
82
+ Enumerator.new do |yielder|
83
+ point_position = start_t.to_f
84
+ number = point_on_curve(point_position)
85
+ loop do
86
+ yielder.yield(number)
87
+ point_position += delta_t.to_f
88
+ raise StopIteration if point_position > 1
89
+ number = point_on_curve(point_position)
90
+ end
91
+ end
92
+ end
93
+
94
+ def order
95
+ @hullpoints.length
96
+ end
97
+ end
98
+ end
99
+
100
+ __END__
101
+
102
+ bezier = Bezier::Curve.new(Bezier::ControlPoint.new(40,250), Bezier::ControlPoint.new(35,100), Bezier::ControlPoint.new(150,70), Bezier::ControlPoint.new(210,120)) # cubic curve, 4 coordinates
103
+
104
+ puts bezier.hullpoints[0].x
105
+ #puts "#{bezier.point_on_curve(0.013).x} #{bezier.point_on_curve(0.013).y}"
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: beziercurve
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Földes László
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Creates a Bézier curve by its control points. Implemented with de Casteljau
14
+ method.
15
+ email: foldes.laszlo2@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/beziercurve.rb
21
+ homepage: http://devrandom.postr.hu
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.1.10
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Create Bnd analyze ézier curves
45
+ test_files: []
46
+ has_rdoc: