v2d 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('v2d', '0.0.1') do |p|
5
+ Echoe.new('v2d', '0.0.2') do |p|
6
6
  p.description = "2D Vector implemented as Ruby extension"
7
7
  p.url = "http://github.com/cypok/v2d"
8
8
  p.author = "Vladimir Parfinenko"
data/ext/v2d.c CHANGED
@@ -5,8 +5,13 @@ VALUE cV2D;
5
5
 
6
6
  ID ix, iy;
7
7
 
8
- /* DEFINITIONS */
8
+ /* DEFINITION */
9
9
 
10
+ typedef struct _V2D {
11
+ double x, y;
12
+ } V2D;
13
+
14
+ VALUE v2d_alloc(VALUE klass);
10
15
  /*VALUE v2d_initialize(VALUE self);*/
11
16
  VALUE v2d_initialize_with_x_and_y(VALUE self, VALUE x, VALUE y);
12
17
  VALUE v2d_square_brackets(VALUE klass, VALUE x, VALUE y);
@@ -24,6 +29,7 @@ VALUE v2d_equals(VALUE self, VALUE other);
24
29
  void Init_v2d() {
25
30
  cV2D = rb_define_class("V2D", rb_cObject);
26
31
 
32
+ rb_define_alloc_func(cV2D, v2d_alloc);
27
33
  /*rb_define_method(cV2D, "initialize", v2d_initialize, 0);*/
28
34
  rb_define_method(cV2D, "initialize", v2d_initialize_with_x_and_y, 2);
29
35
  rb_define_singleton_method(cV2D, "[]", v2d_square_brackets, 2);
@@ -44,13 +50,20 @@ void Init_v2d() {
44
50
 
45
51
  /* IMPLEMENTATION */
46
52
 
53
+ VALUE v2d_alloc(VALUE klass) {
54
+ V2D *v = (V2D *)xmalloc(sizeof(V2D));
55
+ return Data_Wrap_Struct(klass, NULL, xfree, v);
56
+ }
57
+
47
58
  /*VALUE v2d_initialize(VALUE self) {*/
48
59
  /*return self;*/
49
60
  /*}*/
50
61
 
51
62
  VALUE v2d_initialize_with_x_and_y(VALUE self, VALUE x, VALUE y) {
52
- rb_ivar_set(self, ix, x);
53
- rb_ivar_set(self, iy, y);
63
+ V2D *v;
64
+ Data_Get_Struct(self, V2D, v);
65
+ v->x = NUM2DBL(x);
66
+ v->y = NUM2DBL(y);
54
67
  return self;
55
68
  }
56
69
 
@@ -60,77 +73,84 @@ VALUE v2d_square_brackets(VALUE klass, VALUE x, VALUE y) {
60
73
  }
61
74
 
62
75
  VALUE v2d_get_x(VALUE self) {
63
- return rb_ivar_get(self, ix);
76
+ V2D *v;
77
+ Data_Get_Struct(self, V2D, v);
78
+ return rb_float_new(v->x);
64
79
  }
65
80
 
66
81
  VALUE v2d_get_y(VALUE self) {
67
- return rb_ivar_get(self, iy);
82
+ V2D *v;
83
+ Data_Get_Struct(self, V2D, v);
84
+ return rb_float_new(v->y);
68
85
  }
69
86
 
70
87
  VALUE v2d_set_x(VALUE self, VALUE x) {
71
- return rb_ivar_set(self, ix, x);
88
+ V2D *v;
89
+ Data_Get_Struct(self, V2D, v);
90
+ v->x = NUM2DBL(x);
91
+ return x;
72
92
  }
73
93
 
74
94
  VALUE v2d_set_y(VALUE self, VALUE y) {
75
- return rb_ivar_set(self, iy, y);
95
+ V2D *v;
96
+ Data_Get_Struct(self, V2D, v);
97
+ v->y = NUM2DBL(y);
98
+ return y;
76
99
  }
77
100
 
78
101
  VALUE v2d_add(VALUE self, VALUE other) {
79
- double left_x = NUM2DBL(rb_ivar_get(self, ix));
80
- double left_y = NUM2DBL(rb_ivar_get(self, iy));
81
- double right_x = NUM2DBL(rb_ivar_get(other, ix));
82
- double right_y = NUM2DBL(rb_ivar_get(other, iy));
83
- VALUE args[] = {
84
- rb_float_new(left_x + right_x),
85
- rb_float_new(left_y + right_y)
86
- };
87
- return rb_class_new_instance(2, args, cV2D);
102
+ V2D *v1, *v2;
103
+ Data_Get_Struct(self, V2D, v1);
104
+ Data_Get_Struct(other, V2D, v2);
105
+
106
+ V2D *res = (V2D*)xmalloc(sizeof(V2D));
107
+ res->x = v1->x + v2->x;
108
+ res->y = v1->y + v2->y;
109
+ return Data_Wrap_Struct(cV2D, NULL, xfree, res);
88
110
  }
89
111
 
90
112
  VALUE v2d_sub(VALUE self, VALUE other) {
91
- double left_x = NUM2DBL(rb_ivar_get(self, ix));
92
- double left_y = NUM2DBL(rb_ivar_get(self, iy));
93
- double right_x = NUM2DBL(rb_ivar_get(other, ix));
94
- double right_y = NUM2DBL(rb_ivar_get(other, iy));
95
- VALUE args[] = {
96
- rb_float_new(left_x - right_x),
97
- rb_float_new(left_y - right_y)
98
- };
99
- return rb_class_new_instance(2, args, cV2D);
113
+ V2D *v1, *v2;
114
+ Data_Get_Struct(self, V2D, v1);
115
+ Data_Get_Struct(other, V2D, v2);
116
+
117
+ V2D *res = (V2D*)xmalloc(sizeof(V2D));
118
+ res->x = v1->x - v2->x;
119
+ res->y = v1->y - v2->y;
120
+ return Data_Wrap_Struct(cV2D, NULL, xfree, res);
100
121
  }
101
122
 
102
123
  VALUE v2d_mul(VALUE self, VALUE multiplier) {
103
- double left_x = NUM2DBL(rb_ivar_get(self, ix));
104
- double left_y = NUM2DBL(rb_ivar_get(self, iy));
105
- double right = NUM2DBL(multiplier);
106
- VALUE args[] = {
107
- rb_float_new(left_x * right),
108
- rb_float_new(left_y * right)
109
- };
110
- return rb_class_new_instance(2, args, cV2D);
124
+ V2D *v;
125
+ Data_Get_Struct(self, V2D, v);
126
+ double m = NUM2DBL(multiplier);
127
+
128
+ V2D *res = (V2D*)xmalloc(sizeof(V2D));
129
+ res->x = v->x * m;
130
+ res->y = v->y * m;
131
+ return Data_Wrap_Struct(cV2D, NULL, xfree, res);
111
132
  }
112
133
 
113
134
  VALUE v2d_div(VALUE self, VALUE divisor) {
114
- double left_x = NUM2DBL(rb_ivar_get(self, ix));
115
- double left_y = NUM2DBL(rb_ivar_get(self, iy));
116
- double right = NUM2DBL(divisor);
117
- VALUE args[] = {
118
- rb_float_new(left_x / right),
119
- rb_float_new(left_y / right)
120
- };
121
- return rb_class_new_instance(2, args, cV2D);
135
+ V2D *v;
136
+ Data_Get_Struct(self, V2D, v);
137
+ double d = NUM2DBL(divisor);
138
+
139
+ V2D *res = (V2D*)xmalloc(sizeof(V2D));
140
+ res->x = v->x / d;
141
+ res->y = v->y / d;
142
+ return Data_Wrap_Struct(cV2D, NULL, xfree, res);
122
143
  }
123
144
 
124
145
  VALUE v2d_abs(VALUE self) {
125
- double x = NUM2DBL(rb_ivar_get(self, ix));
126
- double y = NUM2DBL(rb_ivar_get(self, iy));
127
- return rb_float_new(sqrt(x*x + y*y));
146
+ V2D *v;
147
+ Data_Get_Struct(self, V2D, v);
148
+ return rb_float_new(sqrt(v->x*v->x + v->y*v->y));
128
149
  }
129
150
 
130
151
  VALUE v2d_equals(VALUE self, VALUE other) {
131
- double left_x = NUM2DBL(rb_ivar_get(self, ix));
132
- double left_y = NUM2DBL(rb_ivar_get(self, iy));
133
- double right_x = NUM2DBL(rb_ivar_get(other, ix));
134
- double right_y = NUM2DBL(rb_ivar_get(other, iy));
135
- return (left_x == right_x && left_y == right_y) ? Qtrue : Qfalse;
152
+ V2D *v1, *v2;
153
+ Data_Get_Struct(self, V2D, v1);
154
+ Data_Get_Struct(other, V2D, v2);
155
+ return (v1->x == v2->x && v1->y == v2->y) ? Qtrue : Qfalse;
136
156
  }
data/spec/v2d_spec.rb CHANGED
@@ -3,12 +3,10 @@ require 'v2d'
3
3
  describe V2D do
4
4
  it "#new should create with arguments" do
5
5
  v = V2D.new(1,2)
6
- v.x.should == 1
7
6
  end
8
7
 
9
8
  it "#[] should create with arguments" do
10
9
  v = V2D[1, 2]
11
- v.x.should == 1
12
10
  end
13
11
 
14
12
  it "#x should return first coordinate" do
@@ -25,7 +23,7 @@ describe V2D do
25
23
  v.should == V2D[3, 2]
26
24
  end
27
25
 
28
- it "#y should set second coordinate" do
26
+ it "#y= should set second coordinate" do
29
27
  v = V2D[1, 2]
30
28
  v.y = 3
31
29
  v.should == V2D[1, 3]
@@ -59,6 +57,12 @@ describe V2D do
59
57
  it "should return new vector as quotient" do
60
58
  (@v1/2).should == V2D[0.5, 1]
61
59
  end
60
+
61
+ it "should return new vector as quotient even with zero divisor" do
62
+ r = @v1/0
63
+ r.x.should be_infinite
64
+ r.y.should be_infinite
65
+ end
62
66
  end
63
67
 
64
68
  describe "#abs" do
data/v2d.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{v2d}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Vladimir Parfinenko"]
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: v2d
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vladimir Parfinenko
metadata.gz.sig CHANGED
@@ -1 +1,3 @@
1
- K] ��"HYQݤ���0�[���\^�r9�d
1
+ ^J�owd��5*��GY��+����m�Y���h/�LR��r���f
2
+ ՟��J"�A�����i*�U��n�W����ʠN�.
3
+ -#