Perimeter Scan Formulae explained:
 
The most obvious way to check if an enemy is next you is to use the "Scan Perimeter" command. However, this scan plus the check to find out what was scanned take four subclicks. A quicker way is to use the #enemy_x and #enemy_y system variables, along with your current position to see whether an enemy is next to you. The usual formula is (#x_pos - #enemy_x)2 + (#y_pos - #enemy_y)2 which gives a number less than 3 when an enemy is directly next to you. Example. The drawback is that it only detects uncloaked enemies.