Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. // RandomInSemiSphere is a function to find a ray for diffuse reflection in semisphere.
  2. //
  3. // Parameters:
  4. // normal - the normal.
  5. //
  6. // Returns:
  7. // the vector.
  8. //
  9. func RandomInSemiSphere(normal utils.Vector) utils.Vector {
  10. random1 := rand.Float64()
  11. random2 := rand.Float64()
  12. v1 := utils.Vector{Coordinates: []float64{random1 + normal.Coordinates[0], -1 * normal.Coordinates[1], -1 * normal.Coordinates[2]}}
  13. v1 = utils.NormalizeVector(&v1)
  14. if utils.DotProduct(&normal, &v1) < 0 { // cos
  15. random1 = -1 * random1
  16. }
  17. v2 := utils.Vector{Coordinates: []float64{-1 * normal.Coordinates[0], random2 + normal.Coordinates[1], -1 * normal.Coordinates[2]}}
  18. v2 = utils.NormalizeVector(&v2)
  19. if utils.DotProduct(&normal, &v2) < 0 {
  20. random2 = -1 * random2
  21. }
  22. resultingVector := utils.Vector{Coordinates: []float64{normal.Coordinates[0] + random1, normal.Coordinates[1] + random2, normal.Coordinates[2]}}
  23.  
  24. return resultingVector
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement