0

I want to check if drone[i].position is inside every drone[EXCEPT i]’s safe area. Basically I dont want to check if drone[i].position is inside drone[i].safe_area.

Maybe I need another for loop or something?

I saw somethiing like

if(i== ) continue; 

but what do I equal to?

Code:

For each Drone I have:
string ip (example "192.168.1.10"
Point position (x,y)
Point Safe_area



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?
              }
}
  • how point (position) can be inside other point (safe area)? – Sergey Berezovskiy Feb 14 '17 at 00:03
  • Sounds like you'd have an inner loop `for (int j = 0; j < dron.Length; j++)` and then your `if` statement would be `if(i == j) continue;`. – juharr Feb 14 '17 at 00:04
  • I wanted to simplify. safe area is not a point. It's 4 points. +x -x -y +y –  Feb 14 '17 at 00:06
  • @juharr wouldnt that always true i == j? When i increment j will also increment. –  Feb 14 '17 at 00:08
  • @ScumbagSteve No, the idea is that the inner loop will go through all the indexes for each value of the outer loop, so i = 0 and then j = 0 to n then i = 1 and j = 0 to n and so on. – juharr Feb 14 '17 at 00:13

2 Answers2

0

You could use linq.

public class Drone
{
    public int ID { get; set; }

    public Point Position { get; set; }
    public bool IsInsideSafearea(Point point)
    {
        throw new NotImplementedException();
    }
}

public class Class1
{
    public void DoSomething()
    {
        var drones = new Drone[]
        {
            new Drone() {ID = 0, Position = new Point(1, 2) },
            new Drone() {ID = 1, Position = new Point(3, 4) },
            new Drone() {ID = 2, Position = new Point(4, 2) },
        };

        var myDrone = drones[0];

        bool result = drones
            .Where(d => d.ID != myDrone.ID)
            .All(d => d.IsInsideSafearea(myDrone.Position));
    }
}
Connell.O'Donnell
  • 3,603
  • 11
  • 27
  • 61
  • How do I know in which drone's safe area it entered? –  Feb 14 '17 at 00:16
  • The problem is I dont think this works for n drones. You're comparing to drones[0].ID. –  Feb 14 '17 at 00:17
0

From my understanding of your question, you basically have a drone and the drone has a safe area to be within. Now you want to check if a drone is within the safe area of another drone.

   ___________
  /           \
 /             \
/               --------`       
|                      /   
| Drone Y's Safe Area /___    
|                         `
--------------------------`

In other words, you are looking to see if another drone's position is within Drone Y's safe area. So you need to find if a point is inside a polygon. You can create a class this:

public class Drone {

  public Point Position { get; set; }

  public Point[] SafeArea { get; set; }

  public bool CheckIfDroneWithinMySafeArea(Drone drone) {
     return IsWithinPolygon( this.SafeArea, drone.Position );
  }

  public static bool IsWithinPolygon(Point[] poly, Point dronePosition) {

     bool isWithinPolygon = false;

     if( poly.Length < 3 ) {
        return isWithinPolygon;
     }

     var oldPoint = new
        Point( poly[ poly.Length - 1 ].X, poly[ poly.Length - 1 ].Y );

     Point p1, p2;
     for( int i = 0; i < poly.Length; i++ ) {

        var newPoint = new Point( poly[ i ].X, poly[ i ].Y );
        if( newPoint.X > oldPoint.X ) {
           p1 = oldPoint;
           p2 = newPoint;
        }
        else {
           p1 = newPoint;
           p2 = oldPoint;
        }

        if( ( newPoint.X < dronePosition.X ) == ( dronePosition.X <= oldPoint.X )
              && ( dronePosition.Y - ( long ) p1.Y ) * ( p2.X - p1.X )
              < ( p2.Y - ( long ) p1.Y ) * ( dronePosition.X - p1.X ) ) {
           isWithinPolygon = !isWithinPolygon;
        }


        oldPoint = newPoint;
     }


     return isWithinPolygon;
  }
}

And here is the usage:

var d1 = new Drone();
   // set d1's position and safe area

var drones = new List<Drone>();
// Add drones to above list

// Now check if any of the drones are within d1's safe area
var dronesWithinD1zSafeArea = drones.Where( x => d1.CheckIfDroneWithinMySafeArea( x ) ).ToList();

Keep in mind I did not worry about encapsulation so you need to take care of that and not allow SafeArea to be manipulated after construction etc but that depends on your needs.

I got the code for checking inside the polygon from here.

EDIT

In a comment to this answer you mentioned:

Safe area is a simple square.

The above code will work for any shape (triangle, rectangle). But for a rectagle, you can just use this code which is much shorter:

public class Drone {
   public System.Windows.Point Position { get; set; }
   public Rectangle SafeArea { get; set; }
   public bool CheckIfDroneWithinMySafeArea(Drone drone) {
   return this.SafeArea.Contains( drone.Position );
   }
}
Community
  • 1
  • 1
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • Safe area is a simple square. I just want to add and subtract a constant to create a safe square area. –  Feb 14 '17 at 00:56
  • Seems like a lot of code for me. I just wanted something like if it's inside a safe area I want to send a movement comand for the drone that entered the safe area to stay outside it. Something like x-1,y –  Feb 14 '17 at 01:00