C#-如果语句继续
我想检查无人机[I]的位置是否在每架无人机[除我]的安全区域内基本上我不想检查无人机[I]。位置在无人机[I]内。安全区域。 也许我需要另一个循环什么的 我看到像这样的东西C#-如果语句继续,c#,C#,我想检查无人机[I]的位置是否在每架无人机[除我]的安全区域内基本上我不想检查无人机[I]。位置在无人机[I]内。安全区域。 也许我需要另一个循环什么的 我看到像这样的东西 if(i== ) continue; 但是我能等于什么呢 代码: 对于我拥有的每一架无人机: 字符串ip(示例“192.168.1.10” 点位置(x,y) 点安全区 对于(int i=0;i您可以使用linq public class Drone { public int ID { get; set; }
if(i== ) continue;
但是我能等于什么呢
代码:
对于我拥有的每一架无人机:
字符串ip(示例“192.168.1.10”
点位置(x,y)
点安全区
对于(int i=0;i您可以使用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));
}
}
根据我对你的问题的理解,你基本上有一架无人机,并且该无人机有一个安全区域。现在你要检查一架无人机是否在另一架无人机的安全区域内
___________
/ \
/ \
/ --------`
| /
| Drone Y's Safe Area /___
| `
--------------------------`
换句话说,您正在查看另一无人机的位置是否在无人机Y的安全区域内。因此,您需要查找点是否在多边形内。您可以创建以下类:
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;
}
}
点(位置)如何位于其他点(安全区域)内?听起来像是(intj=0;j,
然后你的if
语句将是if(i==j)继续;
。我想简化。安全区域不是一个点。它是4个点。+x-x-y+y@juharr这不总是真的吗?当i递增时,j也会递增。@ScumbagSteve不,这个想法是,内循环将遍历外循环每个值的所有索引,因此i=0,然后j=0到n,然后i=1,j=0到n等等开启。我如何知道它进入了哪个无人机的安全区域?问题是我认为这对n架无人机不起作用。你在与无人机进行比较[0].ID.安全区域是一个简单的正方形。我只想加上和减去一个常数来创建一个安全的正方形区域。对我来说似乎有很多代码。我只想做一些事情,比如如果它在安全区域内,我想发送一个移动命令,让进入安全区域的无人机留在安全区域之外。比如x-1,y
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;
}
}
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();
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 );
}
}