简体   繁体   English

C#-继续If语句

[英]C# - Continue If statement

I want to check if drone[i].position is inside every drone[EXCEPT i]'s safe area. 我想检查一下drone [i] .position是否在每个无人机[除了i]的安全区域之内。 Basically I dont want to check if drone[i].position is inside drone[i].safe_area. 基本上我不想检查drone [i] .position是否在drone [i] .safe_area内部。

Maybe I need another for loop or something? 也许我需要另一个for循环之类的东西?

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?
              }
}

You could use linq. 您可以使用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));
    }
}

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. 换句话说,您正在查看是否其他无人机的位置在Y无人机的安全区域内。 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. 请记住,我并不担心封装,因此您需要注意这一点,并且在构造等之后不允许操作SafeArea ,但这取决于您的需要。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM