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 );
}
}