aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Lehoux <martin@lehoux.net>2020-04-04 19:07:33 -0700
committerMartin Lehoux <martin@lehoux.net>2020-04-04 19:07:33 -0700
commitba7682a761343302fd65f315b0406abc470c482f (patch)
treefc801fa01510ed5d9205e0b254147d1366601087
parent3eebd8a349152726808ce83eaec1e83f53eb55e4 (diff)
feat - RandomInUnitSphere
-rw-r--r--vec3d.go14
1 files changed, 13 insertions, 1 deletions
diff --git a/vec3d.go b/vec3d.go
index a94ebbd..fcf5b1c 100644
--- a/vec3d.go
+++ b/vec3d.go
@@ -1,6 +1,9 @@
package main
-import "math"
+import (
+ "math"
+ "math/rand"
+)
type Vec3D struct {
x, y, z float64
@@ -45,3 +48,12 @@ func CrossProduct(vec1 Vec3D, vec2 Vec3D) Vec3D {
vec1.x*vec2.y - vec1.y*vec2.x,
}
}
+
+func RandomInUnitSphere() Vec3D {
+ for {
+ point := Vec3D{2*rand.Float64() - 1, 2*rand.Float64() - 1, 2*rand.Float64() - 1}
+ if point.Length() < 1.0 {
+ return point
+ }
+ }
+}