public static unsafe Rectangle GetImageSaveRectangle(Surface surface) { RectanglePosition rectPos = new RectanglePosition() { Left = surface.Width, Top = surface.Height, Right = 0, Bottom = 0 }; // Search for top non-transparent pixel bool fPixelFound = false; for (int y = 0; y < surface.Height; y++) { if (ExpandImageRectangle(surface, y, 0, surface.Width, ref rectPos)) { fPixelFound = true; break; } } // Narrow down the other dimensions of the image rectangle if (fPixelFound) { // Search for bottom non-transparent pixel for (int y = surface.Height - 1; y > rectPos.Bottom; y--) { if (ExpandImageRectangle(surface, y, 0, surface.Width, ref rectPos)) { break; } } // Search for left and right non-transparent pixels. Because we // scan horizontally, we can't just break, but we can examine fewer // candidate pixels on the remaining rows. for (int y = rectPos.Top + 1; y < rectPos.Bottom; y++) { ExpandImageRectangle(surface, y, 0, rectPos.Left, ref rectPos); ExpandImageRectangle(surface, y, rectPos.Right + 1, surface.Width, ref rectPos); } } else { rectPos.Left = 0; rectPos.Top = 0; } if ((rectPos.Left < rectPos.Right) && (rectPos.Top < rectPos.Bottom)) { return new Rectangle(rectPos.Left, rectPos.Top, rectPos.Right - rectPos.Left + 1, rectPos.Bottom - rectPos.Top + 1); } return Rectangle.Empty; }
private void UpdateCoordinate(Point coordinate) { if (_firstPoint) { _xMin = coordinate.X; _yMin = coordinate.Y; _firstPoint = false; return; } // We have two points var position = new RectanglePosition(_xMin, _yMin, coordinate.X, coordinate.Y); AddData(position); _firstPoint = true; SetupForm(); }
async void CreateRectangleRectAsync(VGCore.Rect rect, VGCore.Layer layer, RectanglePosition rectanglePosition, RGBAssign rgbAssign, CancellationTokenSource cts) { VGCore.Shape shape = null; VGCore.Outline outline = null; VGCore.Fill fill = null; VGCore.Color color = null; try { shape = layer.CreateRectangleRect(rect); shape.PositionX = rectanglePosition.PositionX; shape.PositionY = rectanglePosition.PositionY; outline = shape.Outline; outline.Type = VGCore.cdrOutlineType.cdrNoOutline; fill = shape.Fill; color = fill.UniformColor; color.RGBAssign( rgbAssign.R, rgbAssign.G, rgbAssign.B ); } catch (OperationCanceledException) { await mainWindow.OutputText.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate() { mainWindow.OutputText.Text += "Операция была отменена пользователем!\n"; })); } catch (Exception ex) { await mainWindow.OutputText.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate() { mainWindow.OutputText.Text += $"Work is failed.\n{ex.Message}\n"; })); } finally { Marshal.ReleaseComObject(color); Marshal.ReleaseComObject(fill); Marshal.ReleaseComObject(outline); Marshal.ReleaseComObject(shape); } }
/// <summary> /// Check for non-transparent pixels in a row, or portion of a row. /// Expands the size of the image rectangle if any were found. /// </summary> /// <returns>True if non-transparent pixels were found, false otherwise.</returns> unsafe private static bool ExpandImageRectangle(Surface surface, int y, int xStart, int xEnd, ref RectanglePosition rectPos) { bool fPixelFound = false; ColorBgra* rowStart = surface.GetRowAddress(y); ColorBgra* pixel = rowStart + xStart; for (int x = xStart; x < xEnd; x++) { if (pixel->A > 0) { // Expand the rectangle to include the specified point. if (x < rectPos.Left) { rectPos.Left = x; } if (x > rectPos.Right) { rectPos.Right = x; } if (y < rectPos.Top) { rectPos.Top = y; } if (y > rectPos.Bottom) { rectPos.Bottom = y; } fPixelFound = true; } pixel++; } return fPixelFound; }
public static Rectangle GetRectangle(RectanglePosition rectanglePositions) { var rec = RectanglePosition.GetRectangle(rectanglePositions); return(rec); }
// Get room placement bool getRoomPlacement() { // Cast a ray from screen mouse position to 3d world space Ray cameraRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit cameraRayHit; // Check if the casted ray collides only floor, then highlight the rectangular area if (Physics.Raycast(cameraRay, out cameraRayHit, 1000, 1 << 8)) { // Move the hover tile to collison point Vector3 hitPoint = cameraRayHit.point; Vector2 mouseOverCell = getTilePoints(hitPoint); // Calculate x and y distances, cell corner position int xDist; int yDist; xDist = (int)(mouseOverCell.x - selectedCell.x); yDist = (int)(mouseOverCell.y - selectedCell.y); RectanglePosition selectedCellPos = getRectPosition(xDist, yDist); // Position the hover tile if (selectedCellPos == RectanglePosition.LeftBottom) { hoverTile.transform.position = new Vector3(selectedCell.x, 0F, selectedCell.y); } else if (selectedCellPos == RectanglePosition.RightBottom) { hoverTile.transform.position = new Vector3(selectedCell.x + (float)xDist, 0F, selectedCell.y); } else if (selectedCellPos == RectanglePosition.LeftTop) { hoverTile.transform.position = new Vector3(selectedCell.x, 0F, selectedCell.y + (float)yDist); } else { hoverTile.transform.position = new Vector3(selectedCell.x + (float)xDist, 0F, selectedCell.y + (float)yDist); } // Scale the hover tile float xD = Mathf.Abs((float)xDist) + 1F; float yD = Mathf.Abs((float)yDist) + 1F; hoverTile.transform.localScale = new Vector3(xD, 1F, yD); // Convert left bottom in vector two Vector2 leftBottomPosition = getTilePoints(hoverTile.transform.position); // Check if room purchase is valid if (isBlockSumZero(leftBottomPosition, xD, yD)) { // Change color of hover tile to green hoverTileRenderer.material.SetColor("_Color", new Color(0F, 1.0F, 0F, 0.25F)); // If mouse click, build the room if (Input.GetMouseButtonUp(0)) { // If room size is minimum 2x2 if (xD >= 2f && yD >= 2f) { Vector2 lt = new Vector2(leftBottomPosition.x, leftBottomPosition.y + yD); Vector2 rb = new Vector2(leftBottomPosition.x + xD, leftBottomPosition.y); Vector2 rt = new Vector2(leftBottomPosition.x + xD, leftBottomPosition.y + yD); Room room = new Room(selectedRoomType, leftBottomPosition, lt, rb, rt, horizontalWall, verticalWall, transform); rooms.Add(room); // Update the floor map and room map setBlockValueFloorMap(leftBottomPosition, xD, yD, 2); setBlockValueRoomMap(leftBottomPosition, xD, yD, room.roomId); return(true); } else { return(false); } } } else { // Change color of hover tile to red hoverTileRenderer.material.SetColor("_Color", new Color(1.0F, 0F, 0F, 0.25F)); return(false); } } return(false); }