Explorar o código

Add smoothstep module and update create_grid function

Ghastrod hai 1 ano
pai
achega
042297ce64
Modificáronse 2 ficheiros con 29 adicións e 13 borrados
  1. 10 13
      src/main.rs
  2. 19 0
      src/smoothstep.rs

+ 10 - 13
src/main.rs

@@ -1,13 +1,13 @@
 #![allow(dead_code)]
 use rand::Rng;
-
+mod smoothstep;
 
 //Constants
 const N : usize = 5;
 
 fn main(){
     println!("Hello, world!");
-    println!("{:?}", create_grid(10));
+    //println!("{:?}", create_grid_with_vectors(10));
 }
 fn dotproduct(a:Vec<f64>, b:Vec<f64>)->f64{
     let mut sum = 0.0;
@@ -27,7 +27,7 @@ fn crossproduct(a:Vec<f64>, b:Vec<f64>)-> Vec<f64>{
 
 use std::fmt::Debug;
 
-#[derive(Debug)]
+#[derive(Debug, Clone)]
 struct GridPoint {
     coords: Vec<f64>,
 }
@@ -40,7 +40,8 @@ impl GridPoint {
     }
 }
 
-fn create_grid_point() -> GridPoint {
+//Create a random vector for a point of the grid
+fn create_grid_vector() -> GridPoint {
     let mut rng = rand::thread_rng();
     let mut point = GridPoint::new(N);
     for i in 0..N {
@@ -48,14 +49,11 @@ fn create_grid_point() -> GridPoint {
     }
     point
 }
+//Create a grid of points in N dimensions between -1 and 1 for the N dimensions
+//
+
+
 
-fn create_grid(n: usize) -> Vec<GridPoint> {
-    let mut points = Vec::new();
-    for _ in 0..n {
-        points.push(create_grid_point());
-    }
-    points
-}
 
 fn distance(a: &GridPoint, b: &GridPoint) -> f64 {
     let mut sum = 0.0;
@@ -63,5 +61,4 @@ fn distance(a: &GridPoint, b: &GridPoint) -> f64 {
         sum += (a.coords[i] - b.coords[i]).powi(2);
     }
     sum.sqrt()
-}
-
+}

+ 19 - 0
src/smoothstep.rs

@@ -0,0 +1,19 @@
+pub fn clamp(x: f64, min: f64, max: f64) -> f64 {
+    if x < min {
+        min
+    } else if x > max {
+        max
+    } else {
+        x
+    }
+}
+
+pub fn smoothstep(edge0: f64, edge1: f64, x: f64) -> f64 {
+    let x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
+    x * x * (3.0 - 2.0 * x)
+}
+
+pub fn smootherstep(edge0: f64, edge1: f64, x: f64) -> f64 {
+    let x = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
+    x * x * x * (x * (x * 6.0 - 15.0) + 10.0)
+}