├── README.md ├── Cargo.toml ├── .gitignore ├── LICENSE └── src └── main.rs /README.md: -------------------------------------------------------------------------------- 1 | # barycenter-finder 2 | A small barycenter finder built in Rust 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "barycenter-finder" 3 | version = "0.1.0" 4 | authors = ["Omar Jair Purata Funes "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | /target/ 4 | 5 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 6 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 7 | Cargo.lock 8 | 9 | # These are backup files generated by rustfmt 10 | **/*.rs.bk 11 | 12 | 13 | #Added by cargo 14 | # 15 | #already existing elements are commented out 16 | 17 | /target 18 | #**/*.rs.bk 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Future Lab 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | mod bodies; 2 | 3 | use bodies::get_values; 4 | 5 | #[derive(Debug, Clone, Copy)] 6 | pub struct Body { 7 | x: f64, 8 | y: f64, 9 | z: f64, 10 | mass: f64 11 | } 12 | 13 | fn average(a: f64, b:f64) -> f64 { 14 | (a + b) / 2.0 15 | } 16 | 17 | fn average_with_mass(a: f64, b:f64, amass: f64, bmass: f64) -> f64 { 18 | average(a * amass, b * bmass) / (amass * bmass) 19 | } 20 | 21 | fn merge_two_bodies(a: Body, b: Body) -> Body { 22 | Body { 23 | x: average_with_mass(a.x, b.x, a.mass, b.mass), 24 | y: average_with_mass(a.y, b.y, a.mass, b.mass), 25 | z: average_with_mass(a.z, b.z, a.mass, b.mass), 26 | mass: a.mass + b.mass 27 | } 28 | } 29 | 30 | fn merge_all_bodies_iter(bodies: &[Body]) -> Body { 31 | let barycenter = bodies[0]; 32 | bodies.iter().skip(1).fold(barycenter, |barycenter, body| { 33 | merge_two_bodies(barycenter, *body) 34 | }); 35 | } 36 | 37 | fn main() { 38 | let bodies = get_values(); 39 | let barycenter = merge_all_bodies(&bodies); 40 | println!("Barycenter: ({} {} {})\n Mass: {}", barycenter.x, barycenter.y, barycenter.z, barycenter.mass); 41 | } 42 | --------------------------------------------------------------------------------