Commit 31aa7b729c29c61bc75e2988e5db92178ecb2eb8

Authored by Georg Hopp
1 parent ae2f27ce

Add first vector math stuff

... ... @@ -22,5 +22,6 @@ extern crate lazy_static;
22 22
23 23 pub mod fractional;
24 24 pub mod trigonometry;
  25 +pub mod vector;
25 26
26 27 use fractional::{Fractional};
... ...
... ... @@ -23,13 +23,8 @@ use std::num::TryFromIntError;
23 23 use std::f64::consts::PI as FPI;
24 24
25 25 use fractional::fractional::{Fractional, from_vector, Continuous};
26   -use fractional::trigonometry::{sin, cos, PI};
27   -
28   -struct Vector {
29   - x: Fractional,
30   - y: Fractional,
31   - z: Fractional,
32   -}
  26 +use fractional::trigonometry::{sin, cos, tan, PI};
  27 +use fractional::vector::{Vector};
33 28
34 29 fn mean(v: &Vec<i64>) -> Result<Fractional, TryFromIntError> {
35 30 let r = v.iter().fold(0, |acc, x| acc + x);
... ... @@ -98,7 +93,8 @@ fn pi() {
98 93 }
99 94
100 95 fn _sin() {
101   - for d in [9, 17, 31, 45, 73, 123, 213, 312, 876].iter() {
  96 + for d in [ 0, 45, 90, 135, 180, 225, 270, 315
  97 + , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
102 98 let s = sin(*d as i32);
103 99 let sr :f64 = s.try_into().unwrap();
104 100 let _s = f64::sin(*d as f64 * FPI / 180.0);
... ... @@ -107,8 +103,20 @@ fn _sin() {
107 103 }
108 104 }
109 105
  106 +fn _tan() {
  107 + for d in [ 0, 45, 90, 135, 180, 225, 270, 315
  108 + , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
  109 + let s = tan(*d as i32);
  110 + let sr :f64 = s.try_into().unwrap();
  111 + let _s = f64::tan(*d as f64 * FPI / 180.0);
  112 +
  113 + println!("{:>14} : {} {} / {}", format!("tan {}", d), s, sr, _s);
  114 + }
  115 +}
  116 +
110 117 fn _cos() {
111   - for d in [9, 17, 31, 45, 73, 123, 213, 312, 876].iter() {
  118 + for d in [ 0, 45, 90, 135, 180, 225, 270, 315
  119 + , 9, 17, 31, 73, 89, 123, 213, 312, 876 ].iter() {
112 120 let s = cos(*d as i32);
113 121 let sr :f64 = s.try_into().unwrap();
114 122 let _s = f64::cos(*d as f64 * FPI / 180.0);
... ... @@ -117,6 +125,29 @@ fn _cos() {
117 125 }
118 126 }
119 127
  128 +fn _vector() {
  129 + let v1 = Vector(1.into(), 2.into(), 3.into());
  130 + let v2 = Vector(2.into(), 2.into(), 3.into());
  131 + let s :Fractional = 3.into();
  132 +
  133 + println!("{:>14} : {:?}", "Vector v1", v1);
  134 + println!("{:>14} : {:?}", "Vector v2", v2);
  135 + println!("{:>14} : {}" , "abs v1", v1.abs());
  136 + println!("{:>14} : {:?}", "-v1", -v1);
  137 + println!("{:>14} : {:?}", "v1 + v1", v1 + v1);
  138 + println!("{:>14} : {:?}", "v1 - v1", v1 - v1);
  139 + println!("{:>14} : {:?}", "v2 - v1", v2 - v1);
  140 + println!("{:>14} : {:?}", format!("v1 * {}", s), v1.mul(&s));
  141 + println!("{:>14} : {:?}", "norm v1", v1.norm());
  142 + println!("{:>14} : {}" , "abs norm v1", v1.norm().abs());
  143 + println!("{:>14} : {}" , "distance v1 v2", v1.distance(v2));
  144 + println!("{:>14} : {}" , "distance v2 v1", v2.distance(v1));
  145 + println!("{:>14} : {}" , "v1 dot v2", v1.dot(v2));
  146 + println!("{:>14} : {}" , "v2 dot v1", v2.dot(v1));
  147 + println!("{:>14} : {:?}", "v1 * v2", v1 * v2);
  148 + println!("{:>14} : {:?}", "v2 * v1", v2 * v1);
  149 +}
  150 +
120 151 fn main() {
121 152 common_fractional();
122 153 println!();
... ... @@ -129,4 +160,8 @@ fn main() {
129 160 _sin();
130 161 println!();
131 162 _cos();
  163 + println!();
  164 + _tan();
  165 + println!();
  166 + _vector();
132 167 }
... ...
  1 +//
  2 +// Stuff for manipulating 3 dimensional vectors.
  3 +//
  4 +// Georg Hopp <georg@steffers.org>
  5 +//
  6 +// Copyright © 2019 Georg Hopp
  7 +//
  8 +// This program is free software: you can redistribute it and/or modify
  9 +// it under the terms of the GNU General Public License as published by
  10 +// the Free Software Foundation, either version 3 of the License, or
  11 +// (at your option) any later version.
  12 +//
  13 +// This program is distributed in the hope that it will be useful,
  14 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 +// GNU General Public License for more details.
  17 +//
  18 +// You should have received a copy of the GNU General Public License
  19 +// along with this program. If not, see <http://www.gnu.org/licenses/>.
  20 +//
  21 +use std::ops::{Add, Sub, Neg, Mul};
  22 +use crate::{Fractional};
  23 +
  24 +#[derive(Debug, Eq, Clone, Copy)]
  25 +pub struct Vector(pub Fractional, pub Fractional, pub Fractional);
  26 +
  27 +impl Vector {
  28 + pub fn x(self) -> Fractional { self.0 }
  29 + pub fn y(self) -> Fractional { self.1 }
  30 + pub fn z(self) -> Fractional { self.2 }
  31 +
  32 + pub fn abs(self) -> Fractional {
  33 + let Vector(x, y, z) = self;
  34 + (x * x + y * y + z * z).sqrt().unwrap()
  35 + }
  36 +
  37 + pub fn mul(self, s :&Fractional) -> Self {
  38 + let Vector(x, y, z) = self;
  39 + Vector(x * *s, y * *s, z * *s)
  40 + }
  41 +
  42 + pub fn dot(self, other :Self) -> Fractional {
  43 + let Vector(x1, y1, z1) = self;
  44 + let Vector(x2, y2, z2) = other;
  45 +
  46 + x1 * x2 + y1 * y2 + z1 * z2
  47 + }
  48 +
  49 + pub fn norm(self) -> Self {
  50 + let Fractional(n, d) = self.abs();
  51 + // TODO This can result in 0 or inf Vectors…
  52 + // Maybe we need to handle zero and inf abs here…
  53 + self.mul(&Fractional(d, n))
  54 + }
  55 +
  56 + pub fn distance(self, other :Self) -> Fractional {
  57 + (self - other).abs()
  58 + }
  59 +}
  60 +
  61 +impl PartialEq for Vector {
  62 + fn eq(&self, other :&Self) -> bool {
  63 + let Vector(x1, y1, z1) = self;
  64 + let Vector(x2, y2, z2) = other;
  65 + x1 == x2 && y1 == y2 && z1 == z2
  66 + }
  67 +}
  68 +
  69 +impl Add for Vector {
  70 + type Output = Self;
  71 +
  72 + fn add(self, other :Self) -> Self {
  73 + let Vector(x1, y1, z1) = self;
  74 + let Vector(x2, y2, z2) = other;
  75 + Vector(x1 + x2, y1 + y2, z1 + z2)
  76 + }
  77 +}
  78 +
  79 +impl Sub for Vector {
  80 + type Output = Self;
  81 +
  82 + fn sub(self, other :Self) -> Self {
  83 + self + -other
  84 + }
  85 +}
  86 +
  87 +impl Neg for Vector {
  88 + type Output = Self;
  89 +
  90 + fn neg(self) -> Self {
  91 + let Vector(x, y, z) = self;
  92 + Self(-x, -y, -z)
  93 + }
  94 +}
  95 +
  96 +impl Mul for Vector {
  97 + type Output = Self;
  98 +
  99 + fn mul(self, other :Self) -> Self {
  100 + let Vector(ax, ay, az) = self;
  101 + let Vector(bx, by, bz) = other;
  102 +
  103 + Vector( ay * bz - az * by
  104 + , az * bx - ax * bz
  105 + , ax * by - ay * bx )
  106 + }
  107 +}
... ...
Please register or login to post a comment