v2d 0.0.1 → 0.0.2
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/Rakefile +1 -1
- data/ext/v2d.c +69 -49
- data/spec/v2d_spec.rb +7 -3
- data/v2d.gemspec +1 -1
- data.tar.gz.sig +0 -0
- metadata +3 -3
- metadata.gz.sig +3 -1
data/Rakefile
CHANGED
data/ext/v2d.c
CHANGED
@@ -5,8 +5,13 @@ VALUE cV2D;
|
|
5
5
|
|
6
6
|
ID ix, iy;
|
7
7
|
|
8
|
-
/*
|
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
|
-
|
53
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
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
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
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
|
-
|
104
|
-
|
105
|
-
double
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
return
|
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
|
-
|
115
|
-
|
116
|
-
double
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
return
|
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
|
-
|
126
|
-
|
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
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
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
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:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Vladimir Parfinenko
|
metadata.gz.sig
CHANGED