我现在的代码遇到了麻烦,我希望有人帮助我开发一个简单的或者至少提供一些想法(伪代码)。
基本上我有n个无人机,我想创建一个安全区域"他们周围。
所以我想如果drone2在drone1的安全区域内,我想留在外面。 但我想创建N个无人机(drone.length)
例如: 如果drone2从+ x侧接近drone1,我希望他留在位置(x + 1,y),这样他们就不会碰撞
代码:
For each Drone I have:
int Xmin
int Xmax
int Ymin
int Ymax
string ip (example "192.168.1.10"
Point position (x,y)
for(int i = 0 ; i<drone.length;i++)
{
//i think I need to check drone's IP to know the current drone being iterated
If(drone[i].position //is inside every drone[EXCEPT i]’s safe area
{
//debug: Which drone’s safe area is drone[i].position inside?
}
}
是的,有人能帮帮我吗?非常感谢你
答案 0 :(得分:-1)
好的我觉得我有这个,
对于这个问题而不是使用正方形,我将使用毕达哥拉斯定理。 A=sqrt(B^2+C^2)
这将给出每架无人机之间的距离。
所以在psudocode中我会这样做:
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
修改强>
承诺的完整Java代码:
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;
}
}
和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());
}
}
}