shapeslib 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/shapelib.rb +133 -0
- metadata +45 -0
data/lib/shapelib.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
class Square
|
2
|
+
|
3
|
+
attr_reader :area, :perimiter, :diagonal
|
4
|
+
|
5
|
+
def initialize(side)
|
6
|
+
@side = side
|
7
|
+
@area = side ** 2
|
8
|
+
@perimiter = side * 4
|
9
|
+
@diagonal = Math.sqrt(side ** 2 + side ** 2)
|
10
|
+
end
|
11
|
+
|
12
|
+
def side=(val)
|
13
|
+
@side = val
|
14
|
+
@area = @side ** 2
|
15
|
+
@perimiter = @side * 4
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"Square: #{@side}"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
class Rectangle
|
25
|
+
|
26
|
+
attr_reader :area, :perimiter, :diagonal
|
27
|
+
|
28
|
+
def initialize(side_one, side_two)
|
29
|
+
@side_one, @side_two = side_one, side_two
|
30
|
+
@area = side_one * side_two
|
31
|
+
@perimiter = (side_one * 2) + (side_two * 2)
|
32
|
+
@diagonal = Math.sqrt(side_one ** 2 + side_two ** 2)
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def recalc
|
37
|
+
@area = @side_one * @side_two
|
38
|
+
@perimiter = (@side_one * 2) + (@side_two * 2)
|
39
|
+
@diagonal = Math.sqrt(@side_one ** 2 + @side_two ** 2)
|
40
|
+
end
|
41
|
+
|
42
|
+
public
|
43
|
+
def side_one=(val)
|
44
|
+
@side_one = val
|
45
|
+
recalc
|
46
|
+
end
|
47
|
+
|
48
|
+
def side_two=(val)
|
49
|
+
@side_two = val
|
50
|
+
recalc
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_s
|
54
|
+
"Rectangle: #{@side_one}-#{@side_two}"
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
class Triangle
|
61
|
+
|
62
|
+
attr_reader :side_one, :side_two, :side_three, :perimiter, :semiperimiter, :area
|
63
|
+
|
64
|
+
def initialize(side_one, side_two, side_three)
|
65
|
+
@side_one, @side_two, @side_three = side_one, side_two, side_three
|
66
|
+
@perimiter = side_one + side_two + side_three
|
67
|
+
@semiperimiter = @perimiter/2.0
|
68
|
+
@area = Math.sqrt(((side_one + side_two + side_three) * ((side_one * -1) + side_two + side_three) * (side_one - side_two + side_three) * (side_one + side_two - side_three)) / 16.0)
|
69
|
+
end
|
70
|
+
|
71
|
+
private
|
72
|
+
def recalc
|
73
|
+
@perimiter = @side_one + @side_two + @side_three
|
74
|
+
@semiperimiter = @perimiter/2.0
|
75
|
+
@area = Math.sqrt(((@side_one + @side_two + @side_three) * ((@side_one * -1) + @side_two + @side_three) * (@side_one - @side_two + @side_three) * (@side_one + @side_two - @side_three)) / 16.0)
|
76
|
+
end
|
77
|
+
|
78
|
+
public
|
79
|
+
def side_one=(val)
|
80
|
+
@side_one = val
|
81
|
+
recalc
|
82
|
+
end
|
83
|
+
|
84
|
+
def side_two=(val)
|
85
|
+
@side_two = val
|
86
|
+
recalc
|
87
|
+
end
|
88
|
+
|
89
|
+
def side_three=(val)
|
90
|
+
@side_three = val
|
91
|
+
recalc
|
92
|
+
end
|
93
|
+
|
94
|
+
def to_s
|
95
|
+
"Triangle: #{@side_one}-#{@side_two}-#{@side_three}"
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
class Circle
|
102
|
+
|
103
|
+
attr_reader :radius, :diameter, :circumference, :area
|
104
|
+
|
105
|
+
def initialize(radius)
|
106
|
+
@PI = Math::PI
|
107
|
+
@radius = radius
|
108
|
+
@diameter = radius * 2
|
109
|
+
@circumference = @diameter * @PI
|
110
|
+
@area = @PI * radius ** 2
|
111
|
+
@anti_area = @diameter ** 2 - @area
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
def recalc
|
116
|
+
@diameter = @radius * 2
|
117
|
+
@circumference = @diameter * @PI
|
118
|
+
@area = @PI * @radius ** 2
|
119
|
+
@anti_area = @diameter ** 2 - @area
|
120
|
+
end
|
121
|
+
|
122
|
+
public
|
123
|
+
def radius=(val)
|
124
|
+
@radius = val
|
125
|
+
recalc
|
126
|
+
end
|
127
|
+
|
128
|
+
def to_s
|
129
|
+
"Circle: #{@radius}"
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shapeslib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Solomon Wise
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-20 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A library for manipulating and calculating shapes.
|
15
|
+
email: slmnwise@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/shapelib.rb
|
21
|
+
homepage: http://gist.github.com/2759860
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.24
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: Shape Library
|
45
|
+
test_files: []
|