Mapping coordinates to interact with a 2D Interaction Box

173 views Asked by At

I want to detect whether or not a finger (or any object) is pointing at a certain box (on the screen) as shown in the image below.

The following code can be used to get the xy cordinates of the finger but what is the easiest way to define the interaction boxes and then map the cordinates to see if it matches any of the boxes on the outside of the screen.

I am using the latest version of the leap motion SDK (3.2.0) on Windows.

def on_frame(self, controller):
    app_width = 800
    app_height = 600
    # Get the most recent frame and report some basic information
    frame = controller.frame()

    pointable = frame.pointables.frontmost


    if pointable.is_valid:
        iBox = frame.interaction_box
        leapPoint = pointable.stabilized_tip_position
        normalizedPoint = iBox.normalize_point(leapPoint, False)

        app_x = normalizedPoint.x * app_width
        app_y = (1 - normalizedPoint.y) * app_height
        #The z-coordinate is not used

        print ("X: " + str(app_x) + " Y: " + str(app_y))

The output then looks like this:

X: 467.883825302 Y: 120.019626617
X: 487.480115891 Y: 141.106081009
X: 505.537891388 Y: 164.418339729
X: 522.712898254 Y: 189.527964592
X: 539.482307434 Y: 213.160014153
X: 556.220436096 Y: 233.744287491
X: 573.109865189 Y: 253.145456314

interaction area

1

There are 1 answers

1
Charles Ward On

First of all, this code won't tell you where the finger is pointing. It just tells you where the finger tip is located in space -- an important, but perhaps subtle, distinction.

Since you presumably have your boxes defined in screen coordinates, you could just check every box to see if the finger tip position is inside. Something like:

def check_in_box(box, point):
    if point.x > box.left and point.x < box.right 
       and point.y > box.top and point.y < box.bottom:

        return True

    else:
        return False

This assumes you have a suitable class to represent a 2D box.