/// <summary> /// Find cut lines that separates all point sets equally. /// Generates dcel of dual lines and generates cut lines through intersection of middle faces. /// </summary> /// <param name="a_points1"></param> /// <param name="a_points2"></param> /// <param name="a_points3"></param> /// <returns></returns> public static List <Line> FindCutLines(IEnumerable <Vector2> a_points1, IEnumerable <Vector2> a_points2, IEnumerable <Vector2> a_points3) { // obtain dual lines for game objects var lines1 = PointLineDual.Dual(a_points1); var lines2 = PointLineDual.Dual(a_points2); var lines3 = PointLineDual.Dual(a_points3); // add lines together var allLines = lines1.Concat(lines2.Concat(lines3)); // calculate bounding box around line intersections with some margin var bBox = BoundingBoxComputer.FromLines(allLines, 10f); // calculate dcel for line inside given bounding box var dcel1 = new DCEL(lines1, bBox); var dcel2 = new DCEL(lines2, bBox); var dcel3 = new DCEL(lines3, bBox); // find faces in the middle of the lines vertically var archerFaces = MiddleFaces(dcel1, lines1); var swordsmenFaces = MiddleFaces(dcel2, lines2); var mageFaces = MiddleFaces(dcel3, lines3); // obtain cut lines for the dcel middle faces return(FindCutlinesInDual(archerFaces, swordsmenFaces, mageFaces)); }
/// <summary> /// Finds a number of solutions for the cut problem. Both per type of soldier and for all soldiers. /// /// NOTE: only works if the x coords of all things are all positive or all negative /// </summary> public void FindSolution() { // obtain dual lines for game objects m_archerLines = PointLineDual.Dual(m_archers.Select(x => (Vector2)x.transform.position)).ToList(); m_spearmenLines = PointLineDual.Dual(m_spearmen.Select(x => (Vector2)x.transform.position)).ToList(); m_mageLines = PointLineDual.Dual(m_mages.Select(x => (Vector2)x.transform.position)).ToList(); // add lines together var allLines = m_archerLines.Concat(m_spearmenLines.Concat(m_mageLines)); // calculate bounding box around line intersections with some margin var bBox = BoundingBoxComputer.FromLines(allLines, 10f); // calculate dcel for line inside given bounding box m_archerDcel = new DCEL(m_archerLines, bBox); m_spearmenDcel = new DCEL(m_spearmenLines, bBox); m_mageDcel = new DCEL(m_mageLines, bBox); // find faces in the middle of the lines vertically m_archerFaces = HamSandwich.MiddleFaces(m_archerDcel, m_archerLines); m_spearmenFaces = HamSandwich.MiddleFaces(m_spearmenDcel, m_spearmenLines); m_mageFaces = HamSandwich.MiddleFaces(m_mageDcel, m_mageLines); // obtain cut lines for the dcel middle faces and final possible cutlines m_solution = new DivideSolution(HamSandwich.FindCutlinesInDual(m_archerFaces), HamSandwich.FindCutlinesInDual(m_spearmenFaces), HamSandwich.FindCutlinesInDual(m_mageFaces), HamSandwich.FindCutlinesInDual(m_archerFaces, m_spearmenFaces, m_mageFaces)); // update solution to the drawer m_lineDrawer.Solution = m_solution; }
public void FromLinesTest() { var ret = BoundingBoxComputer.FromLines(m_lines); Assert.IsTrue(CmpRect(expRect, ret)); ret = BoundingBoxComputer.FromLines(m_lines, 1f); Assert.IsTrue(CmpRect(expRectMargin, ret)); Assert.AreEqual(BoundingBoxComputer.FromLines(new List <Line>()), new Rect()); }
public HamSandwichTest() { m_points = new List <Vector2>() { new Vector2(1, -2), new Vector2(2, 0), new Vector2(5, 5), new Vector2(-3, -1) }; m_lines = new List <Line>() { new Line(1, 2), new Line(2, 0), new Line(5, -5), new Line(-3, 1) }; m_rect = BoundingBoxComputer.FromLines(m_lines, 10f); m_dcel = new DCEL(m_lines, m_rect); m_points1 = new List <Vector2>() { new Vector2(-1, 1), new Vector2(2, -1) }; m_points2 = new List <Vector2>() { new Vector2(3, 2), new Vector2(3.5f, 0) }; m_points3 = new List <Vector2>() { new Vector2(-3, 1), new Vector2(-3.4f, -3) }; m_lines1 = new List <Line>() { new Line(-1, -1), new Line(2, 1) }; m_lines2 = new List <Line>() { new Line(3, -2), new Line(3.5f, 0) }; m_lines3 = new List <Line>() { new Line(-3, -1), new Line(-3.4f, 3) }; m_allLines = m_lines1.Concat(m_lines2.Concat(m_lines3)).ToList(); m_rect = BoundingBoxComputer.FromLines(m_allLines, 10f); m_dcel1 = new DCEL(m_lines1, m_rect); m_dcel2 = new DCEL(m_lines2, m_rect); m_dcel3 = new DCEL(m_lines3, m_rect); }
public static List <Line> FindCutLines(IEnumerable <Vector2> a_points) { // obtain dual lines for points var lines = PointLineDual.Dual(a_points); // calculate bounding box around line intersections with some margin var bBox = BoundingBoxComputer.FromLines(lines, 10f); // calculate dcel for line inside given bounding box var m_dcel = new DCEL(lines, bBox); // find faces in the middle of the lines vertically and calculate cut lines var faces = MiddleFaces(m_dcel, lines); return(FindCutlinesInDual(faces)); }