private static void GetPolygonLevel1GridIndex(List <GridIndex> result, GridSegment region, IList <GridPoint> gridPoints, IEnumerable <GridSegment> segments) { var beginX = Math.Floor(region.X1 / GridConfig.Level1XCellSpan) * GridConfig.Level1XCellSpan; var beginY = Math.Floor(region.Y1 / GridConfig.Level1YCellSpan) * GridConfig.Level1YCellSpan; var endX = Math.Floor(region.X2 / GridConfig.Level1XCellSpan) * GridConfig.Level1XCellSpan; var endY = Math.Floor(region.Y2 / GridConfig.Level1YCellSpan) * GridConfig.Level1YCellSpan; var row = (int)Math.Round((endY - beginY) / GridConfig.Level1YCellSpan) + 1; var column = (int)Math.Round((endX - beginX) / GridConfig.Level1XCellSpan) + 1; if (beginX == endX && beginY == endY) { // 只占用顶级一个格子 GetPolygonLevel2GridIndex(result, region, gridPoints, segments); } else { for (int i = 0; i < column; i++) { var x = beginX + i * GridConfig.Level1XCellSpan; for (int j = 0; j < row; j++) { var y = beginY + j * GridConfig.Level1YCellSpan; var gridRect = new GridRect(x, y, GridConfig.Level1XCellSpan, GridConfig.Level1YCellSpan); if (gridPoints.Any(p => RelationPointAndRect(p, gridRect) == GridRelationShip.Within)) { // 多边形顶点所在的格子 // 不能压到边界,否则会判断过界,所以边界需要退一点 // Intersect GetPolygonLevel2GridIndex(result, new GridSegment(gridRect.Point1.X, gridRect.Point1.Y, gridRect.Point4.X - 0.5d * GridConfig.Level4XCellSpan, gridRect.Point4.Y - 0.5d * GridConfig.Level4YCellSpan), gridPoints, segments); } else { var relationship = RelationRectAndPanel(gridRect, segments); if (relationship == GridRelationShip.Within) { // 多边形背部完全包含的顶级格子,无需再分 //Within result.Add(new GridIndex() { Index1 = new GridPoint(x + 0.5d * GridConfig.Level1XCellSpan, y + 0.5d * GridConfig.Level1YCellSpan) .TranformGridIndex( GridConfig.LEVEL1_X_GRID, GridConfig.LEVEL1_Y_GRID, GridConfig.Level1XCellSpan, GridConfig.Level1YCellSpan) }); } else if (relationship == GridRelationShip.Intersect) { // 多边形与格子相交, 继续再分 // 不能压到边界,否则会判断过界,所以边界需要退一点 // Intersect GetPolygonLevel2GridIndex(result, new GridSegment(gridRect.Point1.X, gridRect.Point1.Y, gridRect.Point4.X - 0.5d * GridConfig.Level4XCellSpan, gridRect.Point4.Y - 0.5d * GridConfig.Level4YCellSpan), gridPoints, segments); } } } } } }
private static double GetX(double y, GridSegment segment) { if (segment.X1 == segment.X2) { return(segment.X1); } return((y - segment.Y1) * (segment.X1 - segment.X2) / (segment.Y1 - segment.Y2) + segment.X1); }
private static GridRelationShip RelationSegmentAndPanel(GridSegment segment, IEnumerable <GridSegment> segments) { if (segments.Any(s => RelationSegmentAndSegment(segment, s) == GridRelationShip.Intersect)) { return(GridRelationShip.Intersect); } return(GridRelationShip.None); }
private static GridRelationShip RelationSegmentAndSegment(GridSegment segment1, GridSegment segment2) { var vector1 = GetVector(segment1.Point1, segment2.Point1); var vector2 = GetVector(segment1.Point2, segment2.Point1); var vector3 = GetVector(segment2.Point2, segment2.Point1); var vector4 = GetVector(segment2.Point1, segment1.Point1); var vector5 = GetVector(segment2.Point2, segment1.Point1); var vector6 = GetVector(segment1.Point2, segment1.Point1); if (CrossMul(vector1, vector3) * CrossMul(vector2, vector3) <= 0 && CrossMul(vector4, vector6) * CrossMul(vector5, vector6) <= 0) { return(GridRelationShip.Intersect); } return(GridRelationShip.None); }
private static void GetPolygonLevel4GridIndex(List <GridIndex> result, GridSegment region, IList <GridPoint> gridPoints, IEnumerable <GridSegment> segments) { var beginX = Math.Floor(region.X1 / GridConfig.Level4XCellSpan) * GridConfig.Level4XCellSpan; var beginY = Math.Floor(region.Y1 / GridConfig.Level4YCellSpan) * GridConfig.Level4YCellSpan; var endX = Math.Floor(region.X2 / GridConfig.Level4XCellSpan) * GridConfig.Level4XCellSpan; var endY = Math.Floor(region.Y2 / GridConfig.Level4YCellSpan) * GridConfig.Level4YCellSpan; var row = (int)Math.Round((endY - beginY) / GridConfig.Level4YCellSpan) + 1; var column = (int)Math.Round((endX - beginX) / GridConfig.Level4XCellSpan) + 1; if (beginX == endX && beginY == endY) { // 只占用四级一个格子,无需再分 var point = new GridPoint(beginX, beginY); result.Add(GetPointGridIndex(point)); } else { for (int i = 0; i < column; i++) { var x = beginX + i * GridConfig.Level4XCellSpan; for (int j = 0; j < row; j++) { var y = beginY + j * GridConfig.Level4YCellSpan; var gridRect = new GridRect(x, y, GridConfig.Level4XCellSpan, GridConfig.Level4YCellSpan); var point = new GridPoint(x + 0.5d * GridConfig.Level4XCellSpan, y + 0.5d * GridConfig.Level4YCellSpan); if (gridPoints.Any(p => RelationPointAndRect(p, gridRect) == GridRelationShip.Within)) { result.Add(GetPointGridIndex(point)); } else { var relationship = RelationRectAndPanel(gridRect, segments); if (relationship != GridRelationShip.None) { if (relationship == GridRelationShip.Within) { } result.Add(GetPointGridIndex(point)); } } } } } }
private static void GetPolygonLevel1GridIndex(List<GridIndex> result, GridSegment region, IList<GridPoint> gridPoints, IEnumerable<GridSegment> segments) { var beginX = Math.Floor(region.X1 / GridConfig.Level1XCellSpan) * GridConfig.Level1XCellSpan; var beginY = Math.Floor(region.Y1 / GridConfig.Level1YCellSpan) * GridConfig.Level1YCellSpan; var endX = Math.Floor(region.X2 / GridConfig.Level1XCellSpan) * GridConfig.Level1XCellSpan; var endY = Math.Floor(region.Y2 / GridConfig.Level1YCellSpan) * GridConfig.Level1YCellSpan; var row = (int)Math.Round((endY - beginY) / GridConfig.Level1YCellSpan) + 1; var column = (int)Math.Round((endX - beginX) / GridConfig.Level1XCellSpan) + 1; if (beginX == endX && beginY == endY) { // 只占用顶级一个格子 GetPolygonLevel2GridIndex(result, region, gridPoints, segments); } else { for (int i = 0; i < column; i++) { var x = beginX + i * GridConfig.Level1XCellSpan; for (int j = 0; j < row; j++) { var y = beginY + j * GridConfig.Level1YCellSpan; var gridRect = new GridRect(x, y, GridConfig.Level1XCellSpan, GridConfig.Level1YCellSpan); if (gridPoints.Any(p => RelationPointAndRect(p, gridRect) == GridRelationShip.Within)) { // 多边形顶点所在的格子 // 不能压到边界,否则会判断过界,所以边界需要退一点 // Intersect GetPolygonLevel2GridIndex(result, new GridSegment(gridRect.Point1.X, gridRect.Point1.Y, gridRect.Point4.X - 0.5d * GridConfig.Level4XCellSpan, gridRect.Point4.Y - 0.5d * GridConfig.Level4YCellSpan), gridPoints, segments); } else { var relationship = RelationRectAndPanel(gridRect, segments); if (relationship == GridRelationShip.Within) { // 多边形背部完全包含的顶级格子,无需再分 //Within result.Add(new GridIndex() { Index1 = new GridPoint(x + 0.5d * GridConfig.Level1XCellSpan, y + 0.5d * GridConfig.Level1YCellSpan) .TranformGridIndex( GridConfig.LEVEL1_X_GRID, GridConfig.LEVEL1_Y_GRID, GridConfig.Level1XCellSpan, GridConfig.Level1YCellSpan) }); } else if (relationship == GridRelationShip.Intersect) { // 多边形与格子相交, 继续再分 // 不能压到边界,否则会判断过界,所以边界需要退一点 // Intersect GetPolygonLevel2GridIndex(result, new GridSegment(gridRect.Point1.X, gridRect.Point1.Y, gridRect.Point4.X - 0.5d * GridConfig.Level4XCellSpan, gridRect.Point4.Y - 0.5d * GridConfig.Level4YCellSpan), gridPoints, segments); } } } } } }
private static GridRelationShip RelationSegmentAndSegment(GridSegment segment1, GridSegment segment2) { var vector1 = GetVector(segment1.Point1, segment2.Point1); var vector2 = GetVector(segment1.Point2, segment2.Point1); var vector3 = GetVector(segment2.Point2, segment2.Point1); var vector4 = GetVector(segment2.Point1, segment1.Point1); var vector5 = GetVector(segment2.Point2, segment1.Point1); var vector6 = GetVector(segment1.Point2, segment1.Point1); if(CrossMul(vector1, vector3) * CrossMul(vector2, vector3) <=0 && CrossMul(vector4, vector6) * CrossMul(vector5, vector6) <=0) { return GridRelationShip.Intersect; } return GridRelationShip.None; }
private static GridRelationShip RelationSegmentAndPanel(GridSegment segment, IEnumerable<GridSegment> segments) { if(segments.Any(s => RelationSegmentAndSegment(segment, s) == GridRelationShip.Intersect)) { return GridRelationShip.Intersect; } return GridRelationShip.None; }
private static double GetX(double y, GridSegment segment) { if (segment.X1 == segment.X2) { return segment.X1; } return (y - segment.Y1) * (segment.X1 - segment.X2) / (segment.Y1 - segment.Y2) + segment.X1; }
private static void GetPolygonLevel4GridIndex(List<GridIndex> result, GridSegment region, IList<GridPoint> gridPoints, IEnumerable<GridSegment> segments) { var beginX = Math.Floor(region.X1 / GridConfig.Level4XCellSpan) * GridConfig.Level4XCellSpan; var beginY = Math.Floor(region.Y1 / GridConfig.Level4YCellSpan) * GridConfig.Level4YCellSpan; var endX = Math.Floor(region.X2 / GridConfig.Level4XCellSpan) * GridConfig.Level4XCellSpan; var endY = Math.Floor(region.Y2 / GridConfig.Level4YCellSpan) * GridConfig.Level4YCellSpan; var row = (int)Math.Round((endY - beginY) / GridConfig.Level4YCellSpan) + 1; var column = (int)Math.Round((endX - beginX) / GridConfig.Level4XCellSpan) + 1; if (beginX == endX && beginY == endY) { // 只占用四级一个格子,无需再分 var point = new GridPoint(beginX, beginY); result.Add(GetPointGridIndex(point)); } else { for (int i = 0; i < column; i++) { var x = beginX + i * GridConfig.Level4XCellSpan; for (int j = 0; j < row; j++) { var y = beginY + j * GridConfig.Level4YCellSpan; var gridRect = new GridRect(x, y, GridConfig.Level4XCellSpan, GridConfig.Level4YCellSpan); var point = new GridPoint(x + 0.5d * GridConfig.Level4XCellSpan, y + 0.5d * GridConfig.Level4YCellSpan); if (gridPoints.Any(p => RelationPointAndRect(p, gridRect) == GridRelationShip.Within)) { result.Add(GetPointGridIndex(point)); } else { var relationship = RelationRectAndPanel(gridRect, segments); if (relationship != GridRelationShip.None) { if (relationship == GridRelationShip.Within) { } result.Add(GetPointGridIndex(point)); } } } } } }