From 4342c25e3e02cb27d19e0d5415ac42fff5f278b0 Mon Sep 17 00:00:00 2001 From: teridax Date: Wed, 3 May 2023 10:34:57 +0200 Subject: [PATCH] added line project --- duplicates/src/main.rs | 2 - fibonacci/src/main.rs | 3 +- line/Cargo.toml | 8 +++ line/src/main.rs | 96 ++++++++++++++++++++++++++++++++++++ tuple_arithmetic/src/main.rs | 1 + 5 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 line/Cargo.toml create mode 100644 line/src/main.rs diff --git a/duplicates/src/main.rs b/duplicates/src/main.rs index a55267a..f7287c2 100644 --- a/duplicates/src/main.rs +++ b/duplicates/src/main.rs @@ -14,9 +14,7 @@ * Licensed under the GPLv2 License, Version 2.0 (the "License"); * Copyright (c) Sven Vogel */ - use crate::FloatOrChar::{Char, Float}; -use std::collections::{HashMap, HashSet}; use std::fmt::Debug; use std::usize; diff --git a/fibonacci/src/main.rs b/fibonacci/src/main.rs index 9551b6e..8f625c2 100644 --- a/fibonacci/src/main.rs +++ b/fibonacci/src/main.rs @@ -16,6 +16,7 @@ */ /// recursive variant of the fibonacci function +#[allow(dead_code)] fn fib_rec(x: u128) -> u128 { if x < 2 { return x; @@ -29,7 +30,7 @@ fn fib_loop(x: u128) -> u128 { let mut sum = 0; let mut sum2 = 1; - for x in 0..(x - 1) { + for _ in 0..(x - 1) { let t = sum; sum = sum2; sum2 = t + sum; diff --git a/line/Cargo.toml b/line/Cargo.toml new file mode 100644 index 0000000..8d42634 --- /dev/null +++ b/line/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "line" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/line/src/main.rs b/line/src/main.rs new file mode 100644 index 0000000..837a7cc --- /dev/null +++ b/line/src/main.rs @@ -0,0 +1,96 @@ +/** + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +use std::cmp::PartialOrd; +use std::ops::{Add, Div, Mul, Sub}; + +trait Calculate: + Mul + Add + Sub + Div + Copy +{ +} + +impl Calculate for T where + T: Mul + Add + Sub + Div + Copy +{ +} + +struct Point + Add + Copy> { + x: T, + y: T, +} + +#[allow(dead_code)] +impl + Add + Copy> Point { + fn squared_dist_to_0(&self) -> T { + self.x * self.x + self.y * self.y + } +} + +trait MeasureDistanceTo0 { + fn squared_dist_to_0(&self) -> T; +} + +struct Line { + p: Point, + n: Point, +} + +impl MeasureDistanceTo0 for Line { + fn squared_dist_to_0(&self) -> T { + let len = self.n.x * self.n.x + self.n.y * self.n.y; + let normalized = Point { + x: self.n.x / len, + y: self.n.y / len, + }; + + normalized.x * self.p.x + normalized.y * self.p.y + } +} + +impl Line { + pub fn new() -> Self { + Self { + p: Point { + x: Default::default(), + y: Default::default(), + }, + n: Point { + x: Default::default(), + y: Default::default(), + }, + } + } +} + +fn longest_dist_to_0(p1: Line, p2: Line) -> T +where + T: Calculate + Default + PartialOrd, +{ + let d1 = p1.squared_dist_to_0(); + let d2 = p2.squared_dist_to_0(); + if d1 > d2 { + d1 + } else { + d2 + } +} + +fn main() { + let l0: Line = Line::new(); + let l1: Line = Line::new(); + + println!("{:?}", longest_dist_to_0(l0, l1)); +} diff --git a/tuple_arithmetic/src/main.rs b/tuple_arithmetic/src/main.rs index 64c5249..432f1b6 100644 --- a/tuple_arithmetic/src/main.rs +++ b/tuple_arithmetic/src/main.rs @@ -28,6 +28,7 @@ fn merge_tuple(a: (f64, f64), b: (f64, f64), f: fn(a: f64, b: f64) -> f64) -> (f (f(a.0, b.0), f(a.1, b.1)) } +#[allow(dead_code)] fn add_tuple(a: (f64, f64), b: (f64, f64)) -> (f64, f64) { merge_tuple(a, b, |a, b| a + b) }