def detect2DCollision(lineSegment, polygon): #(lineSegment:LineSegment) (polygon:Polygon) = # // Check characteristics of the shapes # assertGreaterEqualTo "sides" (float polygon.Length) "3 sides" 3.0 # http://stackoverflow.com/questions/12222700/determine-if-line-segment-is-inside-polygon # first, sort a polygon. Here, polygons are assumed to be sorted already # // Gather the sides for the polygon polygonSides = [] for i in range(len(polygon)): if i == len(polygon) - 1: endPoint = polygon[0] else: endPoint = polygon[i + 1] polygonSides.append(Spatial.LineSegment(polygon[i], endPoint)) axes = polygonSides axes.append(lineSegment) axes = [ Spatial.toUnitVector( Spatial.toNormalVector( Spatial.calculateLocationDifference(v.Location1, v.Location2))) for v in axes ] # // Check for separation between objects separationExists = False for axis in axes: polyProj = projectPolygonOntoAxis(polygon, axis) lineSegmentProj = projectSegmentOntoAxis(lineSegment, axis) #// Do gaps exist between max/min intervals? if polyProj.Min > lineSegmentProj.Max or lineSegmentProj.Min > polyProj.Max: separationExists = True return not separationExists
def aircraftInAirspace(airspace, previousPosition, currentPosition): lineSegment = Spatial.LineSegment(previousPosition, currentPosition) altitude = currentPosition.Feet # check whether it cross with the restrictedZone or turbulentZones crossRestricted = False crossTurbulent = False for res in airspace.RestrictedZones: # print res if CollisionDetection.detect3DCollision(lineSegment, altitude, res.Polyhedron): crossRestricted = True for turb in airspace.TurbulentZones: if CollisionDetection.detect3DCollision(lineSegment, altitude, res.Polyhedron): crossTurbulent = True return IntersectAirspace(crossRestricted, crossTurbulent)