v2d 0.0.2 → 0.1.0

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.
data.tar.gz.sig CHANGED
Binary file
data/Manifest CHANGED
@@ -1,6 +1,8 @@
1
- README
1
+ Manifest
2
+ README.rdoc
2
3
  Rakefile
3
4
  ext/extconf.rb
5
+ ext/init.c
4
6
  ext/v2d.c
7
+ ext/v2d.h
5
8
  spec/v2d_spec.rb
6
- Manifest
@@ -1,11 +1,8 @@
1
- Installation:
2
- ruby extconf.rb
3
- make
4
- make install
5
-
6
- Usage:
1
+ === Usage
2
+ Simply require
3
+ require 'rubygems'
7
4
  require 'v2d'
8
-
5
+ And use
9
6
  v1 = V2D[1.0, 2.0] # or v1 = V2D.new(1.0, 2.0)
10
7
  v2 = V2D[0, 0]
11
8
 
@@ -17,5 +14,8 @@ Usage:
17
14
  (v2 / 4) == V2D[2.75, 3]
18
15
  V2D[3, 4].abs == 5
19
16
 
20
- Tests:
17
+ === Tests
21
18
  spec v2d_spec.rb
19
+
20
+ === Author
21
+ Vladimir Parfinenko, write at vladimir[dot]parfinenko[at]gmail[dot]com
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.2') do |p|
5
+ Echoe.new('v2d', '0.1.0') 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"
@@ -0,0 +1,163 @@
1
+ #include <ruby.h>
2
+ #include "v2d.h"
3
+
4
+ VALUE cV2D;
5
+
6
+ ID ix, iy;
7
+
8
+ // DEFINITIONS
9
+
10
+ /* Allocate memory */
11
+ VALUE rb_alloc(VALUE klass);
12
+
13
+ /* Return new with initialized x and y */
14
+ VALUE rb_initialize_with_x_and_y(VALUE self, VALUE x, VALUE y);
15
+
16
+ /* Return <tt>V2D.new(x, y)</tt> */
17
+ VALUE rb_square_brackets(VALUE klass, VALUE x, VALUE y);
18
+
19
+ /* Return x */
20
+ VALUE rb_get_x(VALUE self);
21
+
22
+ /* Return y */
23
+ VALUE rb_get_y(VALUE self);
24
+
25
+ /* Set x */
26
+ VALUE rb_set_x(VALUE self, VALUE x);
27
+
28
+ /* Set y */
29
+ VALUE rb_set_y(VALUE self, VALUE y);
30
+
31
+ /* Return sum with +other+ */
32
+ VALUE rb_add(VALUE self, VALUE other);
33
+
34
+ /* Return diff with +other+ */
35
+ VALUE rb_sub(VALUE self, VALUE other);
36
+
37
+ /* Return product with +multiplier+ */
38
+ VALUE rb_mul(VALUE self, VALUE multiplier);
39
+
40
+ /* Return quotient with +divisor+ */
41
+ VALUE rb_div(VALUE self, VALUE divisor);
42
+
43
+ /* Return absolute value */
44
+ VALUE rb_abs(VALUE self);
45
+
46
+ /* Return whether equals to +other+ */
47
+ VALUE rb_equals(VALUE self, VALUE other);
48
+
49
+
50
+ void Init_v2d() {
51
+ /* 2D vector */
52
+ cV2D = rb_define_class("V2D", rb_cObject);
53
+
54
+ rb_define_alloc_func(cV2D, rb_alloc);
55
+ rb_define_method(cV2D, "initialize", rb_initialize_with_x_and_y, 2);
56
+ rb_define_singleton_method(cV2D, "[]", rb_square_brackets, 2);
57
+ rb_define_method(cV2D, "x", rb_get_x, 0);
58
+ rb_define_method(cV2D, "y", rb_get_y, 0);
59
+ rb_define_method(cV2D, "x=", rb_set_x, 1);
60
+ rb_define_method(cV2D, "y=", rb_set_y, 1);
61
+ rb_define_method(cV2D, "+", rb_add, 1);
62
+ rb_define_method(cV2D, "-", rb_sub, 1);
63
+ rb_define_method(cV2D, "*", rb_mul, 1);
64
+ rb_define_method(cV2D, "/", rb_div, 1);
65
+ rb_define_method(cV2D, "abs", rb_abs, 0);
66
+ rb_define_method(cV2D, "==", rb_equals, 1);
67
+
68
+ ix = rb_intern("@x");
69
+ iy = rb_intern("@y");
70
+ }
71
+
72
+ // IMPLEMENTATIONS
73
+
74
+ VALUE rb_alloc(VALUE klass) {
75
+ V2D *v = v2d_new();
76
+ return Data_Wrap_Struct(klass, NULL, v2d_delete, v);
77
+ }
78
+
79
+ VALUE rb_initialize_with_x_and_y(VALUE self, VALUE x, VALUE y) {
80
+ V2D *v;
81
+ Data_Get_Struct(self, V2D, v);
82
+ v->x = NUM2DBL(x);
83
+ v->y = NUM2DBL(y);
84
+ return self;
85
+ }
86
+
87
+ VALUE rb_square_brackets(VALUE klass, VALUE x, VALUE y) {
88
+ VALUE args[] = {x, y};
89
+ return rb_class_new_instance(2, args, cV2D);
90
+ }
91
+
92
+ VALUE rb_get_x(VALUE self) {
93
+ V2D *v;
94
+ Data_Get_Struct(self, V2D, v);
95
+ return rb_float_new(v->x);
96
+ }
97
+
98
+ VALUE rb_get_y(VALUE self) {
99
+ V2D *v;
100
+ Data_Get_Struct(self, V2D, v);
101
+ return rb_float_new(v->y);
102
+ }
103
+
104
+ VALUE rb_set_x(VALUE self, VALUE x) {
105
+ V2D *v;
106
+ Data_Get_Struct(self, V2D, v);
107
+ v->x = NUM2DBL(x);
108
+ return x;
109
+ }
110
+
111
+ VALUE rb_set_y(VALUE self, VALUE y) {
112
+ V2D *v;
113
+ Data_Get_Struct(self, V2D, v);
114
+ v->y = NUM2DBL(y);
115
+ return y;
116
+ }
117
+
118
+ VALUE rb_add(VALUE self, VALUE other) {
119
+ V2D *v1, *v2;
120
+ Data_Get_Struct(self, V2D, v1);
121
+ Data_Get_Struct(other, V2D, v2);
122
+ V2D *res = v2d_add(v1, v2);
123
+ return Data_Wrap_Struct(cV2D, NULL, xfree, res);
124
+ }
125
+
126
+ VALUE rb_sub(VALUE self, VALUE other) {
127
+ V2D *v1, *v2;
128
+ Data_Get_Struct(self, V2D, v1);
129
+ Data_Get_Struct(other, V2D, v2);
130
+ V2D *res = v2d_sub(v1, v2);
131
+ return Data_Wrap_Struct(cV2D, NULL, xfree, res);
132
+ }
133
+
134
+ VALUE rb_mul(VALUE self, VALUE multiplier) {
135
+ V2D *v;
136
+ Data_Get_Struct(self, V2D, v);
137
+ double m = NUM2DBL(multiplier);
138
+ V2D *res = v2d_mul(v, m);
139
+ return Data_Wrap_Struct(cV2D, NULL, xfree, res);
140
+ }
141
+
142
+ VALUE rb_div(VALUE self, VALUE divisor) {
143
+ V2D *v;
144
+ Data_Get_Struct(self, V2D, v);
145
+ double d = NUM2DBL(divisor);
146
+ V2D *res = v2d_div(v, d);
147
+ return Data_Wrap_Struct(cV2D, NULL, xfree, res);
148
+ }
149
+
150
+ VALUE rb_abs(VALUE self) {
151
+ V2D *v;
152
+ Data_Get_Struct(self, V2D, v);
153
+ double res = v2d_abs(v);
154
+ return rb_float_new(res);
155
+ }
156
+
157
+ VALUE rb_equals(VALUE self, VALUE other) {
158
+ V2D *v1, *v2;
159
+ Data_Get_Struct(self, V2D, v1);
160
+ Data_Get_Struct(other, V2D, v2);
161
+ return v2d_equals(v1, v2) ? Qtrue : Qfalse;
162
+ }
163
+
data/ext/v2d.c CHANGED
@@ -1,156 +1,76 @@
1
+ #include "v2d.h"
1
2
  #include <ruby.h>
