Ok I think I have got this,
For this problem instead of using squares i am going to use Pythagoras theorem which states. A=sqrt(B^2+C^2)
This will give the distance between each drone.
So in psudocode i would do this:
xDangerDrones=[]
yDangerBots=[]
Drones=[] // A list of all your drones
for drone in drones:
for drone2 in drones:
if(not (drone==drone2 and check if drone and drone 2 are in lists xDangerDrones and yDangerBots)):
distance=sqrt(abs(drone.x-drone2.x)^2+abs(drone.y-drone2.y)^2)
if distance>YOUR RADIUS HERE:
if (abs(drone.x-drone2.x)>abs(drone.y-drone2.y)):
if (drone.y>drone2.y)
yDangerBots.append(drone)
else:
yDangerBots.append(drone2)
else
if (drone.x>drone2.x)
xDangerBots.append(drone)
else:
xDangerBots.append(drone2)
for xDangerDrone in xDangerDrones:
drone move right
for yDangerDrone in yDangerDrones:
drone move up
Edit
Full Java code as promised:
Drone.java
import java.util.Random;
import java.util.UUID;
/**
* Created by Asim Poptani on 2/14/2017.
*/
public class Drone {
private UUID uuid;
private int positionX;
private int positionY;
Drone()
{
Random random=new Random();
uuid= UUID.randomUUID();
positionX=random.nextInt(100);
positionY=random.nextInt(100);
}
public UUID getUuid() {
return uuid;
}
public int getPositionX() {
return positionX;
}
public void setPositionX(int positionX) {
this.positionX = positionX;
}
public int getPositionY() {
return positionY;
}
public void setPositionY(int positionY) {
this.positionY = positionY;
}
}
And Main.java
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
final int emergencyRadius=5;
HashSet<Drone> moveUp = new HashSet<>();
HashSet<Drone> moveDown = new HashSet<>();
HashSet<Drone> moveLeft = new HashSet<>();
HashSet<Drone> moveRight = new HashSet<>();
HashSet<Drone> drones= new HashSet<>();
for (int generator=0;generator<10;generator++)
{
drones.add(new Drone());
}
for (Drone drone:drones)
{
for (Drone drone2:drones){
if (
!(
drone==drone2
||
moveUp.contains(drone)
||
moveUp.contains(drone2)
||
moveDown.contains(drone)
||
moveDown.contains(drone2)
||
moveLeft.contains(drone)
||
moveLeft.contains(drone2)
||
moveRight.contains(drone)
||
moveRight.contains(drone2)
))
{
double distance=Math.sqrt(Math.abs(drone.getPositionX()-drone2.getPositionX())^2+Math.abs(drone.getPositionY()-drone2.getPositionY())^2);
if (distance<emergencyRadius)
{
if(Math.abs(drone.getPositionX()-drone2.getPositionX())>Math.abs(drone.getPositionY()-drone2.getPositionY()))
{
if(drone.getPositionY()>drone2.getPositionY())
{
moveUp.add(drone);
moveDown.add(drone2);
}
else {
moveUp.add(drone2);
moveDown.add(drone);
}
}
else {
if(drone.getPositionX()>drone2.getPositionX())
{
moveLeft.add(drone);
moveRight.add(drone2);
}
else
{
moveLeft.add(drone2);
moveRight.add(drone);
}
}
}
}
}
}
for (Drone drone:moveUp)
{
System.out.print("\nmove up ");
System.out.print(drone.getUuid());
}
for (Drone drone:moveDown)
{
System.out.print("\nmove down");
System.out.print(drone.getUuid());
}
for (Drone drone:moveRight)
{
System.out.print("\nmove Right ");
System.out.print(drone.getUuid());
}
for (Drone drone:moveLeft)
{
System.out.print("\nmove left ");
System.out.print(drone.getUuid());
}
}
}