public override MyRectangle GenerarRectanguloAsociado() { var Xs = from punto in puntos select punto.X; var Ys = from punto in puntos select punto.Y; var Xminima = Xs.Min(); var Yminima = Ys.Min(); var puntosfinales = from punto in puntos select new PointF(punto.X - Xminima, punto.Y - Yminima); var XsNuevos = from punto in puntosfinales select punto.X; var YsNuevos = from punto in puntosfinales select punto.Y; var rectwidth = XsNuevos.Max(); var rectheight = YsNuevos.Max(); var nuevoPoligono = new Poligono(puntosfinales.ToList()); nuevoPoligono.color = this.color; var rectangulo = new MyRectangle(rectwidth, rectheight, nuevoPoligono); return(rectangulo); }
private static int Comparer_By_Max(MyRectangle rct1, MyRectangle rct2) { var max_rct1 = rct1.Max_Width_Height(); var max_rct2 = rct2.Max_Width_Height(); if (max_rct1 > max_rct2) { return(1); } else if (max_rct1 < max_rct2) { return(-1); } else { return(0); } }
private static int Comparer_By_Perimeter(MyRectangle rct1, MyRectangle rct2) { var perimeter_rct1 = rct1.Perimeter(); var perimeter_rct2 = rct2.Perimeter(); if (perimeter_rct1 > perimeter_rct2) { return(1); } else if (perimeter_rct1 < perimeter_rct2) { return(-1); } else { return(0); } }
private static int Comparer_By_Area(MyRectangle rct1, MyRectangle rct2) { var area_rct1 = rct1.Area(); var area_rct2 = rct2.Area(); if (area_rct1 > area_rct2) { return(1); } else if (area_rct1 < area_rct2) { return(-1); } else { return(0); } }
private static int Comparer_By_Diag(MyRectangle rct1, MyRectangle rct2) { var diag_rct1 = rct1.Diag_Width_Height(); var diag_rct2 = rct2.Diag_Width_Height(); if (diag_rct1 > diag_rct2) { return(1); } else if (diag_rct1 < diag_rct2) { return(-1); } else { return(0); } }
public static List <List <MyRectangle> > GetRectanglesOrdered(List <MyRectangle> rectangles) { List <List <MyRectangle> > rectanglesToReturn = new List <List <MyRectangle> >(); var rectanglesArray = new MyRectangle[rectangles.Count]; MyRectangle.Sort_By_Area(rectangles); rectangles.CopyTo(rectanglesArray); rectanglesToReturn.Add(rectanglesArray.ToList()); MyRectangle.Sort_By_Perimeter(rectangles); rectangles.CopyTo(rectanglesArray); rectanglesToReturn.Add(rectanglesArray.ToList()); MyRectangle.Sort_By_Diag(rectangles); rectangles.CopyTo(rectanglesArray); rectanglesToReturn.Add(rectanglesArray.ToList()); MyRectangle.Sort_By_Max(rectangles); rectangles.CopyTo(rectanglesArray); rectanglesToReturn.Add(rectanglesArray.ToList()); return(rectanglesToReturn); }
private Dictionary <int, dynamic> LeftPlacementEvaluation(List <Tuple <double, double, double> > skyline, Tuple <double, double> endpoint, double spread, MyRectangle rectangle, List <MyRectangle> rectangles) { var evaluation = new Dictionary <int, dynamic>(); evaluation[1] = false; if (Utils.CanPlace(skyline, this.Width, this.Height, endpoint, rectangle)) { evaluation[-2] = rectangle; evaluation[-1] = endpoint; evaluation[0] = UpdateStep1(skyline, endpoint, rectangle); evaluation[1] = Utils.CalcSpread(evaluation[0], spread); if (evaluation[1]) { evaluation[2] = false; evaluation[3] = Utils.CalcLocalWaste(skyline, this.Width, this.Height, rectangle, rectangles, endpoint); evaluation[4] = Utils.FitnessNumber(skyline, this.Height, rectangle, endpoint); } } return(evaluation); }
public static int FitnessNumber(List <Tuple <double, double, double> > skyline, double skylineHeight, MyRectangle rectangle, Tuple <double, double> position) { int fitnessNumber = 0; int i = 0; while (skyline[i].Item1 != position.Item1 || skyline[i].Item3 != position.Item2) { i++; } // S if (skyline[i].Item2 - skyline[i].Item1 == rectangle.Width) { fitnessNumber++; } // W if (i - 1 > 0 && skyline[i - 1].Item3 - skyline[i].Item3 == rectangle.Height) { fitnessNumber++; } // E if (i + 1 < skyline.Count && skyline[i].Item1 + rectangle.Width == skyline[i + 1].Item2 && skyline[i + 1].Item3 - skyline[i].Item3 == rectangle.Height) { fitnessNumber++; } // N if (skyline[i].Item3 + rectangle.Height == skylineHeight) { fitnessNumber++; } return(fitnessNumber); }
private List <Tuple <double, double, double> > UpdateStep1(List <Tuple <double, double, double> > skyline, Tuple <double, double> endpoint, MyRectangle rectangle) { var new_skyline = new List <Tuple <double, double, double> >(); int i = 0; while (skyline[i].Item1 != endpoint.Item1 || skyline[i].Item3 != endpoint.Item2) { new_skyline.Add(skyline[i]); i++; } new_skyline.Add(new Tuple <double, double, double>(skyline[i].Item1, skyline[i].Item1 + rectangle.Width, skyline[i].Item3 + rectangle.Height)); if (rectangle.Width < skyline[i].Item2 - skyline[i].Item1) { new_skyline.Add(new Tuple <double, double, double>(skyline[i].Item1 + rectangle.Width, skyline[i].Item2, skyline[i].Item3)); i++; } else if (rectangle.Width > skyline[i].Item2 - skyline[i].Item1) { int j = 0; while (i + j < skyline.Count && new_skyline[i].Item2 > skyline[i + j].Item2) { j++; } if (i + j < skyline.Count) { new_skyline.Add(new Tuple <double, double, double>(new_skyline[i].Item2, skyline[i + j].Item2, skyline[i + j].Item3)); } i += j + 1; } else { i++; } for (; i < skyline.Count; i++) { new_skyline.Add(skyline[i]); } skyline = new_skyline; new_skyline = new List <Tuple <double, double, double> >(); for (int k = 0; k < skyline.Count;) { int j = 1; while (j + k < skyline.Count && skyline[k].Item3 == skyline[k + j].Item3) { j++; } new_skyline.Add(new Tuple <double, double, double>(skyline[k].Item1, skyline[k + j - 1].Item2, skyline[k].Item3)); k += j; } return(new_skyline); }
public static double CalcLocalWaste(List <Tuple <double, double, double> > skyline, double skylineWidth, double skylineHeight, MyRectangle rectangle, List <MyRectangle> remainingRectangles, Tuple <double, double> position) { double waste = 0; double min_width = double.MaxValue; double min_height = double.MaxValue; if (rectangle.Rotate) { var rectangleDif = remainingRectangles.Where(r => r.Width == rectangle.Height && r.Height == rectangle.Width).First(); foreach (var rct in remainingRectangles) { if (rct != rectangleDif) { if (rct.Width < min_width) { min_width = rct.Width; } if (rct.Height < min_height) { min_height = rct.Height; } } } } else { foreach (var rct in remainingRectangles) { if (rct != rectangle) { if (rct.Width < min_width) { min_width = rct.Width; } if (rct.Height < min_height) { min_height = rct.Height; } } } } int i = 0; while (skyline[i].Item1 != position.Item1 || skyline[i].Item3 != position.Item2) { i++; } // type a int j = 1; while (i + j < skyline.Count && skyline[i + j].Item1 < skyline[i].Item1 + rectangle.Width) { if (skyline[i + j].Item2 < skyline[i].Item1 + rectangle.Width) { waste += (skyline[i + j].Item2 - skyline[i + j].Item1) * (skyline[i].Item3 - skyline[i + j].Item3); } else { waste += (skyline[i].Item1 + rectangle.Width - skyline[i + j].Item1) * (skyline[i].Item3 - skyline[i + j].Item3); } j++; } // type b bool cond; if (i != 0) { j = i - 1; while (j != 0 && skyline[j].Item3 < skyline[i].Item3 + rectangle.Height) { j--; } if (j == i - 1) { cond = false; } else { cond = skyline[i].Item1 - skyline[j].Item1 < min_width; j++; } if (cond) { while (j != i) { waste += (skyline[j].Item2 - skyline[j].Item1) * (skyline[i].Item3 + rectangle.Height - skyline[j].Item3); j++; } } } // type c j = i; while (j < skyline.Count && skyline[j].Item3 < skyline[i].Item3 + rectangle.Height) { j++; } if (j == skyline.Count) { cond = skylineWidth - (skyline[i].Item1 + rectangle.Width) < min_width; } else { cond = skyline[j].Item1 - (skyline[i].Item1 + rectangle.Width) < min_width; } if (cond) { int k = 0; while (i + k < skyline.Count && skyline[i + k].Item2 < skyline[i].Item1 + rectangle.Width) { k++; } if (i + k < skyline.Count) { waste += (skyline[i + k].Item2 - (skyline[i].Item1 + rectangle.Width)) * (skyline[i].Item3 + rectangle.Height - skyline[i + k].Item3); k++; } while (i + k < skyline.Count && k < j) { waste += (skyline[i + k].Item2 - skyline[i + k].Item1) * (skyline[i].Item3 + rectangle.Height - skyline[i + k].Item3); k++; } } // type d cond = (skylineHeight - (skyline[i].Item3 + rectangle.Height)) < min_height; if (cond) { waste += rectangle.Width * (skylineHeight - (skyline[i].Item3 + rectangle.Height)); } return(waste); }
public static bool CanPlace(List <Tuple <double, double, double> > skyline, double skylineWidth, double skylineHeight, Tuple <double, double> endpoint, MyRectangle rectangle) { int i = 0; while (skyline[i].Item1 != endpoint.Item1 || skyline[i].Item3 != endpoint.Item2) { i++; } if (skyline[i].Item1 + rectangle.Width <= skylineWidth && skyline[i].Item3 + rectangle.Height <= skylineHeight) { int j = 1; while (i + j < skyline.Count && skyline[i + j].Item1 < skyline[i].Item1 + rectangle.Width) { if (skyline[i + j].Item3 > skyline[i].Item3) { return(false); } j++; } return(true); } return(false); }
private Dictionary <int, dynamic> RightPlacementEvaluation(List <Tuple <double, double, double> > skyline, Tuple <double, double> endpoint, double spread, MyRectangle rectangle, List <MyRectangle> rectangles) { var skylineRevert = Utils.Revert(skyline, this.Width); var new_endpoint = new Tuple <double, double>(this.Width - endpoint.Item1, endpoint.Item2); var evaluation = LeftPlacementEvaluation(skylineRevert, new_endpoint, spread, rectangle, rectangles); if (evaluation[1]) { evaluation[-1] = new Tuple <double, double>(this.Width - evaluation[-1].Item1 - rectangle.Width, evaluation[-1].Item2); evaluation[0] = Utils.Revert(evaluation[0], this.Width); } return(evaluation); }