2
3
  #include <math.h>
3
4
 
4
- VALUE cV2D;
5
-
6
- ID ix, iy;
7
-
8
- /* DEFINITION */
9
-
10
- typedef struct _V2D {
11
- double x, y;
12
- } V2D;
13
-
14
- VALUE v2d_alloc(VALUE klass);
15
- /*VALUE v2d_initialize(VALUE self);*/
16
- VALUE v2d_initialize_with_x_and_y(VALUE self, VALUE x, VALUE y);
17
- VALUE v2d_square_brackets(VALUE klass, VALUE x, VALUE y);
18
- VALUE v2d_get_x(VALUE self);
19
- VALUE v2d_get_y(VALUE self);
20
- VALUE v2d_set_x(VALUE self, VALUE x);
21
- VALUE v2d_set_y(VALUE self, VALUE y);
22
- VALUE v2d_add(VALUE self, VALUE other);
23
- VALUE v2d_sub(VALUE self, VALUE other);
24
- VALUE v2d_mul(VALUE self, VALUE multiplier);
25
- VALUE v2d_div(VALUE self, VALUE divisor);
26
- VALUE v2d_abs(VALUE self);
27
- VALUE v2d_equals(VALUE self, VALUE other);
28
-
29
- void Init_v2d() {
30
- cV2D = rb_define_class("V2D", rb_cObject);
31
-
32
- rb_define_alloc_func(cV2D, v2d_alloc);
33
- /*rb_define_method(cV2D, "initialize", v2d_initialize, 0);*/
34
- rb_define_method(cV2D, "initialize", v2d_initialize_with_x_and_y, 2);
35
- rb_define_singleton_method(cV2D, "[]", v2d_square_brackets, 2);
36
- rb_define_method(cV2D, "x", v2d_get_x, 0);
37
- rb_define_method(cV2D, "y", v2d_get_y, 0);
38
- rb_define_method(cV2D, "x=", v2d_set_x, 1);
39
- rb_define_method(cV2D, "y=", v2d_set_y, 1);
40
- rb_define_method(cV2D, "+", v2d_add, 1);
41
- rb_define_method(cV2D, "-", v2d_sub, 1);
42
- rb_define_method(cV2D, "*", v2d_mul, 1);
43
- rb_define_method(cV2D, "/", v2d_div, 1);
44
- rb_define_method(cV2D, "abs", v2d_abs, 0);
45
- rb_define_method(cV2D, "==", v2d_equals, 1);
46
-
47
- ix = rb_intern("@x");
48
- iy = rb_intern("@y");
49
- }
50
-
51
- /* IMPLEMENTATION */
5
+ #define DBL_EPSILON (1e-9)
52
6
 
