Pārlūkot izejas kodu

Add rand dependency and implement grid point creation

Ghastrod 1 gadu atpakaļ
vecāks
revīzija
e7759859e6
4 mainītis faili ar 70 papildinājumiem un 1 dzēšanām
  1. 1 0
      Cargo.lock
  2. 2 0
      Cargo.toml
  3. 2 0
      justfile
  4. 65 1
      src/main.rs

+ 1 - 0
Cargo.lock

@@ -777,6 +777,7 @@ version = "0.1.0"
 dependencies = [
  "anyhow",
  "image",
+ "rand",
 ]
 
 [[package]]

+ 2 - 0
Cargo.toml

@@ -3,8 +3,10 @@ name = "simplex1"
 version = "0.1.0"
 edition = "2021"
 
+
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
 anyhow = "1.0.81"
 image = "0.25.0"
+rand = "0.8.5"

+ 2 - 0
justfile

@@ -0,0 +1,2 @@
+run:
+    cargo run

+ 65 - 1
src/main.rs

@@ -1,3 +1,67 @@
-fn main() {
+#![allow(dead_code)]
+use rand::Rng;
+
+
+//Constants
+const N : usize = 5;
+
+fn main(){
     println!("Hello, world!");
+    println!("{:?}", create_grid(10));
 }
+fn dotproduct(a:Vec<f64>, b:Vec<f64>)->f64{
+    let mut sum = 0.0;
+    for i in 0..a.len(){
+        sum += a[i]*b[i];
+    }
+    sum
+}
+
+fn crossproduct(a:Vec<f64>, b:Vec<f64>)-> Vec<f64>{
+    let mut c = vec![0.0; 3];
+    c[0] = a[1]*b[2] - a[2]*b[1];
+    c[1] = a[2]*b[0] - a[0]*b[2];
+    c[2] = a[0]*b[1] - a[1]*b[0];
+    c
+}
+
+use std::fmt::Debug;
+
+#[derive(Debug)]
+struct GridPoint {
+    coords: Vec<f64>,
+}
+
+impl GridPoint {
+    fn new(dimensions: usize) -> Self {
+        GridPoint {
+            coords: vec![0.0; dimensions],
+        }
+    }
+}
+
+fn create_grid_point() -> GridPoint {
+    let mut rng = rand::thread_rng();
+    let mut point = GridPoint::new(N);
+    for i in 0..N {
+        point.coords[i] = rng.gen_range(-1.0..=1.0);
+    }
+    point
+}
+
+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;
+    for i in 0..N {
+        sum += (a.coords[i] - b.coords[i]).powi(2);
+    }
+    sum.sqrt()
+}
+