// sets the coordinates to the position given without linking a reference public void SetToPosition(Position position) { this.edgeLeftUp.x = position.edgeLeftUp.x; this.edgeLeftUp.y = position.edgeLeftUp.y; this.edgeRightDown.x = position.edgeRightDown.x; this.edgeRightDown.y = position.edgeRightDown.y; }
// contains the numbers of the already existing rects on the board that have a collision with the rect which should // be placed. the number means the position in the list (rectList) of the board public CollisionBundle(Position position, List<int> CollectionListNr) { this.position = new Position(); this.position.edgeLeftUp = new MyPoint(); this.position.edgeRightDown = new MyPoint(); this.position.edgeLeftUp.x = position.edgeLeftUp.x; this.position.edgeLeftUp.y = position.edgeLeftUp.y; this.position.edgeRightDown.x = position.edgeRightDown.x; this.position.edgeRightDown.y = position.edgeRightDown.y; }
// gets the best position for the rect on the board // the rect given to this method does not have to be initialized on 0/0 public void GetValidPositions(List<Rect> rectList, Rect rect, Boolean evolution) { // position = position of rect 0/0 Position position = new Position(); position.edgeLeftUp.x = 0; position.edgeLeftUp.y = rect.height; position.edgeRightDown.x = rect.width; position.edgeRightDown.y = 0; // largest part of this method, recursive ManagePosition(rectList, rect, position, evolution); }
public Boolean IsEqualPositionAs(Position position) { if(position.edgeLeftUp.x == this.edgeLeftUp.x && position.edgeLeftUp.y == this.edgeLeftUp.y && position.edgeRightDown.x == this.edgeRightDown.x && position.edgeRightDown.y == this.edgeRightDown.y) { return true; } else { return false; } }
// one step for a single position public void ManagePosition(List<Rect> rectList, Rect rect, Position position, Boolean evolution) { Base global = Base.GetInstance(); // 1.1 position out of borders? (boardHeight<rectY etc.) // TODO: general board width/height not set, therefore using height/width of the first board in the solution if (position.edgeLeftUp.y > global.solution.BoardList[0].height || position.edgeRightDown.x > global.solution.BoardList[0].width) { return; } // 1.2 position already managed? (already tried in another recursive call) for(int i = 0; i < global.positionsManaged.Count; i++) { // if yes, stop this method if (position.IsEqualPositionAs(global.positionsManaged[i])) { return; } } // if no (then the method reaches this point), add to positions managed global.positionsManaged.Add(position); // 2. collision? Collision collision = new Collision(); Rect rectCopy = new Rect(); rectCopy.edgeLeftUp.x = position.edgeLeftUp.x; rectCopy.edgeLeftUp.y = position.edgeLeftUp.y; rectCopy.edgeRightDown.x = position.edgeRightDown.x; rectCopy.edgeRightDown.y = position.edgeRightDown.y; rectCopy.height = rect.height; rectCopy.width = rect.width; rectCopy.rectID = rect.rectID; List<int> collisionListNr = collision.GetCollisions(rectList, rectCopy); if(collisionListNr.Count == 0) { // 3. add to valid positions global.positionsValid.Add(position); if(!evolution && global.Verschnittoptimierung.radioButton_FirstFitFilling.Checked || global.selectedGreedy <= 8 ) { return; } } else { // for all collisions for (int i = 0; i < collisionListNr.Count; i++) { // move up and try again // 1. move up Position positionUp = new Position(); positionUp.SetToPosition(position); positionUp.edgeRightDown.y = rectList[collisionListNr[i]].edgeLeftUp.y; float height = position.edgeLeftUp.y - position.edgeRightDown.y; positionUp.edgeLeftUp.y = positionUp.edgeRightDown.y + height; // 2. try again this.ManagePosition(rectList, rect, positionUp, evolution); // move right and try again // 1. move right Position positionRight = new Position(); positionRight.SetToPosition(position); positionRight.edgeLeftUp.x = rectList[collisionListNr[i]].edgeRightDown.x; float width = position.edgeRightDown.x - position.edgeLeftUp.x; positionRight.edgeRightDown.x = positionRight.edgeLeftUp.x + width; // 2. try again this.ManagePosition(rectList, rect, positionRight, evolution); } } }