private static GridRelationShip RelationRectAndPanel(GridRect rect, IEnumerable <GridSegment> segments) { if (RelationSegmentAndPanel(new GridSegment(rect.Point1, rect.Point2), segments) == GridRelationShip.Intersect || RelationSegmentAndPanel(new GridSegment(rect.Point2, rect.Point3), segments) == GridRelationShip.Intersect || RelationSegmentAndPanel(new GridSegment(rect.Point3, rect.Point4), segments) == GridRelationShip.Intersect || RelationSegmentAndPanel(new GridSegment(rect.Point4, rect.Point1), segments) == GridRelationShip.Intersect) { return(GridRelationShip.Intersect); } var relationship1 = RelationPointAndPanel(rect.Point1, segments); var relationship2 = RelationPointAndPanel(rect.Point2, segments); var relationship3 = RelationPointAndPanel(rect.Point3, segments); var relationship4 = RelationPointAndPanel(rect.Point4, segments); if (relationship1 == GridRelationShip.None && relationship2 == GridRelationShip.None && relationship3 == GridRelationShip.None && relationship4 == GridRelationShip.None) { return(GridRelationShip.None); } else if (relationship1 == GridRelationShip.None || relationship2 == GridRelationShip.None || relationship3 == GridRelationShip.None || relationship4 == GridRelationShip.None) { return(GridRelationShip.Intersect); } return(GridRelationShip.Within); }
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 RelationPointAndRect(GridPoint point, GridRect rect) { if (point.X > rect.Point1.X && point.X < rect.Point4.X && point.Y > rect.Point1.Y && point.Y < rect.Point4.Y) { return(GridRelationShip.Within); } // TODO: 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 RelationRectAndPanel(GridRect rect, IEnumerable<GridSegment> segments) { if (RelationSegmentAndPanel(new GridSegment(rect.Point1, rect.Point2), segments) == GridRelationShip.Intersect || RelationSegmentAndPanel(new GridSegment(rect.Point2, rect.Point3), segments) == GridRelationShip.Intersect || RelationSegmentAndPanel(new GridSegment(rect.Point3, rect.Point4), segments) == GridRelationShip.Intersect || RelationSegmentAndPanel(new GridSegment(rect.Point4, rect.Point1), segments) == GridRelationShip.Intersect) { return GridRelationShip.Intersect; } var relationship1 = RelationPointAndPanel(rect.Point1, segments); var relationship2 = RelationPointAndPanel(rect.Point2, segments); var relationship3 = RelationPointAndPanel(rect.Point3, segments); var relationship4 = RelationPointAndPanel(rect.Point4, segments); if (relationship1 == GridRelationShip.None && relationship2 == GridRelationShip.None && relationship3 == GridRelationShip.None && relationship4 == GridRelationShip.None) { return GridRelationShip.None; } else if (relationship1 == GridRelationShip.None || relationship2 == GridRelationShip.None || relationship3 == GridRelationShip.None || relationship4 == GridRelationShip.None) { return GridRelationShip.Intersect; } return GridRelationShip.Within; }
private static GridRelationShip RelationPointAndRect(GridPoint point, GridRect rect) { if (point.X > rect.Point1.X && point.X < rect.Point4.X && point.Y > rect.Point1.Y && point.Y < rect.Point4.Y) { return GridRelationShip.Within; } // TODO: 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)); } } } } } }