|
| 1 | +use super::measurement::*; |
| 2 | +use length; |
| 3 | +use volume; |
| 4 | + |
| 5 | +const SQUARE_METER_ACRE_FACTOR: f64 = 4046.86; |
| 6 | + |
| 7 | +/// The `Area` struct can be used to deal with areas in a common way. |
| 8 | +/// Common metric and imperial units are supported. |
| 9 | +/// |
| 10 | +/// # Example |
| 11 | +/// |
| 12 | +/// ``` |
| 13 | +/// use measurements::Area; |
| 14 | +/// |
| 15 | +/// let football_field = Area::from_square_meters(7140.0); |
| 16 | +/// let acres = football_field.as_acres(); |
| 17 | +/// println!("There are {} acres in a football field.", acres); |
| 18 | +/// ``` |
| 19 | +#[derive(Copy, Clone, Debug)] |
| 20 | +pub struct Area { |
| 21 | + square_meters: f64, |
| 22 | +} |
| 23 | + |
| 24 | +impl Area { |
| 25 | + // Inputs, metric, with both spellings of meter/metre |
| 26 | + |
| 27 | + pub fn from_square_meters(square_meters: f64) -> Self { |
| 28 | + Area { square_meters: square_meters } |
| 29 | + } |
| 30 | + |
| 31 | + pub fn from_square_metres(square_metres: f64) -> Self { |
| 32 | + Self::from_square_meters(square_metres) |
| 33 | + } |
| 34 | + |
| 35 | + pub fn from_square_nanometers(square_nanometers: f64) -> Self { |
| 36 | + Self::from_square_meters(square_nanometers / (length::METER_NANOMETER_FACTOR * length::METER_NANOMETER_FACTOR)) |
| 37 | + } |
| 38 | + |
| 39 | + pub fn from_square_nanometres(square_nanometres: f64) -> Self { |
| 40 | + Self::from_square_nanometers(square_nanometres) |
| 41 | + } |
| 42 | + |
| 43 | + pub fn from_square_micrometers(square_micrometers: f64) -> Self { |
| 44 | + Self::from_square_meters(square_micrometers / (length::METER_MICROMETER_FACTOR * length::METER_MICROMETER_FACTOR)) |
| 45 | + } |
| 46 | + |
| 47 | + pub fn from_square_micrometres(square_micrometres: f64) -> Self { |
| 48 | + Self::from_square_micrometers(square_micrometres) |
| 49 | + } |
| 50 | + |
| 51 | + pub fn from_square_millimeters(square_millimeters: f64) -> Self { |
| 52 | + Self::from_square_meters(square_millimeters / (length::METER_MILLIMETER_FACTOR * length::METER_MILLIMETER_FACTOR)) |
| 53 | + } |
| 54 | + |
| 55 | + pub fn from_square_millimetres(square_millimetres: f64) -> Self { |
| 56 | + Self::from_square_millimeters(square_millimetres) |
| 57 | + } |
| 58 | + |
| 59 | + pub fn from_square_centimeters(square_centimeters: f64) -> Self { |
| 60 | + Self::from_square_meters(square_centimeters / (length::METER_CENTIMETER_FACTOR * length::METER_CENTIMETER_FACTOR)) |
| 61 | + } |
| 62 | + |
| 63 | + pub fn from_square_centimetres(square_centimetres: f64) -> Self { |
| 64 | + Self::from_square_centimeters(square_centimetres) |
| 65 | + } |
| 66 | + |
| 67 | + pub fn from_square_decameters(square_decameters: f64) -> Self { |
| 68 | + Self::from_square_meters(square_decameters / (length::METER_DECAMETER_FACTOR * length::METER_DECAMETER_FACTOR)) |
| 69 | + } |
| 70 | + |
| 71 | + pub fn from_square_decametres(square_decametres: f64) -> Self { |
| 72 | + Self::from_square_decameters(square_decametres) |
| 73 | + } |
| 74 | + |
| 75 | + pub fn from_square_hectometers(square_hectometers: f64) -> Self { |
| 76 | + Self::from_square_meters(square_hectometers / (length::METER_HECTOMETER_FACTOR * length::METER_HECTOMETER_FACTOR)) |
| 77 | + } |
| 78 | + |
| 79 | + pub fn from_square_hectometres(square_hectometres: f64) -> Self { |
| 80 | + Self::from_square_hectometers(square_hectometres) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn from_hectares(hectares: f64) -> Self { |
| 84 | + Self::from_square_hectometers(hectares) |
| 85 | + } |
| 86 | + |
| 87 | + pub fn from_square_kilometers(square_kilometers: f64) -> Self { |
| 88 | + Self::from_square_meters(square_kilometers / (length::METER_KILOMETER_FACTOR * length::METER_KILOMETER_FACTOR)) |
| 89 | + } |
| 90 | + |
| 91 | + pub fn from_square_kilometres(square_kilometres: f64) -> Self { |
| 92 | + Self::from_square_kilometers(square_kilometres) |
| 93 | + } |
| 94 | + |
| 95 | + // Inputs, imperial |
| 96 | + pub fn from_square_inches(square_inches: f64) -> Self { |
| 97 | + Self::from_square_meters(square_inches / (length::METER_INCH_FACTOR * length::METER_INCH_FACTOR)) |
| 98 | + } |
| 99 | + |
| 100 | + pub fn from_square_feet(square_feet: f64) -> Self { |
| 101 | + Self::from_square_meters(square_feet / (length::METER_FEET_FACTOR * length::METER_FEET_FACTOR)) |
| 102 | + } |
| 103 | + |
| 104 | + pub fn from_square_yards(square_yards: f64) -> Self { |
| 105 | + Self::from_square_meters(square_yards / (length::METER_YARD_FACTOR * length::METER_YARD_FACTOR)) |
| 106 | + } |
| 107 | + |
| 108 | + pub fn from_acres(acres: f64) -> Self { |
| 109 | + Self::from_square_meters(acres / SQUARE_METER_ACRE_FACTOR) |
| 110 | + } |
| 111 | + |
| 112 | + pub fn from_square_miles(square_miles: f64) -> Self { |
| 113 | + Self::from_square_meters(square_miles / (length::METER_MILE_FACTOR * length::METER_MILE_FACTOR)) |
| 114 | + } |
| 115 | + |
| 116 | + // Outputs, metric |
| 117 | + pub fn as_square_nanometers(&self) -> f64 { |
| 118 | + self.square_meters * length::METER_NANOMETER_FACTOR * length::METER_NANOMETER_FACTOR |
| 119 | + } |
| 120 | + |
| 121 | + pub fn as_square_nanometres(&self) -> f64 { |
| 122 | + self.as_square_nanometers() |
| 123 | + } |
| 124 | + |
| 125 | + pub fn as_square_micrometers(&self) -> f64 { |
| 126 | + self.square_meters * length::METER_MICROMETER_FACTOR * length::METER_MICROMETER_FACTOR |
| 127 | + } |
| 128 | + |
| 129 | + pub fn as_square_micrometres(&self) -> f64 { |
| 130 | + self.as_square_micrometers() |
| 131 | + } |
| 132 | + |
| 133 | + pub fn as_square_millimeters(&self) -> f64 { |
| 134 | + self.square_meters * length::METER_MILLIMETER_FACTOR * length::METER_MILLIMETER_FACTOR |
| 135 | + } |
| 136 | + |
| 137 | + pub fn as_square_millimetres(&self) -> f64 { |
| 138 | + self.as_square_millimeters() |
| 139 | + } |
| 140 | + |
| 141 | + pub fn as_square_centimeters(&self) -> f64 { |
| 142 | + self.square_meters * length::METER_CENTIMETER_FACTOR * length::METER_CENTIMETER_FACTOR |
| 143 | + } |
| 144 | + |
| 145 | + pub fn as_square_centimetres(&self) -> f64 { |
| 146 | + self.as_square_centimeters() |
| 147 | + } |
| 148 | + |
| 149 | + pub fn as_square_meters(&self) -> f64 { |
| 150 | + self.square_meters |
| 151 | + } |
| 152 | + |
| 153 | + pub fn as_square_metres(&self) -> f64 { |
| 154 | + self.as_square_meters() |
| 155 | + } |
| 156 | + |
| 157 | + pub fn as_square_decameters(&self) -> f64 { |
| 158 | + self.square_meters * length::METER_DECAMETER_FACTOR * length::METER_DECAMETER_FACTOR |
| 159 | + } |
| 160 | + |
| 161 | + pub fn as_square_decametres(&self) -> f64 { |
| 162 | + self.as_square_decameters() |
| 163 | + } |
| 164 | + |
| 165 | + pub fn as_square_hectometers(&self) -> f64 { |
| 166 | + self.square_meters * length::METER_HECTOMETER_FACTOR * length::METER_HECTOMETER_FACTOR |
| 167 | + } |
| 168 | + |
| 169 | + pub fn as_square_hectometres(&self) -> f64 { |
| 170 | + self.as_square_hectometers() |
| 171 | + } |
| 172 | + |
| 173 | + pub fn as_hectares(&self) -> f64 { |
| 174 | + self.as_square_hectometers() |
| 175 | + } |
| 176 | + |
| 177 | + pub fn as_square_kilometers(&self) -> f64 { |
| 178 | + self.square_meters * length::METER_KILOMETER_FACTOR * length::METER_KILOMETER_FACTOR |
| 179 | + } |
| 180 | + |
| 181 | + pub fn as_square_kilometres(&self) -> f64 { |
| 182 | + self.as_square_kilometers() |
| 183 | + } |
| 184 | + |
| 185 | + // Outputs, imperial |
| 186 | + pub fn as_square_inches(&self) -> f64 { |
| 187 | + self.square_meters * length::METER_INCH_FACTOR * length::METER_INCH_FACTOR |
| 188 | + } |
| 189 | + |
| 190 | + pub fn as_square_feet(&self) -> f64 { |
| 191 | + self.square_meters * length::METER_FEET_FACTOR * length::METER_FEET_FACTOR |
| 192 | + } |
| 193 | + |
| 194 | + pub fn as_square_yards(&self) -> f64 { |
| 195 | + self.square_meters * length::METER_YARD_FACTOR * length::METER_YARD_FACTOR |
| 196 | + } |
| 197 | + |
| 198 | + pub fn as_acres(&self) -> f64 { |
| 199 | + self.square_meters * SQUARE_METER_ACRE_FACTOR |
| 200 | + } |
| 201 | + |
| 202 | + pub fn as_square_miles(&self) -> f64 { |
| 203 | + self.square_meters * length::METER_MILE_FACTOR * length::METER_MILE_FACTOR |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +/// Area / Length = Length |
| 208 | +impl ::std::ops::Div<length::Length> for Area { |
| 209 | + type Output = length::Length; |
| 210 | + |
| 211 | + fn div(self, rhs: length::Length) -> length::Length { |
| 212 | + length::Length::from_meters(self.as_square_meters() / rhs.as_meters()) |
| 213 | + } |
| 214 | +} |
| 215 | + |
| 216 | +/// Area * Length = Volume |
| 217 | +impl ::std::ops::Mul<length::Length> for Area { |
| 218 | + type Output = volume::Volume; |
| 219 | + |
| 220 | + fn mul(self, rhs: length::Length) -> volume::Volume { |
| 221 | + volume::Volume::from_cubic_meters(self.as_square_meters() * rhs.as_meters()) |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +impl Measurement for Area { |
| 226 | + fn get_base_units(&self) -> f64 { |
| 227 | + self.square_meters |
| 228 | + } |
| 229 | + |
| 230 | + fn from_base_units(units: f64) -> Self { |
| 231 | + Self::from_square_meters(units) |
| 232 | + } |
| 233 | + |
| 234 | + fn get_base_units_name(&self) -> &'static str { |
| 235 | + "m\u{00B2}" |
| 236 | + } |
| 237 | + |
| 238 | + fn get_appropriate_units(&self) -> (&'static str, f64) { |
| 239 | + // Smallest to largest |
| 240 | + let list = [ |
| 241 | + ("nm\u{00B2}", 1e-18), |
| 242 | + ("\u{00B5}m\u{00B2}", 1e-12), |
| 243 | + ("mm\u{00B2}", 1e-6), |
| 244 | + ("cm\u{00B2}", 1e-4), |
| 245 | + ("m\u{00B2}", 1e0), |
| 246 | + ("km\u{00B2}", 1e6), |
| 247 | + ("thousand km\u{00B2}", 1e9), |
| 248 | + ("million km\u{00B2}", 1e12), |
| 249 | + ]; |
| 250 | + self.pick_appropriate_units(&list) |
| 251 | + } |
| 252 | +} |
| 253 | + |
| 254 | +implement_measurement! { Area } |
0 commit comments