1
+ //! Types and contants for handling areas.
2
+
1
3
use super :: measurement:: * ;
2
4
use super :: length;
3
5
6
+ /// Number of acres in a square meter
4
7
const SQUARE_METER_ACRE_FACTOR : f64 = 1.0 / 4046.86 ;
5
8
6
9
/// The `Area` struct can be used to deal with areas in a common way.
@@ -21,205 +24,244 @@ pub struct Area {
21
24
}
22
25
23
26
impl Area {
24
- // Inputs, metric, with both spellings of meter/metre
25
-
27
+ /// Create new area from floating point value in Square Meters
26
28
pub fn from_square_meters ( square_meters : f64 ) -> Self {
27
29
Area { square_meters : square_meters }
28
30
}
29
31
32
+ /// Create new area from floating point value in Square Metres
30
33
pub fn from_square_metres ( square_metres : f64 ) -> Self {
31
34
Self :: from_square_meters ( square_metres)
32
35
}
33
36
37
+ /// Create new area from floating point value in Square Nanometers
34
38
pub fn from_square_nanometers ( square_nanometers : f64 ) -> Self {
35
39
Self :: from_square_meters (
36
40
square_nanometers / ( length:: METER_NANOMETER_FACTOR * length:: METER_NANOMETER_FACTOR ) ,
37
41
)
38
42
}
39
43
44
+ /// Create new area from floating point value in Square Nanometres
40
45
pub fn from_square_nanometres ( square_nanometres : f64 ) -> Self {
41
46
Self :: from_square_nanometers ( square_nanometres)
42
47
}
43
48
49
+ /// Create new area from floating point value in Square Micrometers
44
50
pub fn from_square_micrometers ( square_micrometers : f64 ) -> Self {
45
51
Self :: from_square_meters (
46
52
square_micrometers / ( length:: METER_MICROMETER_FACTOR * length:: METER_MICROMETER_FACTOR ) ,
47
53
)
48
54
}
49
55
56
+ /// Create new area from floating point value in Square Micrometres
50
57
pub fn from_square_micrometres ( square_micrometres : f64 ) -> Self {
51
58
Self :: from_square_micrometers ( square_micrometres)
52
59
}
53
60
61
+ /// Create new area from floating point value in Square Millimeters
54
62
pub fn from_square_millimeters ( square_millimeters : f64 ) -> Self {
55
63
Self :: from_square_meters (
56
64
square_millimeters / ( length:: METER_MILLIMETER_FACTOR * length:: METER_MILLIMETER_FACTOR ) ,
57
65
)
58
66
}
59
67
68
+ /// Create new area from floating point value in Square Millimetres
60
69
pub fn from_square_millimetres ( square_millimetres : f64 ) -> Self {
61
70
Self :: from_square_millimeters ( square_millimetres)
62
71
}
63
72
73
+ /// Create new area from floating point value in Square Centimeters
64
74
pub fn from_square_centimeters ( square_centimeters : f64 ) -> Self {
65
75
Self :: from_square_meters (
66
76
square_centimeters / ( length:: METER_CENTIMETER_FACTOR * length:: METER_CENTIMETER_FACTOR ) ,
67
77
)
68
78
}
69
79
80
+ /// Create new area from floating point value in Square Centimetres
70
81
pub fn from_square_centimetres ( square_centimetres : f64 ) -> Self {
71
82
Self :: from_square_centimeters ( square_centimetres)
72
83
}
73
84
85
+ /// Create new area from floating point value in Square Decimeters
74
86
pub fn from_square_decimeters ( square_decimeters : f64 ) -> Self {
75
87
Self :: from_square_meters (
76
88
square_decimeters / ( length:: METER_DECIMETER_FACTOR * length:: METER_DECIMETER_FACTOR ) ,
77
89
)
78
90
}
79
91
92
+ /// Create new area from floating point value in Square Decimetres
80
93
pub fn from_square_decimetres ( square_decimetres : f64 ) -> Self {
81
94
Self :: from_square_decimeters ( square_decimetres)
82
95
}
83
96
97
+ /// Create new area from floating point value in Square Hectometers
84
98
pub fn from_square_hectometers ( square_hectometers : f64 ) -> Self {
85
99
Self :: from_square_meters (
86
100
square_hectometers / ( length:: METER_HECTOMETER_FACTOR * length:: METER_HECTOMETER_FACTOR ) ,
87
101
)
88
102
}
89
103
104
+ /// Create new area from floating point value in Square Hectometres
90
105
pub fn from_square_hectometres ( square_hectometres : f64 ) -> Self {
91
106
Self :: from_square_hectometers ( square_hectometres)
92
107
}
93
108
109
+ /// Create new area from floating point value in Hectares
94
110
pub fn from_hectares ( hectares : f64 ) -> Self {
95
111
Self :: from_square_hectometers ( hectares)
96
112
}
97
113
114
+ /// Create new area from floating point value in Square Kilometers
98
115
pub fn from_square_kilometers ( square_kilometers : f64 ) -> Self {
99
116
Self :: from_square_meters (
100
117
square_kilometers / ( length:: METER_KILOMETER_FACTOR * length:: METER_KILOMETER_FACTOR ) ,
101
118
)
102
119
}
103
120
121
+ /// Create new area from floating point value in Square Kilometres
104
122
pub fn from_square_kilometres ( square_kilometres : f64 ) -> Self {
105
123
Self :: from_square_kilometers ( square_kilometres)
106
124
}
107
125
108
- // Inputs, imperial
126
+ /// Create new area from floating point value in Square Inches
109
127
pub fn from_square_inches ( square_inches : f64 ) -> Self {
110
128
Self :: from_square_meters (
111
129
square_inches / ( length:: METER_INCH_FACTOR * length:: METER_INCH_FACTOR ) ,
112
130
)
113
131
}
114
132
133
+ /// Create new area from floating point value in Square Feet
115
134
pub fn from_square_feet ( square_feet : f64 ) -> Self {
116
135
Self :: from_square_meters (
117
136
square_feet / ( length:: METER_FEET_FACTOR * length:: METER_FEET_FACTOR ) ,
118
137
)
119
138
}
120
139
140
+ /// Create new area from floating point value in Square Yards
121
141
pub fn from_square_yards ( square_yards : f64 ) -> Self {
122
142
Self :: from_square_meters (
123
143
square_yards / ( length:: METER_YARD_FACTOR * length:: METER_YARD_FACTOR ) ,
124
144
)
125
145
}
126
146
147
+ /// Create new area from floating point value in Acres
127
148
pub fn from_acres ( acres : f64 ) -> Self {
128
149
Self :: from_square_meters ( acres / SQUARE_METER_ACRE_FACTOR )
129
150
}
130
151
152
+ /// Create new area from floating point value in Square Miles
131
153
pub fn from_square_miles ( square_miles : f64 ) -> Self {
132
154
Self :: from_square_meters (
133
155
square_miles / ( length:: METER_MILE_FACTOR * length:: METER_MILE_FACTOR ) ,
134
156
)
135
157
}
136
158
137
- // Outputs, metric
159
+ /// Convert this Area to a floating point value in Square Nanometers
138
160
pub fn as_square_nanometers ( & self ) -> f64 {
139
161
self . square_meters * ( length:: METER_NANOMETER_FACTOR * length:: METER_NANOMETER_FACTOR )
140
162
}
141
163
164
+ /// Convert this Area to a floating point value in Square Nanometres
142
165
pub fn as_square_nanometres ( & self ) -> f64 {
143
166
self . as_square_nanometers ( )
144
167
}
145
168
169
+ /// Convert this Area to a floating point value in Square Micrometers
146
170
pub fn as_square_micrometers ( & self ) -> f64 {
147
171
self . square_meters * ( length:: METER_MICROMETER_FACTOR * length:: METER_MICROMETER_FACTOR )
148
172
}
149
173
174
+ /// Convert this Area to a floating point value in Square Micrometres
150
175
pub fn as_square_micrometres ( & self ) -> f64 {
151
176
self . as_square_micrometers ( )
152
177
}
153
178
179
+ /// Convert this Area to a floating point value in Square Millimeters
154
180
pub fn as_square_millimeters ( & self ) -> f64 {
155
181
self . square_meters * ( length:: METER_MILLIMETER_FACTOR * length:: METER_MILLIMETER_FACTOR )
156
182
}
157
183
184
+ /// Convert this Area to a floating point value in Square Millimetres
158
185
pub fn as_square_millimetres ( & self ) -> f64 {
159
186
self . as_square_millimeters ( )
160
187
}
161
188
189
+ /// Convert this Area to a floating point value in Square Centimeters
162
190
pub fn as_square_centimeters ( & self ) -> f64 {
163
191
self . square_meters * ( length:: METER_CENTIMETER_FACTOR * length:: METER_CENTIMETER_FACTOR )
164
192
}
165
193
194
+ /// Convert this Area to a floating point value in Square Centimetres
166
195
pub fn as_square_centimetres ( & self ) -> f64 {
167
196
self . as_square_centimeters ( )
168
197
}
169
198
199
+ /// Convert this Area to a floating point value in Square Meters
170
200
pub fn as_square_meters ( & self ) -> f64 {
171
201
self . square_meters
172
202
}
173
203
204
+ /// Convert this Area to a floating point value in Square Metres
174
205
pub fn as_square_metres ( & self ) -> f64 {
175
206
self . as_square_meters ( )
176
207
}
177
208
209
+ /// Convert this Area to a floating point value in Square Decimeters
178
210
pub fn as_square_decimeters ( & self ) -> f64 {
179
211
self . square_meters * ( length:: METER_DECIMETER_FACTOR * length:: METER_DECIMETER_FACTOR )
180
212
}
181
213
214
+ /// Convert this Area to a floating point value in Square Decimetres
182
215
pub fn as_square_decimetres ( & self ) -> f64 {
183
216
self . as_square_decimeters ( )
184
217
}
185
218
219
+ /// Convert this Area to a floating point value in Square Hectometers
186
220
pub fn as_square_hectometers ( & self ) -> f64 {
187
221
self . square_meters * ( length:: METER_HECTOMETER_FACTOR * length:: METER_HECTOMETER_FACTOR )
188
222
}
189
223
224
+ /// Convert this Area to a floating point value in Square Hectometres
190
225
pub fn as_square_hectometres ( & self ) -> f64 {
191
226
self . as_square_hectometers ( )
192
227
}
193
228
229
+ /// Convert this Area to a floating point value in Hectares
194
230
pub fn as_hectares ( & self ) -> f64 {
195
231
self . as_square_hectometers ( )
196
232
}
197
233
234
+ /// Convert this Area to a floating point value in Square Kilometers
198
235
pub fn as_square_kilometers ( & self ) -> f64 {
199
236
self . square_meters * ( length:: METER_KILOMETER_FACTOR * length:: METER_KILOMETER_FACTOR )
200
237
}
201
238
239
+ /// Convert this Area to a floating point value in Square Kilometres
202
240
pub fn as_square_kilometres ( & self ) -> f64 {
203
241
self . as_square_kilometers ( )
204
242
}
205
243
206
- // Outputs, imperial
244
+ /// Convert this Area to a floating point value in Square Inches
207
245
pub fn as_square_inches ( & self ) -> f64 {
208
246
self . square_meters * ( length:: METER_INCH_FACTOR * length:: METER_INCH_FACTOR )
209
247
}
210
248
249
+ /// Convert this Area to a floating point value in Square Feet
211
250
pub fn as_square_feet ( & self ) -> f64 {
212
251
self . square_meters * ( length:: METER_FEET_FACTOR * length:: METER_FEET_FACTOR )
213
252
}
214
253
254
+ /// Convert this Area to a floating point value in Square Yards
215
255
pub fn as_square_yards ( & self ) -> f64 {
216
256
self . square_meters * ( length:: METER_YARD_FACTOR * length:: METER_YARD_FACTOR )
217
257
}
218
258
259
+ /// Convert this Area to a floating point value in Acres
219
260
pub fn as_acres ( & self ) -> f64 {
220
261
self . square_meters * SQUARE_METER_ACRE_FACTOR
221
262
}
222
263
264
+ /// Convert this Area to a floating point value in Square Miles
223
265
pub fn as_square_miles ( & self ) -> f64 {
224
266
self . square_meters * ( length:: METER_MILE_FACTOR * length:: METER_MILE_FACTOR )
225
267
}
0 commit comments