bool IsFree(MyObject obj, int x, int y) { Rect myRectangleObj = new Rect(); myRectangleObj.Location = new Point(x, y); myRectangleObj.Size = new Size(obj.Width, obj.Height); foreach (var item in area.LMyObject) { Rect myRectangle = new Rect(); // The Location property specifies the coordinates of the upper left-hand // corner of the rectangle. myRectangle.Location = new Point(item.X, item.Y); myRectangle.Size = new Size(item.Width, item.Height); //bool flag = false; //if ( // ((x <= item.X + item.Width && x >= item.X) || (x + obj.Width >= item.X && x + obj.Width <= item.X + item.Width)) // && // ((y <= item.Y + item.Height && y >= item.Y) || (y + obj.Height >= item.Y && y + obj.Height <= item.Y + item.Height)) // ) // return false; //if (x == item.X && y == item.Y || x + obj.Width == item.X + item.Width && y + obj.Height == item.Y + item.Height) // return false; if (myRectangle.IntersectsWith(myRectangleObj) || (obj.Width + x) > (Area.Width) || (obj.Height + y) > (Area.Height)) return false; } return true; }
private int getRegion(MyObject obj) { Rect myRectangleObj = new Rect(); myRectangleObj.Location = new Point(obj.X, obj.Y); myRectangleObj.Size = new Size(obj.Width, obj.Height); List<TempRect> Sr = new List<TempRect>(); // лист площадей пересечений foreach (var item in area.LReg) { Rect myRectangle = new Rect(); myRectangle.Location = new Point(item.X, item.Y); myRectangle.Size = new Size(Region.Width, Region.Height); Rect temp = Rect.Intersect(myRectangleObj, myRectangle); if (!temp.IsEmpty) { TempRect tr = new TempRect(); tr.Sr = temp.Height * temp.Width; tr.Number = item.Number; Sr.Add(tr); } } try { return Sr.FirstOrDefault(p => p.Sr == Sr.Max(pr => pr.Sr)).Number; } catch { return 0; } }
private void DrawMyObj(MyObject obj) { Rectangle rectangle = new Rectangle(); rectangle.Height = obj.Height; rectangle.Width = obj.Width; rectangle.Stroke = new SolidColorBrush(Colors.Black); rectangle.StrokeThickness = 0.2; rectangle.Fill = new SolidColorBrush(Colors.Yellow); Canvas.SetLeft(rectangle, obj.X); Canvas.SetTop(rectangle, obj.Y); CanvasArea.Children.Add(rectangle); }
public MyObject NewObject(int Width, int Height) { MyObject obj = new MyObject(); obj.Width = Width; obj.Height = Height; if (setСoordinates(obj) != null) { obj.region = new Region(getRegion(obj)); return obj; } else return null; }
MyObject setСoordinates(MyObject obj) { if (area.LMyObject.Count == 0) { obj.X = 0; obj.Y = 0; area.LMyObject.Add(obj); return obj; } if (obj.Width >= obj.Height) { for (int i = 0; i < Area.Height; i++) { for (int j = 0; j < Area.Width; j++) { if (IsFree(obj, j, i)) { obj.X = j; obj.Y = i; area.LMyObject.Add(obj); return obj; } } } } else { for (int i = 0; i < Area.Width; i++) { for (int j = 0; j < Area.Height; j++) { if (IsFree(obj, i, j)) { obj.X = i; obj.Y = j; area.LMyObject.Add(obj); return obj; } } } } return null; //if (obj.Width >= obj.Height) //{ // foreach (var item in area.LMyObject) // { // int dx = item.X + item.Width; // int dy = item.Y; // if (IsFree(obj, dx, dy)) // { // obj.X = dx; // obj.Y = dy; // area.LMyObject.Add(obj); // return obj; // } // } // foreach (var item in area.LMyObject) // { // int dx = item.X; // int dy = item.Y + item.Height; // if (IsFree(obj, dx, dy)) // { // obj.X = dx; // obj.Y = dy; // area.LMyObject.Add(obj); // return obj; // } // } //} //else //{ // foreach (var item in area.LMyObject) // { // int dx = item.X; // int dy = item.Y + item.Height; // if (IsFree(obj, dx, dy)) // { // obj.X = dx; // obj.Y = dy; // area.LMyObject.Add(obj); // return obj; // } // } // foreach (var item in area.LMyObject) // { // int dx = item.X + item.Width; // int dy = item.Y; // if (IsFree(obj, dx, dy)) // { // obj.X = dx; // obj.Y = dy; // area.LMyObject.Add(obj); // return obj; // } // } //} //return null; }