From 3b2251746c2af6e79d0d18eb7f4157eb80382012 Mon Sep 17 00:00:00 2001 From: Cabbache Date: Fri, 3 Feb 2023 17:48:10 +0100 Subject: [PATCH 1/3] Add cone generator --- src/noise_fns/generators.rs | 3 +- src/noise_fns/generators/cone.rs | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/noise_fns/generators/cone.rs diff --git a/src/noise_fns/generators.rs b/src/noise_fns/generators.rs index 663e7c3d..6fbcc3df 100644 --- a/src/noise_fns/generators.rs +++ b/src/noise_fns/generators.rs @@ -1,11 +1,12 @@ pub use self::{ checkerboard::*, constant::*, cylinders::*, fractals::*, open_simplex::*, perlin::*, - perlin_surflet::*, simplex::*, super_simplex::*, value::*, worley::*, + perlin_surflet::*, simplex::*, super_simplex::*, value::*, worley::*, cone::*, }; mod checkerboard; mod constant; mod cylinders; +mod cone; mod fractals; mod open_simplex; mod perlin; diff --git a/src/noise_fns/generators/cone.rs b/src/noise_fns/generators/cone.rs new file mode 100644 index 00000000..3a6a30b1 --- /dev/null +++ b/src/noise_fns/generators/cone.rs @@ -0,0 +1,47 @@ +use crate::noise_fns::NoiseFn; + +/// Noise function that outputs a cone. +/// +/// This noise function takes a 2d point and outputs a cone that is aligned along the z axis. +/// The origin has a value of 1 and points with a distance from the origin beyond the radius +/// of the cone are -1. +#[derive(Clone, Copy, Debug)] +pub struct Cone { + /// radius of the cone. + pub radius: f64, +} + +impl Cone { + pub const DEFAULT_RADIUS: f64 = 1.0; + + pub fn new() -> Self { + Self { + radius: Self::DEFAULT_RADIUS, + } + } + + pub fn set_radius(self, radius: f64) -> Self { + Self { radius } + } +} + +impl Default for Cone { + fn default() -> Self { + Self::new() + } +} + +impl NoiseFn for Cone { + fn get(&self, point: [f64; 2]) -> f64 { + let x = point[0]; + let y = point[1]; + + // Calculate the distance of the point from the origin. + let dist_from_center = (x.powi(2) + y.powi(2)).sqrt(); + + match dist_from_center > self.radius{ + true => -1f64, + false => 1.0 - 2.0*(dist_from_center / self.radius) + } + } +} From e107eaf25b1052f7c94407cace1c2b0677aa2cbb Mon Sep 17 00:00:00 2001 From: Cabbache Date: Fri, 3 Feb 2023 20:36:36 +0100 Subject: [PATCH 2/3] remove sqrt --- src/noise_fns/generators/cone.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/noise_fns/generators/cone.rs b/src/noise_fns/generators/cone.rs index 3a6a30b1..dc3202e0 100644 --- a/src/noise_fns/generators/cone.rs +++ b/src/noise_fns/generators/cone.rs @@ -9,6 +9,9 @@ use crate::noise_fns::NoiseFn; pub struct Cone { /// radius of the cone. pub radius: f64, + + /// private member, square of the radius + radius_squared: f64, } impl Cone { @@ -17,11 +20,12 @@ impl Cone { pub fn new() -> Self { Self { radius: Self::DEFAULT_RADIUS, + radius_squared: Self::DEFAULT_RADIUS.powi(2), } } pub fn set_radius(self, radius: f64) -> Self { - Self { radius } + Self { radius: radius, radius_squared: radius.powi(2) } } } @@ -36,12 +40,11 @@ impl NoiseFn for Cone { let x = point[0]; let y = point[1]; - // Calculate the distance of the point from the origin. - let dist_from_center = (x.powi(2) + y.powi(2)).sqrt(); + let dist_from_center_squared = x.powi(2) + y.powi(2); - match dist_from_center > self.radius{ + match dist_from_center_squared > self.radius_squared{ true => -1f64, - false => 1.0 - 2.0*(dist_from_center / self.radius) + false => 1.0 - 2.0*(dist_from_center_squared / self.radius_squared) } } } From 2778069103e06365435c27979a5eb773619bbb63 Mon Sep 17 00:00:00 2001 From: Cabbache Date: Fri, 3 Feb 2023 21:01:23 +0100 Subject: [PATCH 3/3] add .sqrt to case false and remove member radius --- src/noise_fns/generators/cone.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/noise_fns/generators/cone.rs b/src/noise_fns/generators/cone.rs index dc3202e0..d1c86695 100644 --- a/src/noise_fns/generators/cone.rs +++ b/src/noise_fns/generators/cone.rs @@ -7,10 +7,7 @@ use crate::noise_fns::NoiseFn; /// of the cone are -1. #[derive(Clone, Copy, Debug)] pub struct Cone { - /// radius of the cone. - pub radius: f64, - - /// private member, square of the radius + /// the cone's radius, sqaured radius_squared: f64, } @@ -19,13 +16,12 @@ impl Cone { pub fn new() -> Self { Self { - radius: Self::DEFAULT_RADIUS, radius_squared: Self::DEFAULT_RADIUS.powi(2), } } pub fn set_radius(self, radius: f64) -> Self { - Self { radius: radius, radius_squared: radius.powi(2) } + Self { radius_squared: radius.powi(2) } } } @@ -44,7 +40,7 @@ impl NoiseFn for Cone { match dist_from_center_squared > self.radius_squared{ true => -1f64, - false => 1.0 - 2.0*(dist_from_center_squared / self.radius_squared) + false => 1.0 - 2.0*(dist_from_center_squared / self.radius_squared).sqrt() } } }