inside_bz_2d Function

public function inside_bz_2d(x, y, A, B, C, D)

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: x
integer, intent(in) :: y
integer, intent(in) :: A(2)
integer, intent(in) :: B(2)
integer, intent(in) :: C(2)
integer, intent(in) :: D(2)

Return Value logical


Contents

Source Code


Source Code

    logical function inside_bz_2d(x, y, A, B, C, D)
        ! function to check if a point (x,y) is inside our outside the
        ! rectangle indicated by the 3 points A,B,C,D
        ! the definition is to provide the points in this fashion:
        !  A -- D
        !  |    |
        !  B -- C
        ! in a circular fashion, otherwise it does not work, since it would
        ! be a different rectangle
        ! idea from:
        ! https://stackoverflow.com/questions/2752725/finding-whether-a-point-lies-inside-a-rectangle-or-not
        integer, intent(in) :: x, y, A(2), B(2), C(2), D(2)

        integer :: vertex(4, 2), edges(4, 2), R(4), S(4), T(4), U(4)
        inside_bz_2d = .false.

        vertex = transpose(reshape([A, B, C, D], [2, 4]))

        edges(1, :) = A - B
        edges(2, :) = B - C
        edges(3, :) = C - D
        edges(4, :) = D - A

        R = edges(:, 2)
        S = -edges(:, 1)
        T = -(R * vertex(:, 1) + S * vertex(:, 2))

        U = R * x + S * y + T

        if (all(U >= 0)) inside_bz_2d = .true.

    end function inside_bz_2d