hex_round Function

private pure function hex_round(xyz) result(qrs)

Round a triplet of reals to a proper hex object

this needs to preserve the q+r+s==0 condition

Arguments

Type IntentOptional Attributes Name
real(kind=8), intent(in) :: xyz(3)

Return Value type(hex)


Called by

proc~~hex_round~~CalledByGraph proc~hex_round hex_geometries::hex_round proc~hex_line hex_geometries::hex_line proc~hex_line->proc~hex_round

Source Code

   pure function hex_round(xyz) result(qrs)
      !! Round a triplet of reals to a proper hex object
      !! > this needs to preserve the q+r+s==0 condition
      real(8),intent(in)   :: xyz(3)
      type(hex)            :: qrs
      real(8)              :: x,y,z
      integer              :: q,r,s
      real(8)              :: dq,dr,ds
      ! Unpack
      x = xyz(1)
      y = xyz(2)
      z = xyz(3)
      ! Round xyz
      q = nint(x)
      r = nint(y)
      s = nint(z)
      ! Eval diffs
      dq = abs(x-q)
      dr = abs(x-r)
      ds = abs(x-s)
      ! Reset bigger diff
      if(dq > dr .and. dq > ds)then
         q = -r-s
      elseif(dr > ds)then
         r = -q-s
      else
         s = -q-r
      endif
      ! Repack
      qrs = hex(q,r,s) ! internal assertion :)
   end function hex_round