private void LinearSlice(Pair2f slice) { List <Slice2D> results = Slicer2D.LinearSliceAll(slice, sliceLayer); if (addForce == true) { float sliceRotation = Vector2f.Atan2(slice.B, slice.A); foreach (Slice2D id in results) { foreach (GameObject gameObject in id.gameObjects) { Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> (); if (rigidBody2D) { foreach (Vector2f p in id.collisions) { Vector2 force = new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount); rigidBody2D.AddForceAtPosition(force, p.Get()); } } } } } cutterCount--; }
private static void DrawLine_Smooth_Matrix(Pair2f pair, float z = 0f) { float size = lineWidth; float pi2 = Mathf.PI / 2; float rot = Vector2f.Atan2(pair.A, pair.B); Vector2f A1 = new Vector2f(pair.A); Vector2f A2 = new Vector2f(pair.A); Vector2f B1 = new Vector2f(pair.B); Vector2f B2 = new Vector2f(pair.B); A1.Push(rot + pi2, size); A2.Push(rot - pi2, size); B1.Push(rot + pi2, size); B2.Push(rot - pi2, size); GL.TexCoord2(0, 0); GL.Vertex3(B1.GetX(), B1.GetY(), z); GL.TexCoord2(1, 0); GL.Vertex3(A1.GetX(), A1.GetY(), z); GL.TexCoord2(1, 1); GL.Vertex3(A2.GetX(), A2.GetY(), z); GL.TexCoord2(0, 1); GL.Vertex3(B2.GetX(), B2.GetY(), z); }
static private Slice2D SliceWithTwoHoles(Polygon polygon, Pair2f slice, Polygon holeA, Polygon holeB) { Slice2D result = Slice2D.Create(); if (holeA == holeB) { Debug.LogError("Slicer2D: Incorrect Split 2: Cannot Split Into Same Hole"); return(result); } Polygon polyA = new Polygon(); Polygon polyB = new Polygon(polygon.pointsList); polyA.AddPoints(VectorList2f.GetListStartingIntersectLine(holeA.pointsList, slice)); polyA.AddPoints(VectorList2f.GetListStartingIntersectLine(holeB.pointsList, slice)); foreach (Polygon poly in polygon.holesList) { if (poly != holeA && poly != holeB) { polyB.AddHole(poly); } } polyB.AddHole(polyA); result.AddPolygon(polyB); return(result); }
static public void DrawSlice(List <Vector2f> slice, float z = 0f) { foreach (Pair2f p in Pair2f.GetList(slice, false)) { DrawLine(p.A, p.B, z); } }
private static void DrawSlice_Smooth_Matrix(List <Vector2f> list, float z) { foreach (Pair2f p in Pair2f.GetList(list)) { DrawLine_Smooth_Matrix(p, z); } }
private void ComplexSlice(List <Vector2f> slice) { List <Slice2D> results = Slicer2D.ComplexSliceAll(slice, sliceLayer); if (addForce == true) { foreach (Slice2D id in results) { foreach (GameObject gameObject in id.gameObjects) { Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> (); if (rigidBody2D) { List <Pair2f> list = Pair2f.GetList(id.collisions); float forceVal = 1.0f / list.Count; foreach (Pair2f p in list) { float sliceRotation = -Vector2f.Atan2(p.B, p.A); Vector2 force = new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount); rigidBody2D.AddForceAtPosition(forceVal * force, (p.A.Get() + p.B.Get()) / 2f); } } } } } }
static private Slice2D SliceWithoutHoles(Polygon polygon, Pair2f slice) { Slice2D result = Slice2D.Create(); if (polygon.LineIntersectHoles(slice).Count > 0) { Debug.LogError("Slicer2D: Slice Intersect Holes (Point Slicer?)"); // When does this happen? - Only when simple slicer is set - point slicer!!! return(result); } Polygon polyA = new Polygon(); Polygon polyB = new Polygon(); Polygon currentPoly = polyA; int collisionCount = 0; foreach (Pair2f p in Pair2f.GetList(polygon.pointsList)) { Vector2f intersection = MathHelper.GetPointLineIntersectLine(p, slice); if (intersection != null) { polyA.AddPoint(intersection); polyB.AddPoint(intersection); currentPoly = (currentPoly == polyA) ? polyB : polyA; collisionCount++; } currentPoly.AddPoint(p.B); } if (collisionCount == 2) // ' Is it concave split? { if (polyA.pointsList.Count() >= 3) { result.AddPolygon(polyA); } if (polyB.pointsList.Count() >= 3) { result.AddPolygon(polyB); } foreach (Polygon poly in result.polygons) { foreach (Polygon hole in polygon.holesList) { if (poly.PolyInPoly(hole) == true) { poly.AddHole(hole); } } } return(result); } return(result); }
static private Slice2D SlicePolygonInside(Polygon polygon, List <Vector2f> slice) // Create Polygon Inside? { Slice2D result = Slice2D.Create(); Polygon newPoly = new Polygon(); bool createPoly = false; foreach (Pair2f pairA in Pair2f.GetList(slice, false)) { foreach (Pair2f pairB in Pair2f.GetList(slice, false)) { Vector2f intersection = MathHelper.GetPointLineIntersectLine(pairA, pairB); if (intersection != null && (pairA.A != pairB.A && pairA.B != pairB.B && pairA.A != pairB.B && pairA.B != pairB.A)) { createPoly = !createPoly; newPoly.AddPoint(intersection); } } if (createPoly == true) { newPoly.AddPoint(pairA.B); } } bool inHoles = false; foreach (Polygon hole in polygon.holesList) { if (hole.PolyInPoly(newPoly) == true) { inHoles = true; } } if (inHoles == false && newPoly.pointsList.Count > 2 && polygon.PolyInPoly(newPoly) == true) { polygon.AddHole(newPoly); List <Polygon> polys = new List <Polygon> (polygon.holesList); foreach (Polygon hole in polys) { if (newPoly.PolyInPoly(hole) == true) { polygon.holesList.Remove(hole); newPoly.AddHole(hole); } } result.AddPolygon(polygon); if (Slicer2D.complexSliceType == Slicer2D.SliceType.FillSlicedHole) { result.AddPolygon(newPoly); } return(result); } return(result); }
private static void DrawSlice_Matrix(List <Vector2f> list, float z) { foreach (Pair2f p in Pair2f.GetList(list)) { GL.Vertex3(p.A.GetX(), p.A.GetY(), z); GL.Vertex3(p.B.GetX(), p.B.GetY(), z); } }
public static bool LineIntersectLine(Pair2f lineS, Pair2f lineE) { if (GetPointLineIntersectLine(lineS, lineE) != null) { return(true); } return(false); }
static public void DrawSmoothLine(Pair2f pair, float z = 0f) { GL.PushMatrix(); lineMaterial.SetPass(0); GL.Begin(GL.QUADS); DrawLine_Smooth_Matrix(pair, z); GL.End(); GL.PopMatrix(); }
public bool IsClockwise() { double sum = 0; foreach (Pair2f id in Pair2f.GetList(pointsList)) { sum += (id.B.GetX() - id.A.GetX()) * (id.B.GetY() + id.A.GetY()); } return(sum > 0); }
public static bool LineIntersectPoly(Pair2f line, List <Vector2f> poly) { foreach (Pair2f b in Pair2f.GetList(poly)) { if (LineIntersectLine(line, b)) { return(true); } } return(false); }
public static List <Vector2f> GetListLineIntersectSlice(Pair2f pair, List <Vector2f> slice) { List <Vector2f> resultList = new List <Vector2f> (); foreach (Pair2f id in Pair2f.GetList(slice, false)) { Vector2f result = GetPointLineIntersectLine(id, pair); if (result != null) { resultList.Add(result); } } return(resultList); }
public static List <Vector2f> GetListPolyIntersectSlice(List <Vector2f> pointsList, Pair2f slice) { List <Vector2f> resultList = new List <Vector2f> (); foreach (Pair2f id in Pair2f.GetList(pointsList)) { Vector2f result = GetPointLineIntersectLine(id, slice); if (result != null) { resultList.Add(result); } } return(resultList); }
public Slice2D LinearSlice(Pair2f slice) { Polygon colliderPolygon = GetPolygonToSlice(); if (colliderPolygon != null) { Slice2D sliceResult = Slicer2D.LinearSlice(colliderPolygon, slice); sliceResult.AddGameObjects(PerformResult(sliceResult.polygons)); return(sliceResult); } return(Slice2D.Create()); }
// Getting List is Slower public static List <Vector2f> GetListLineIntersectPoly(Pair2f line, List <Vector2f> poly) { List <Vector2f> result = new List <Vector2f>(); foreach (Pair2f b in Pair2f.GetList(poly)) { Vector2f intersection = GetPointLineIntersectLine(line, b); if (intersection != null) { result.Add(intersection); } } return(result); }
public List <Vector2f> GetListSliceIntersectPoly(Pair2f slice) { List <Vector2f> intersections = MathHelper.GetListPolyIntersectSlice(pointsList, slice); foreach (Polygon poly in holesList) { foreach (Vector2f p in MathHelper.GetListPolyIntersectSlice(poly.pointsList, slice)) { intersections.Add(p); } } return(intersections); }
public List <Polygon> LineIntersectHoles(Pair2f pair) { List <Polygon> resultList = new List <Polygon> (); foreach (Polygon poly in holesList) { if (MathHelper.LineIntersectPoly(pair, poly.pointsList) == true) { resultList.Add(poly); } } return(resultList); }
public static bool SliceIntersectPoly(List <Vector2f> slice, List <Vector2f> polyB) { foreach (Pair2f a in Pair2f.GetList(slice, false)) { foreach (Pair2f b in Pair2f.GetList(polyB)) { if (LineIntersectLine(a, b)) { return(true); } } } return(false); }
public float GetArea() { float area = 0f; foreach (Pair2f id in Pair2f.GetList(pointsList)) { area += ((id.B.GetX() - id.A.GetX()) * (id.B.GetY() + id.A.GetY())) / 2.0f; } foreach (Polygon p in holesList) { area -= p.GetArea(); } return(Mathf.Abs(area)); }
public static bool PolyInPoly(List <Vector2f> polyIn, List <Vector2f> poly) { foreach (Pair2f p in Pair2f.GetList(poly)) { if (PointInPoly(p.A, polyIn) == false) { return(false); } } if (PolyIntersectPoly(polyIn, poly) == true) { return(false); } return(true); }
public static bool SliceIntersectItself(List <Vector2f> slice) { foreach (Pair2f pairA in Pair2f.GetList(slice, true)) { foreach (Pair2f pairB in Pair2f.GetList(slice, true)) { if (MathHelper.GetPointLineIntersectLine(pairA, pairB) != null) { if (pairA.A != pairB.A && pairA.B != pairB.B && pairA.A != pairB.B && pairA.B != pairB.A) { return(true); } } } } return(false); }
static public void DrawPolygon(Polygon poly, float z = 0f) { Check(); if (smooth) { GL.PushMatrix(); lineMaterial.SetPass(0); GL.Begin(GL.QUADS); DrawSlice_Smooth_Matrix(poly.pointsList, z); GL.End(); GL.PopMatrix(); GL.PushMatrix(); lineEndMaterial.SetPass(0); GL.Begin(GL.QUADS); foreach (Pair2f p in Pair2f.GetList(poly.pointsList)) { DrawLineEnd_Smooth_Matrix(p, z); } GL.End(); GL.PopMatrix(); } else { GL.PushMatrix(); defaultMaterial.SetPass(0); GL.Begin(GL.LINES); GL.Color(setColor); DrawSlice_Matrix(poly.pointsList, z); GL.End(); GL.PopMatrix(); } foreach (Polygon p in poly.holesList) { DrawPolygon(p, z); } }
static public List <Slice2D> LinearSliceAll(Pair2f slice, Slice2DLayer layer) { List <Slice2D> result = new List <Slice2D> (); foreach (Slicer2D id in GetList()) { if (id.MatchLayers(layer)) { Slice2D sliceResult = id.LinearSlice(slice); if (sliceResult.gameObjects.Count > 0) { result.Add(sliceResult); } } } return(result); }
public static bool PointInPoly(Vector2f point, List <Vector2f> xy) { if (xy.Count < 3) { return(false); } int total = 0; int diff = 0; foreach (Pair2f id in Pair2f.GetList(xy)) { diff = (GetQuad(point, id.A) - GetQuad(point, id.B)); switch (diff) { case -2: case 2: if ((id.B.GetX() - (((id.B.GetY() - point.GetY()) * (id.A.GetX() - id.B.GetX())) / (id.A.GetY() - id.B.GetY()))) < point.GetX()) { diff = -diff; } break; case 3: diff = -1; break; case -3: diff = 1; break; default: break; } total += diff; } return(Mathf.Abs(total) == 4); }
static private Slice2D SingleSlice(Polygon polygon, Pair2f slice) { Slice2D result = Slice2D.Create(); if ((polygon.PointInPoly(slice.A) == true || polygon.PointInPoly(slice.B) == true)) // && pointsInHoles == 1 { Debug.LogError("Slicer2D: Incorrect Split 1: When it Happens"); // Slicing through hole cut-out return(result); } Polygon holeA = polygon.PointInHole(slice.A); Polygon holeB = polygon.PointInHole(slice.B); int pointsInHoles = Convert.ToInt32(holeA != null) + Convert.ToInt32(holeB != null); if (pointsInHoles == 2 && holeA == holeB) { pointsInHoles = 1; } switch (pointsInHoles) { case 0: return(SliceWithoutHoles(polygon, slice)); case 1: return(SliceWithOneHole(polygon, slice, holeA, holeB)); case 2: return(SliceWithTwoHoles(polygon, slice, holeA, holeB)); default: break; } return(result); }
private void ComplexSlice(List <Vector2f> slice) { List <Slice2D> results = Slicer2D.ComplexSliceAll(slice, null); if (addForce == true) { foreach (Slice2D id in results) { foreach (GameObject gameObject in id.gameObjects) { Rigidbody2D rigidBody2D = gameObject.GetComponent <Rigidbody2D> (); if (rigidBody2D) { foreach (Pair2f p in Pair2f.GetList(id.collisions)) { float sliceRotation = Vector2f.Atan2(p.B, p.A); rigidBody2D.AddForceAtPosition(new Vector2(Mathf.Cos(sliceRotation) * addForceAmount, Mathf.Sin(sliceRotation) * addForceAmount), (p.A.Get() + p.B.Get()) / 2f); } } } } } }
// Might Break (Only for 2 collisions) /// <summary> /// Return a list which starts with first interesction with given slice /// </summary> public static List <Vector2f> GetListStartingIntersectSlice(List <Vector2f> pointsList, List <Vector2f> slice) { List <Vector2f> result = new List <Vector2f> (); bool start = false; foreach (Pair2f p in Pair2f.GetList(pointsList)) { List <Vector2f> r = MathHelper.GetListLineIntersectSlice(p, slice); if (start == true) { result.Add(p.A); } if (r.Count > 0) { result.Add(r.First()); start = true; } } foreach (Pair2f p in Pair2f.GetList(pointsList)) { List <Vector2f> r = MathHelper.GetListLineIntersectSlice(p, slice); if (start == true) { result.Add(p.A); } if (r.Count > 0) { result.Add(r.First()); start = false; } } return(result); }
/// <summary> /// Return a list which starts with first interesction with given line /// </summary> public static List <Vector2f> GetListStartingIntersectLine(List <Vector2f> pointsList, Pair2f line) { List <Vector2f> result = new List <Vector2f> (); bool start = false; foreach (Pair2f p in Pair2f.GetList(pointsList)) { Vector2f r = MathHelper.GetPointLineIntersectLine(p, line); if (start == true) { result.Add(p.A); } if (r != null) { result.Add(r); start = true; } } foreach (Pair2f p in Pair2f.GetList(pointsList)) { Vector2f r = MathHelper.GetPointLineIntersectLine(p, line); if (start == true) { result.Add(p.A); } if (r != null) { result.Add(r); start = false; } } return(result); }