[request] want as feature: get the nearest point/region/image to a given point/region/image

Bug #1355017 reported by RaiMan
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
SikuliX
In Progress
Medium
RaiMan

Bug Description

I ran into a major problem while making a script. At present sikuli randomly selects the pictures , i wanted sikuli to select the recognized picture closest to the mouse since there are more than 5 matches , at least on the screen.but since i am new to this i am hopelessly lost ! , how do i get sikuli to select the the picture closest to the mouse location when there are multiple matches. thanks :)
is it possible to achieve this by setting a region at the mouse location every time ?

Revision history for this message
RaiMan (raimund-hocke) wrote :

This is a more general solution:

import math

# used by sorted, to calculate the distance to refLocation
# the sort function uses this value for sorting the list elements
def by_nearest(match):
  x = math.fabs(match.getCenter().x - refLocation.x)
  y = math.fabs(match.getCenter().y - refLocation.y)
  distance = int(math.sqrt(x * x + y * y))
  return distance

img = "some-image.png" # the image to look for

refLocation = Env.getMouseLocation() # the reference point

# use findAll(), to find all matches on screen
# and create the sorted list
sortedMatches = sorted(findAll(img), key=by_nearest)

# list contains the elements sorted by distance in ascending order
# hence the first element is the one we are looking for
sortedMatches[0].highlight(2)

RaiMan (raimund-hocke)
Changed in sikuli:
status: New → In Progress
importance: Undecided → Medium
assignee: nobody → RaiMan (raimund-hocke)
milestone: none → 1.2.0
RaiMan (raimund-hocke)
Changed in sikuli:
milestone: 2.0.0 → 2.1.0
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.