53
- VALUE v2d_alloc(VALUE klass) {
54
- V2D *v = (V2D *)xmalloc(sizeof(V2D));
55
- return Data_Wrap_Struct(klass, NULL, xfree, v);
7
+ V2D * v2d_new() {
8
+ V2D *res = (V2D*)xmalloc(sizeof(V2D));
9
+ res->x = 0;
10
+ res->y = 0;
11
+ return res;
56
12
  }
57
13
 
58
- /*VALUE v2d_initialize(VALUE self) {*/
59
- /*return self;*/
60
- /*}*/
61
-
62
- VALUE v2d_initialize_with_x_and_y(VALUE self, VALUE x, VALUE y) {
63
- V2D *v;
64
- Data_Get_Struct(self, V2D, v);
65
- v->x = NUM2DBL(x);
66
- v->y = NUM2DBL(y);
67
- return self;
14
+ void v2d_delete(V2D * v) {
15
+ xfree(v);
68
16
  }
69
17
 
70
- VALUE v2d_square_brackets(VALUE klass, VALUE x, VALUE y) {
71
- VALUE args[] = {x, y};
72
- return rb_class_new_instance(2, args, cV2D);
18
+ V2D * v2d_clone(V2D * v) {
19
+ return v2d_assign(v2d_new(), v);
73
20
  }
74
21
 
75
- VALUE v2d_get_x(VALUE self) {
76
- V2D *v;
77
- Data_Get_Struct(self, V2D, v);
78
- return rb_float_new(v->x);
22
+ V2D * v2d_assign(V2D * dest, V2D * src) {
23
+ dest->x = src->x;
24
+ dest->y = src->y;
25
+ return dest;
79
26
  }
80
27
 
81
- VALUE v2d_get_y(VALUE self) {
82
- V2D *v;
83
- Data_Get_Struct(self, V2D, v);
84
- return rb_float_new(v->y);
28
+ V2D * v2d_add_to(V2D * v1, V2D * v2) {
29
+ v1->x += v2->x;
30
+ v1->y += v2->y;
31
+ return v1;
85
32
  }
86
33
 
87
- VALUE v2d_set_x(VALUE self, VALUE x) {
88
- V2D *v;
89
- Data_Get_Struct(self, V2D, v);
90
- v->x = NUM2DBL(x);
91
- return x;
34
+ V2D * v2d_add(V2D * v1, V2D * v2) {
35
+ return v2d_add_to(v2d_clone(v1), v2);
92
36
  }
93
37
 
94
- VALUE v2d_set_y(VALUE self, VALUE y) {
95
- V2D *v;
96
- Data_Get_Struct(self, V2D, v);
97
- v->y = NUM2DBL(y);
98
- return y;
38
+ V2D * v2d_sub_from(V2D * v1, V2D * v2) {
39
+ v1->x -= v2->x;
40
+ v1->y -= v2->y;
41
+ return v1;
99
42
  }
100
43
 
101
- VALUE v2d_add(VALUE self, VALUE other) {
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);
44
+ V2D * v2d_sub(V2D * v1, V2D * v2) {
45
+ return v2d_sub_from(v2d_clone(v1), v2);
110
46
  }
111
47
 
112
- VALUE v2d_sub(VALUE self, VALUE other) {
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);
48
+ V2D * v2d_mul_by(V2D * v, double multiplier) {
49
+ v->x *= multiplier;
50
+ v->y *= multiplier;
51
+ return v;
121
52
  }
122
53
 
123
- VALUE v2d_mul(VALUE self, VALUE multiplier) {
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);
54
+ V2D * v2d_mul(V2D * v, double multiplier) {
55
+ return v2d_mul_by(v2d_clone(v), multiplier);
132
56
  }
133
57
 
134
- VALUE v2d_div(VALUE self, VALUE divisor) {
135
- V2D *v;
136
- Data_Get_Struct(self, V2D, v);
137
- double d = NUM2DBL(divisor);
58
+ V2D * v2d_div_by(V2D * v, double divisor) {
59
+ v->x /= divisor;
60
+ v->y /= divisor;
61
+ return v;
62
+ }
138
63
 
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);
64
+ V2D * v2d_div(V2D * v, double divisor) {
65
+ return v2d_div_by(v2d_clone(v), divisor);
143
66
  }
144
67
 
145
- VALUE v2d_abs(VALUE self) {
146
- V2D *v;
147
- Data_Get_Struct(self, V2D, v);
148
- return rb_float_new(sqrt(v->x*v->x + v->y*v->y));
68
+ double v2d_abs(V2D * v) {
69
+ return sqrt(v->x*v->x + v->y*v->y);
149
70
  }
150
71
 
151
- VALUE v2d_equals(VALUE self, VALUE other) {
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;
72
+ int v2d_equals(V2D * v1, V2D * v2) {
73
+ return fabs(v1->x - v2->x) < DBL_EPSILON &&
74
+ fabs(v1->y - v2->y) < DBL_EPSILON;
156
75
  }
76
+
@@ -0,0 +1,48 @@
1
+ #pragma once
2
+
3
+ typedef struct _V2D {
4
+ double x, y;
5
+ } V2D;
6
+
7
+ /* Allocates new zero-vector */
8
+ V2D * v2d_new();
9
+
10
+ /* Delete vector */
11
+ void v2d_delete(V2D * v);
12
+
13
+ /* Allocates new vector equals to +v+ */
14
+ V2D * v2d_clone(V2D * v);
15
+
16
+ /* Assigns +src+ to +dest+ */
17
+ V2D * v2d_assign(V2D * dest, V2D * src);
18
+
19
+ /* Add +v2+ to +v1+ and return +v1+ */
20
+ V2D * v2d_add_to(V2D * v1, V2D * v2);
21
+
22
+ /* Return sum of +v1+ and +v2+ */
23
+ V2D * v2d_add(V2D * v1, V2D * v2);
24
+
25
+ /* Sub +v2+ from +v1+ and return +v1+ */
26
+ V2D * v2d_sub_from(V2D * v1, V2D * v2);
27
+
28
+ /* Return diff of +v1+ and +v2+ */
29
+ V2D * v2d_sub(V2D * v1, V2D * v2);
30
+
31
+ /* Mul +v+ by +multiplier+ and return +v+ */
32
+ V2D * v2d_mul_by(V2D * v, double multiplier);
33
+
34
+ /* Return product of +v1+ and +multiplier+ */
35
+ V2D * v2d_mul(V2D * v, double multiplier);
36
+
37
+ /* Div +v+ by +multiplier+ and return +v+ */
38
+ V2D * v2d_div_by(V2D * v, double divisor);
39
+
40
+ /* Return quotient of +v1+ and +divisor+ */
41
+ V2D * v2d_div(V2D * v, double divisor);
42
+
43
+ /* Return absolute value of +v+ */
44
+ double v2d_abs(V2D * v);
45
+
46
+ /* Return whether +v1+ equals to +v2+ */
47
+ int v2d_equals(V2D * v1, V2D * v2);
48
+
@@ -2,20 +2,20 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{v2d}
5
- s.version = "0.0.2"
5
+ s.version = "0.1.0"
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"]
9
9
  s.cert_chain = ["/Users/cypok/gem-public_cert.pem"]
10
- s.date = %q{2010-09-15}
10
+ s.date = %q{2010-09-17}
11
11
  s.description = %q{2D Vector implemented as Ruby extension}
12
12
  s.email = %q{vladimir.parfinenko@gmail.com}
13
13
  s.extensions = ["ext/extconf.rb"]
14
- s.extra_rdoc_files = ["README", "ext/extconf.rb", "ext/v2d.c"]
15
- s.files = ["README", "Rakefile", "ext/extconf.rb", "ext/v2d.c", "spec/v2d_spec.rb", "Manifest", "v2d.gemspec"]
14
+ s.extra_rdoc_files = ["README.rdoc", "ext/extconf.rb", "ext/init.c", "ext/v2d.c", "ext/v2d.h"]
15
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "ext/extconf.rb", "ext/init.c", "ext/v2d.c", "ext/v2d.h", "spec/v2d_spec.rb", "v2d.gemspec"]
16
16
  s.homepage = %q{http://github.com/cypok/v2d}
17
- s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "V2d", "--main", "README"]
18
- s.require_paths = ["ext"]
17
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "V2d", "--main", "README.rdoc"]
18
+ s.require_paths = ["lib", "ext"]
19
19
  s.rubyforge_project = %q{v2d}
20
20
  s.rubygems_version = %q{1.3.7}
21
21
  s.signing_key = %q{/Users/cypok/gem-private_key.pem}
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 2
10
- version: 0.0.2
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Vladimir Parfinenko
@@ -36,7 +36,7 @@ cert_chain:
36
36
  78F0qvtLjR0DAnN6uYs98PR+jHE+0ckLX3D9sw==
37
37
  -----END CERTIFICATE-----
38
38
 
39
- date: 2010-09-15 00:00:00 +07:00
39
+ date: 2010-09-17 00:00:00 +07:00
40
40
  default_executable:
41
41
  dependencies: []
42
42
 
@@ -47,16 +47,20 @@ executables: []
47
47
  extensions:
48
48
  - ext/extconf.rb
49
49
  extra_rdoc_files:
50
- - README
50
+ - README.rdoc
51
51
  - ext/extconf.rb
52
+ - ext/init.c
52
53
  - ext/v2d.c
54
+ - ext/v2d.h
53
55
  files:
54
- - README
56
+ - Manifest
57
+ - README.rdoc
55
58
  - Rakefile
56
59
  - ext/extconf.rb
60
+ - ext/init.c
57
61
  - ext/v2d.c
62
+ - ext/v2d.h
58
63
  - spec/v2d_spec.rb
59
- - Manifest
60
64
  - v2d.gemspec
61
65
  has_rdoc: true
62
66
  homepage: http://github.com/cypok/v2d
@@ -69,8 +73,9 @@ rdoc_options:
69
73
  - --title
70
74
  - V2d
71
75
  - --main
72
- - README
76
+ - README.rdoc
73
77
  require_paths:
78
+ - lib
74
79
  - ext
75
80
  required_ruby_version: !ruby/object:Gem::Requirement
76
81
  none: false
metadata.gz.sig CHANGED
Binary file