added line project
This commit is contained in:
parent
ad2bf148c3
commit
4342c25e3e
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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]
|
|
@ -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<Output = Self> + Add<Output = Self> + Sub<Output = Self> + Div<Output = Self> + Copy
|
||||
{
|
||||
}
|
||||
|
||||
impl<T> Calculate for T where
|
||||
T: Mul<Output = Self> + Add<Output = Self> + Sub<Output = Self> + Div<Output = Self> + Copy
|
||||
{
|
||||
}
|
||||
|
||||
struct Point<T: Mul<Output = T> + Add<Output = T> + Copy> {
|
||||
x: T,
|
||||
y: T,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl<T: Mul<Output = T> + Add<Output = T> + Copy> Point<T> {
|
||||
fn squared_dist_to_0(&self) -> T {
|
||||
self.x * self.x + self.y * self.y
|
||||
}
|
||||
}
|
||||
|
||||
trait MeasureDistanceTo0<T: Calculate> {
|
||||
fn squared_dist_to_0(&self) -> T;
|
||||
}
|
||||
|
||||
struct Line<T: Calculate> {
|
||||
p: Point<T>,
|
||||
n: Point<T>,
|
||||
}
|
||||
|
||||
impl<T: Calculate + Default> MeasureDistanceTo0<T> for Line<T> {
|
||||
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<T: Calculate + Default> Line<T> {
|
||||
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<T>(p1: Line<T>, p2: Line<T>) -> 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<f64> = Line::new();
|
||||
let l1: Line<f64> = Line::new();
|
||||
|
||||
println!("{:?}", longest_dist_to_0(l0, l1));
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue