Example #1
0
		public void NewPolygon()
		{
			var polygon = new Polygon2D(Rectangle.One, Color.White);
			polygon.Points.AddRange(new[] { Vector2D.Zero, Vector2D.One, Vector2D.UnitY });
			Assert.AreEqual(Rectangle.One, polygon.DrawArea);
			Assert.AreEqual(Color.White, polygon.Color);
		}
Example #2
0
		public void ChangePolygonAfterFirstFrame()
		{
			var polygon = new Polygon2D(Rectangle.One, Color.White);
			polygon.Points.AddRange(new[] { Vector2D.Zero, Vector2D.One, Vector2D.UnitY });
			AdvanceTimeAndUpdateEntities();
			polygon.Points.Add(Vector2D.Zero);
			AdvanceTimeAndUpdateEntities();
		}
Example #3
0
		private static void CreatePolygon(Vector2D position, float radius, Color color)
		{
			var polygon = new Polygon2D(Rectangle.FromCenter(position, new Size(radius)), color);
			var points = new List<Vector2D> { polygon.Center };
			for (int num = 0; num <= 500; num++)
				points.Add(polygon.Center +
					new Vector2D(radius * MathExtensions.Sin(num * 360.0f / 500.0f),
						radius * MathExtensions.Cos(num * 360.0f / 500.0f)));
			polygon.Points.AddRange(points);
		}
Example #4
0
 public Clipper(int xmin, int xmax, int ymin, int ymax)
 {
     m_xMin = xmin;
     m_xMax = xmax;
     m_yMin = ymin;
     m_yMax = ymax;
     m_clipPoly = new Polygon2D(m_xMin, m_yMin);
     //Need to add vertices in clockwise order
     m_clipPoly.AddVertex(new Vector3<double>(m_xMax, m_yMin, 1));
     m_clipPoly.AddVertex(new Vector3<double>(m_xMax, m_yMax, 1));
     m_clipPoly.AddVertex(new Vector3<double>(m_xMin, m_yMax, 1));
 }
Example #5
0
    static void DrawTileOcclussion(Vector2D offset, float z, LightingTilemapCollider2D id)
    {
        Polygon2D poly = Polygon2DList.CreateFromRect(new Vector2(0.5f, 0.5f));

        foreach (DoublePair2D p in DoublePair2D.GetList(poly.pointsList))
        {
            Vector2D vA = p.A + offset;
            Vector2D vB = p.B + offset;
            Vector2D vC = p.B + offset;

            Vector2D pA = p.A + offset;
            Vector2D pB = p.B + offset;

            vA.Push(Vector2D.Atan2(p.A, p.B) - Mathf.PI / 2, -1);
            vB.Push(Vector2D.Atan2(p.A, p.B) - Mathf.PI / 2, -1);
            vC.Push(Vector2D.Atan2(p.B, p.C) - Mathf.PI / 2, -1);

            GL.TexCoord2(uv0, uv0);
            Max2D.Vertex3(pB, z);
            GL.TexCoord2(0.5f - uv0, uv0);
            Max2D.Vertex3(pA, z);
            GL.TexCoord2(0.5f - uv0, uv1);
            Max2D.Vertex3(vA, z);

            GL.TexCoord2(uv0, uv1);
            Max2D.Vertex3(vA, z);
            GL.TexCoord2(0.5f - uv0, uv1);
            Max2D.Vertex3(vB, z);
            GL.TexCoord2(0.5f - uv0, uv0);
            Max2D.Vertex3(pB, z);

            GL.TexCoord2(uv1, uv0);
            Max2D.Vertex3(vB, z);
            GL.TexCoord2(0.5f - uv0, uv0);
            Max2D.Vertex3(pB, z);
            GL.TexCoord2(0.5f - uv0, uv1);
            Max2D.Vertex3(vC, z);
        }
    }
Example #6
0
    // Get List Of Polygons from Collider (Usually Used Before Creating Slicer2D Object)
    static public List <Polygon2D> CreateFromPolygonColliderToWorldSpace(PolygonCollider2D collider)
    {
        List <Polygon2D> result = new List <Polygon2D> ();

        if (collider != null && collider.pathCount > 0)
        {
            Polygon2D newPolygon = new Polygon2D();

            foreach (Vector2 p in collider.GetPath(0))
            {
                newPolygon.AddPoint(p + collider.offset);
            }

            newPolygon = newPolygon.ToWorldSpace(collider.transform);

            result.Add(newPolygon);

            for (int i = 1; i < collider.pathCount; i++)
            {
                Polygon2D hole = new Polygon2D();
                foreach (Vector2 p in collider.GetPath(i))
                {
                    hole.AddPoint(p + collider.offset);
                }

                hole = hole.ToWorldSpace(collider.transform);

                if (newPolygon.PolyInPoly(hole) == true)
                {
                    newPolygon.AddHole(hole);
                }
                else
                {
                    result.Add(hole);
                }
            }
        }
        return(result);
    }
Example #7
0
        public static Segment2D GetLineSegment2D(Polygon2D polygon, int index)
        {
            Segment2D lineSegment = null;

            Vector2D[] polygonVertices = polygon.GetVertices();
            Vector2D   startPoint;
            Vector2D   endPoint;

            if (index <= polygonVertices.Length - 2)
            {
                startPoint = polygonVertices[index];
                endPoint   = polygonVertices[index + 1];

                lineSegment = CreateSegment2D(new Vector2D[] { startPoint, endPoint }, polygon.Name);
            }
            else
            {
                lineSegment = null;
            }

            return(lineSegment);
        }
Example #8
0
        public Slice2D PointSlice(Vector2D point, float rotation)
        {
            Slice2D slice2D = Slice2D.Create(gameObject, point, rotation);

            if (isActiveAndEnabled == false)
            {
                return(slice2D);
            }

            Polygon2D colliderPolygon = GetPolygonToSlice();

            if (colliderPolygon != null)
            {
                Slice2D sliceResult = Slicer2D.API.PointSlice(colliderPolygon, point, rotation);

                PerformResult(sliceResult.GetPolygons(), sliceResult);

                return(sliceResult);
            }

            return(slice2D);
        }
Example #9
0
        public void VisibleToOtherVerticesTest2()
        {
            var m_topVertex      = new Vector2(1, 1);
            var m_botVertex      = new Vector2(0, -1);
            var m_rightVertex    = new Vector2(1, 0);
            var m_farRightVertex = new Vector2(2, 0);

            var polygon = new Polygon2D(
                new List <Vector2>()
            {
                m_topVertex,
                m_farRightVertex,
                m_botVertex,
                m_rightVertex
            });

            var vertices = new List <Vector2>()
            {
                m_farRightVertex,
                m_botVertex,
                m_rightVertex
            };

            var visibleVertices = new List <Vector2>()
            {
                m_farRightVertex,
                m_rightVertex
            };

            var vertex = m_topVertex;

            var actual =
                m_L2LVisibility.VisibleToOtherVertices(
                    vertex,
                    vertices,
                    polygon);

            Assert.AreEqual(visibleVertices.Count, actual.Count);
        }
Example #10
0
    public bool Detach(PolygonCollider2D collider)
    {
        Polygon2D polygon = Polygon2DList.CreateFromPolygonColliderToWorldSpace(collider)[0];

        bool attached = true;

        foreach (Collider2D c in anchorColliders)
        {
            Polygon2D p = anchorPolygons[anchorColliders.IndexOf(c)].ToWorldSpace(c.transform);

            // Fix for PolyCollidePoly!!!
            bool inHole = false;

            foreach (Polygon2D hole in polygon.holesList)
            {
                if (hole.PolyInPoly(p))
                {
                    inHole = true;
                }
            }

            if (inHole == false)
            {
                if (Math2D.PolyCollidePoly(p, polygon) == false)
                {
                    attached = false;
                }
            }
            else
            {
                attached = false;
            }
        }
        if (attached == false)
        {
            return(true);
        }
        return(false);
    }
Example #11
0
    // Getting List is Slower
    public static List <Vector2D> GetListLineIntersectPoly(Pair2D line, Polygon2D poly)
    {
        List <Vector2D> result = new List <Vector2D>();

        Vector2D intersection;

        Pair2D pair = new Pair2D(new Vector2D(poly.pointsList.Last()), null);

        for (int i = 0; i < poly.pointsList.Count; i++)
        {
            pair.B = poly.pointsList[i];

            intersection = GetPointLineIntersectLine(line, pair);
            if (intersection != null)
            {
                result.Add(intersection);
            }

            pair.A = pair.B;
        }
        return(result);
    }
        static void InitShape()
        {
            Coefficients DefaultCoefficients = TimeWarp.Coefficients;

            Vector2D[] mainhullvertecies = new Vector2D[]
            {
                new Vector2D(90, 0),
                new Vector2D(70, 15),
                new Vector2D(-20, 15),
                new Vector2D(-40, 10),
                new Vector2D(-40, -10),
                new Vector2D(-20, -15),
                new Vector2D(70, -15)
            };
            Vector2D[] leftWingvertecies = new Vector2D[]
            {
                new Vector2D(10, 10),
                new Vector2D(-30, 10),
                new Vector2D(-50, -30),
                new Vector2D(-20, -30)
            };
            int length = leftWingvertecies.Length;

            Vector2D[] RightWingvertecies = new Vector2D[]
            {
                new Vector2D(-20, 30),
                new Vector2D(-50, 30),
                new Vector2D(-30, -10),
                new Vector2D(10, -10)
            };
            IGeometry2D mainhull  = new Polygon2D(ALVector2D.Zero, mainhullvertecies);
            IGeometry2D leftWing  = new Polygon2D(new ALVector2D(0, new Vector2D(10, -20)), leftWingvertecies);
            IGeometry2D RightWing = new Polygon2D(new ALVector2D(0, new Vector2D(10, 20)), RightWingvertecies);

            DefaultShape = new RigidBodyTemplate(10, 1468.2144894018809f, new IGeometry2D[] { leftWing, RightWing, mainhull }, new Coefficients[] { DefaultCoefficients, DefaultCoefficients, DefaultCoefficients });
            DefaultShape.BalanceBody();
            ////DefaultShape.CalcInertiaMultiplier(.1f);
        }
Example #13
0
        static public void LinearSlice(Slice2D slice, float forceAmount)
        {
            float sliceRotationA = (float)Vector2D.Atan2(slice.slice[1], slice.slice[0]) - 90f * Mathf.Deg2Rad;
            float sliceRotationB = (float)Vector2D.Atan2(slice.slice[1], slice.slice[0]) + 90f * Mathf.Deg2Rad;
            float rot            = 0;

            Rigidbody2D rigidBody2D;

            List <Vector2D> collisions      = slice.GetCollisions();
            Vector2         middleCollision = ((collisions[0] + collisions[1]) / 2f).ToVector2();

            foreach (GameObject gameObject in slice.GetGameObjects())
            {
                rigidBody2D = gameObject.GetComponent <Rigidbody2D> ();
                if (rigidBody2D)
                {
                    Polygon2D poly   = slice.GetPolygons()[slice.GetGameObjects().IndexOf(gameObject)];
                    Rect      bounds = poly.GetBounds();

                    Vector2 cpA = middleCollision;
                    cpA = cpA.Push(sliceRotationA, 1);

                    Vector2 cpB = middleCollision;
                    cpB = cpB.Push(sliceRotationB, 1);

                    if (Vector2.Distance(cpA, bounds.center) < Vector2.Distance(cpB, bounds.center))
                    {
                        rot = sliceRotationA;
                    }
                    else
                    {
                        rot = sliceRotationB;
                    }

                    rigidBody2D.AddForce(new Vector2(Mathf.Cos(rot), Mathf.Sin(rot)) * forceAmount);
                }
            }
        }
Example #14
0
    public void OnRenderObject()
    {
        if (Camera.current != Camera.main)
        {
            return;
        }

        //Max2D.SetLineWidth (0.25f);
        //Max2D.SetColor (Color.black);
        //Max2D.SetBorder (false);
        //Max2D.SetLineMode(Max2D.LineMode.Smooth);

        inspectorPosition = Vector3.zero;

        Vector2D pos = new Vector2D(Camera.main.ScreenToWorldPoint(Input.mousePosition));

        foreach (Slicer2D slicer in Slicer2D.GetList())
        {
            Polygon2D poly = slicer.shape.GetWorld().ToWorldSpace(slicer.transform);
            if (poly.PointInPoly(pos))
            {
                Rect rect = poly.GetBounds();

                //Max2D.lineMaterial = Max2D.lineLegacyMaterial;

                //Max2DLegacy.DrawLineRectf(rect.x, rect.y, rect.width, rect.height);
                //Max2DLegacy.DrawLinef(rect.center.x, rect.center.y, rect.center.x, rect.center.y + rect.height / 2 + 1);

                //Max2D.lineMaterial = Max2D.lineNewMaterial;

                inspectorPosition = new Vector2(rect.center.x, rect.center.y + rect.height / 2);

                originalSize = slicer.GetComponent <DemoSlicer2DInspectorTracker>().originalSize;
                currentSize  = poly.GetArea();
                sliced       = slicer.limit.counter;
            }
        }
    }
Example #15
0
        internal Class454(IShape2D shape, GraphicsConfig config)
        {
            IList <Polyline2D> flattened = (IList <Polyline2D>)ShapeTool.GetFlattened(shape, config.ShapeFlattenEpsilon);

            if (flattened != null)
            {
                IList <Polygon2D> input1        = (IList <Polygon2D>) new List <Polygon2D>(flattened.Count);
                IList <Polygon2D> polygon2DList = (IList <Polygon2D>) new List <Polygon2D>();
                for (int index = 0; index < flattened.Count; ++index)
                {
                    Polygon2D polygon2D = new Polygon2D((IEnumerable <Point2D>)flattened[index]);
                    if (polygon2D.IsClockwise())
                    {
                        polygon2DList.Add(polygon2D);
                    }
                    else if (input1.Count == 0)
                    {
                        input1.Add(polygon2D);
                    }
                    else
                    {
                        input1 = (IList <Polygon2D>)Polygon2D.GetUnion(input1, (IList <Polygon2D>) new Polygon2D[1]
                        {
                            polygon2D
                        });
                    }
                }
                List <IList <Point2D> > point2DListList = new List <IList <Point2D> >(input1.Count + polygon2DList.Count);
                point2DListList.AddRange((IEnumerable <IList <Point2D> >)input1);
                point2DListList.AddRange((IEnumerable <IList <Point2D> >)polygon2DList);
                Triangulator2D.Triangulate((IList <IList <Point2D> >)point2DListList, (IList <Triangulator2D.Triangle>) this.list_0, (IList <Point2D>) this.list_1);
            }
            else
            {
                this.list_0 = (List <Triangulator2D.Triangle>)null;
                this.list_1 = (List <Point2D>)null;
            }
        }
Example #16
0
    static private Slice2D SingleSlice(Polygon2D polygon, Pair2D slice)
    {
        Slice2D result = Slice2D.Create(slice);

        if ((polygon.PointInPoly(slice.A) == true || polygon.PointInPoly(slice.B) == true))             //  && pointsInHoles == 1
        {
            Slicer2D.Debug.LogError("Incorrect Split 1: When it Happens");
            // Slicing through hole cut-out
            //	return(result);
        }

        Polygon2D holeA = polygon.PointInHole(slice.A);
        Polygon2D 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);
    }
Example #17
0
    public static Mesh TriangulateAdvanced(Polygon2D polygon, Vector2 UVScale, Vector2 UVOffset)
    {
        TriangulationWrapper.Polygon poly = new TriangulationWrapper.Polygon();

        List <Vector2> pointsList   = null;
        List <Vector2> UVpointsList = null;

        Vector3 v = Vector3.zero;

        foreach (Vector2D p in polygon.pointsList)
        {
            v = p.ToVector2();
            poly.outside.Add(v);
            //poly.outsideUVs.Add (new Vector2(v.x / UVScale.x + .5f + UVOffset.x, v.y / UVScale.y + .5f + UVOffset.y));

            poly.outsideUVs.Add(Vector2.zero);
        }

        foreach (Polygon2D hole in polygon.holesList)
        {
            pointsList   = new List <Vector2> ();
            UVpointsList = new List <Vector2> ();

            foreach (Vector2D p in hole.pointsList)
            {
                v = p.ToVector2();
                pointsList.Add(v);
                //UVpointsList.Add (new Vector2(v.x / UVScale.x + .5f, v.y / UVScale.y + .5f));

                UVpointsList.Add(Vector2.zero);
            }

            poly.holes.Add(pointsList);
            poly.holesUVs.Add(UVpointsList);
        }

        return(TriangulationWrapper.CreateMesh(poly));
    }
        public VisibilitySimpleTest()
        {
            var m_topVertex      = new Vector2(0, 1);
            var m_botVertex      = new Vector2(0, -1);
            var m_leftVertex     = new Vector2(-1, 0);
            var m_rightVertex    = new Vector2(1, 0);
            var m_farRightVertex = new Vector2(2, 0);

            arrowPoly = new Polygon2D(new List <Vector2>()
            {
                m_topVertex, m_farRightVertex, m_botVertex, m_rightVertex
            });
            diamondPoly = new Polygon2D(new List <Vector2>()
            {
                m_topVertex, m_rightVertex, m_botVertex, m_leftVertex
            });

            LShape = new Polygon2D(new List <Vector2>()
            {
                new Vector2(0, 0), new Vector2(0, 4), new Vector2(4, 4),
                new Vector2(4, 2), new Vector2(2, 2), new Vector2(2, 0)
            });
        }
    static public Mesh GeneratePolygonMesh(Vector2D pos, Polygon2D.PolygonType polygonType, float polygonSize, float minVertexDistance, Transform transform, float lineWidth, float zPosition)
    {
        List <Mesh2DSubmesh> trianglesList = new List <Mesh2DSubmesh>();

        Polygon2D slicePolygon = Polygon2D.Create(polygonType, polygonSize);

        slicePolygon.ToOffsetItself(pos);

        Vector2D vA, vB;

        foreach (Pair2D pair in Pair2D.GetList(slicePolygon.pointsList, true))
        {
            vA = new Vector2D(pair.A);
            vB = new Vector2D(pair.B);

            vA.Push(Vector2D.Atan2(pair.A, pair.B), -minVertexDistance / 5);
            vB.Push(Vector2D.Atan2(pair.A, pair.B), minVertexDistance / 5);

            trianglesList.Add(Max2DMesh.CreateLine(new Pair2D(vA, vB), transform, lineWidth, zPosition));
        }

        return(Max2DMesh.Export(trianglesList));
    }
Example #20
0
 private void Load()
 {
     if (!_isLoaded)
     {
         var points = new List <Point2D>();
         if (Commands != null && Commands.Any())
         {
             points.Add(Commands.First().PointFrom);
             points.AddRange(Commands.Select(c => new Point2D {
                 X = c.PointTo.X ?? 0.0, Y = c.PointTo.Y ?? 0.0
             }));
         }
         _polygon = new Polygon2D {
             Points = points
         };
         _maxX     = _polygon.MaxX;
         _minX     = _polygon.MinX;
         _maxY     = _polygon.MaxY;
         _minY     = _polygon.MinY;
         _isClosed = _polygon.IsClosed;
         _isLoaded = true;
     }
 }
    void Start()
    {
        instance = this;
        tempList = new List <Vector2>();
        for (int i = 0; i < 10; i++)
        {
            tempList.Add(RoomGenerationInCircle.getRandomPointInCircle(100, 1));
        }
        // tempList.Add(new Vector2(-123,73));
        // tempList.Add(new Vector2(108,-63));
        // tempList.Add(new Vector2(-107,-46));
        // tempList.Add(new Vector2(-39,-8));
        // tempList.Add(new Vector2(-54,110));
        triangles = Polygon2D.DelaunayTriangulation(tempList);

        // foreach (var item in triangles)
        // {
        //     Debug.DrawLine(item.pointA, item.pointB);
        //     Debug.DrawLine(item.pointA, item.pointC);
        //     Debug.DrawLine(item.pointC, item.pointB);
        // }
        // StartCoroutine(Draw());
    }
 private void Update()
 {
     foreach (var item in tempList)
     {
         Debug.DrawLine(item + Vector2.up, item + Vector2.down);
         Debug.DrawLine(item + Vector2.left, item + Vector2.right);
     }
     foreach (var item in triangles)
     {
         Debug.DrawLine(item.pointA, item.pointB);
         Debug.DrawLine(item.pointA, item.pointC);
         Debug.DrawLine(item.pointC, item.pointB);
     }
     if (Input.GetKeyDown("space"))
     {
         tempList.RemoveRange(0, tempList.Count);
         for (int i = 0; i < 10; i++)
         {
             tempList.Add(RoomGenerationInCircle.getRandomPointInCircle(100, 1));
         }
         triangles = Polygon2D.DelaunayTriangulation(tempList);
     }
 }
Example #23
0
        public void GetOutlinePoints_Rectangle()
        {
            var polygon        = new Polygon2D(PolygonGrid2D.GetRectangle(6, 10));
            var expectedPoints = new List <Vector2Int>();

            // Vertical lines
            for (int i = 0; i <= 10; i++)
            {
                expectedPoints.Add(new Vector2Int(0, i));
                expectedPoints.Add(new Vector2Int(6, i));
            }

            // Horizontal lines
            for (int i = 1; i < 6; i++)
            {
                expectedPoints.Add(new Vector2Int(i, 0));
                expectedPoints.Add(new Vector2Int(i, 10));
            }

            var points = polygon.GetOutlinePoints();

            Assert.That(points, Is.EquivalentTo(expectedPoints));
        }
Example #24
0
    public List <Polygon2D> GetPolygon(LightingTilemapCollider2D tilemap)
    {
        if (world_polygon == null)
        {
            if (tilemap.colliderType == LightingTilemapCollider2D.ColliderType.SpriteCustomPhysicsShape)
            {
                if (GetShapePolygons().Count < 1)
                {
                    return(null);
                }

                world_polygon = GetShapePolygons();                 //poly.ToScaleItself(defaultSize); // scale?
            }
            else
            {
                Vector2 size = tilemap.cellSize * 0.5f;

                world_polygon = new List <Polygon2D>();
                world_polygon.Add(Polygon2D.CreateFromRect(size));
            }
        }
        return(world_polygon);
    }
Example #25
0
        static void InitShape()
        {
            Coefficients        DefaultCoefficients = TimeWarp.Coefficients;
            List <IGeometry2D>  goes = new List <IGeometry2D>();
            List <Coefficients> coes = new List <Coefficients>();



            Vector2D[] engineconevertecies = new Vector2D[]
            {
                new Vector2D(20, 10),
                new Vector2D(-50, 40),
                new Vector2D(-100, 40),
                new Vector2D(-100, -40),
                new Vector2D(-50, -40),
                new Vector2D(20, -10)
            };
            Vector2D offset = Polygon2D.CalcCentroid(engineconevertecies);

            //engineconevertecies = Vector2D.Translate(-offset, engineconevertecies);

            OperationHelper.ArrayRefOp <Vector2D, Vector2D, Vector2D>(
                engineconevertecies,
                ref offset,
                engineconevertecies,
                Vector2D.Subtract);


            goes.Add(new Polygon2D(new ALVector2D(0, offset), engineconevertecies));
            coes.Add(DefaultCoefficients);
            goes.Add(new Polygon2D(new ALVector2D(0, new Vector2D(10, 0)), Polygon2D.FromNumberofSidesAndRadius(8, 40)));
            coes.Add(DefaultCoefficients);
            DefaultShape = new RigidBodyTemplate(20, 1995.1433515249612f, goes.ToArray(), coes.ToArray());

            DefaultShape.BalanceBody();
            ////DefaultShape.CalcInertiaMultiplier(.1f);
        }
Example #26
0
        private void BuildSlice()
        {
            this.Original = SliceModel.GeometrizeModel(this.data.CurrentModel);
            var bounds = this.data.CurrentModel.Bounds;

            Construct obj = Construct.Create(this.Original, this.data.CurrentModel.Transform);
            //Construct box = Construct.Create(this.SlicePlane.Geometry as MeshGeometry3D);
            //Construct sli = obj.Intersect(box);

            var dense_spacing = this.data.NozzleDiameter * 2.875;  // Default == 1.15

            // Genarate infills
            var infill_struct = Polygon2D.GenerateInfill(
                bounds.X, bounds.Y,
                bounds.X + bounds.SizeX, bounds.Y + bounds.SizeY,
                this.data.NozzleDiameter, this.data.InfillSpacing, this.data.UseInfill
                );

            // Construct slice
            this.Slice = obj.Slice(bounds.Z + data.CurrentSliceIdx * data.NozzleThickness,
                                   data.NozzleThickness);
            this.Slice.SetNozzleHeight(data.CurrentSliceIdx * data.NozzleThickness);
            this.Slice.Erode(data.NozzleThickness / 2.0);
            this.Slice.AddShells(data.NumberOfShells, data.NozzleThickness * dense_spacing);
            this.Slice.AddInfill(infill_struct);
            this.Slice.SortPolygons();

            var min   = Math.Min(bounds.X, bounds.Y);
            var max   = Math.Max(bounds.X + bounds.SizeX, bounds.Y + bounds.SizeY);
            var size  = Math.Min(this.data.SliceCanvas.ActualWidth, this.data.SliceCanvas.ActualHeight);
            var scale = size / (max - min);

            this.data.SliceShapes = this.Slice.ToShapes(bounds.X, bounds.Y, scale, this.data.PreviewArrowThickness, this.data.PreviewStrokeThickness);

            this.data.SliceCanvas.Children.Clear();
            this.data.SliceShapes.ForEach(x => this.data.SliceCanvas.Children.Add(x));
        }
    static public Slice2D LinearCutSlice(Polygon2D polygon, LinearCut linearCut)
    {
        List <Vector2D> slice  = linearCut.GetPointsList();
        Slice2D         result = Slice2D.Create(linearCut);

        if (slice.Count < 1)
        {
            return(result);
        }

        Vector2D startPoint = null;

        foreach (Vector2D id in slice)
        {
            if (polygon.PointInPoly(id) == false)
            {
                startPoint = id;
                break;
            }
        }

        Polygon2D newPolygon = new Polygon2D(slice);

        slice = Vector2DList.GetListStartingPoint(slice, startPoint);
        slice.Add(startPoint);

        if (polygon.PolyInPoly(newPolygon))
        {
            polygon.AddHole(newPolygon);
            result.AddPolygon(polygon);
            return(result);
        }

        result = ComplexSlicer.Slice(polygon, slice);

        return(result);
    }
Example #28
0
    static public void DrawPolygon(Polygon2D poly, float z = 0f, bool connect = true)
    {
        Max2D.Check();

        switch (Max2D.lineMode)
        {
        case Max2D.LineMode.Smooth:
            GL.PushMatrix();
            Max2D.lineMaterial.SetPass(0);
            GL.Begin(GL.QUADS);

            Max2DMatrixLegacy.DrawSliceImage(poly.pointsList, z, connect);

            GL.End();
            GL.PopMatrix();

            break;

        case Max2D.LineMode.Default:
            GL.PushMatrix();
            Max2D.defaultMaterial.SetPass(0);
            GL.Begin(GL.LINES);
            GL.Color(Max2D.setColor);

            Max2DMatrixLegacy.DrawSlice(poly.pointsList, z, connect);

            GL.End();
            GL.PopMatrix();

            break;
        }

        foreach (Polygon2D p in poly.holesList)
        {
            DrawPolygon(p, z);
        }
    }
Example #29
0
    private Polygon2D WorldPolyon()
    {
        if (worldPolygon != null)
        {
            return(worldPolygon);
        }

        float cameraSizeY = camera.orthographicSize;
        float cameraSizeX = cameraSizeY * (float)camera.pixelWidth / camera.pixelHeight;

        float sizeX = cameraSizeX * 2;
        float sizeY = cameraSizeY * 2;

        float x = -sizeX / 2;
        float y = -sizeY / 2;

        worldPolygon = Polygon();

        worldPolygon.pointsList[0].x = x;
        worldPolygon.pointsList[0].y = y;

        worldPolygon.pointsList[1].x = x + sizeX;
        worldPolygon.pointsList[1].y = y;

        worldPolygon.pointsList[2].x = x + sizeX;
        worldPolygon.pointsList[2].y = y + sizeY;

        worldPolygon.pointsList[3].x = x;
        worldPolygon.pointsList[3].y = y + sizeY;

        worldPolygon.ToRotationItself(rotation * Mathf.Deg2Rad);
        worldPolygon.ToOffsetItself(position);

        worldRect = worldPolygon.GetRect();

        return(worldPolygon);
    }
        /// <summary>
        /// Triangulates this polygon using the two ears theorem. This is O(n^2).
        /// </summary>
        /// <remarks>
        /// Currently runs in O(n^2) time.
        /// TODO improve this to O(n log n) or O(n log log n).
        /// </remarks>
        /// <param name="polygon"></param>
        /// <param name="setTwinPointers"> whether the triangulation should set twin pointers </param>
        /// <returns>A list of clockwise triangles whose disjoint union is this polygon</returns>
        public static Triangulation Triangulate(Polygon2D polygon, bool setTwinPointers = true)
        {
            // cannot yet triangulate non-simple polygons

            /* assume it to be, checks takes too long
             * if (!polygon.IsSimple())
             * {
             *  throw new ArgumentException("Polygon must be simple: " + polygon);
             * }
             */

            if (polygon.VertexCount < 3)
            {
                return(new Triangulation());
            }

            var T = TriangulateRecursive(polygon.Vertices.ToList());

            if (setTwinPointers)
            {
                T.SetTwinPointers();
            }
            return(T);
        }
Example #31
0
        private IList <Polyline4D> method_16(
            DrawContext.Wireframe context,
            out Polyline3DT imageBoundaryPolyline,
            out IClippingTransformer transformer)
        {
            Polygon2D clipBoundary = this.GetClipBoundary((DrawContext)context);

            if (clipBoundary != null && clipBoundary.Count != 0)
            {
                Matrix4D preTransform = this.method_15();
                transformer = context.GetTransformer();
                transformer = (IClippingTransformer)transformer.Clone();
                transformer.SetPreTransform(preTransform);
                imageBoundaryPolyline = new Polyline3DT(true);
                foreach (WW.Math.Point2D point2D in (List <WW.Math.Point2D>)clipBoundary)
                {
                    imageBoundaryPolyline.Add(new Point3DT((WW.Math.Point3D)point2D, 0U));
                }
                return((IList <Polyline4D>) new List <Polyline4D>((IEnumerable <Polyline4D>)transformer.Transform(imageBoundaryPolyline.ToPolyline3D(), true)));
            }
            imageBoundaryPolyline = (Polyline3DT)null;
            transformer           = (IClippingTransformer)null;
            return((IList <Polyline4D>) new Polyline4D[0]);
        }
Example #32
0
    public override void _Ready()
    {
        lvl = (level)GetNode("/root/level");
        if (lvl != null)
        {
            difficulty_coef = lvl.get_difficulty();
        }

        polygon            = GetNode <Polygon2D>("polygon_2d");
        collision_shape_2d = GetNode <CollisionShape2D>("collision_shape_2d");
        polygon.Color      = new Color(color_name);

        //draw square
        var vectors = polygon.Polygon;

        vectors[0].x = (-(width / 2));
        vectors[0].y = (-(height / 2));
        vectors[1].x = width / 2;
        vectors[1].y = (-(height / 2));
        vectors[2].x = width / 2;
        vectors[2].y = height / 2;
        vectors[3].x = (-(width / 2));
        vectors[3].y = height / 2;

        //vectors[0].Set(-(width / 2), -(height / 2));				// old api (before 3.2)
        //vectors[1].Set((width / 2), -(height / 2));
        //vectors[2].Set((width / 2), (height / 2));
        //vectors[3].Set(-(width / 2), (height / 2));
        polygon.Polygon = vectors;

        //set collision shape
        RectangleShape2D shape = new RectangleShape2D();

        shape.Extents            = new Vector2(width / 2, height / 2);
        collision_shape_2d.Shape = shape;
    }
Example #33
0
        public void UnionTest3()
        {
            var polygon0 = new Polygon2D(new List <Vector2>
            {
                new Vector2(4.00124f, -0.9741771f), new Vector2(1.329107f, 0.0009262562f),
                new Vector2(-1.329329f, -0.003419161f), new Vector2(-1.336399f, -2.668384f),
                new Vector2(-4.00066f, -2.661608f), new Vector2(-4.00317f, 2.666523f), new Vector2(4.00428f, 2.665259f),
            });
            var polygon1 = new Polygon2D(new List <Vector2>
            {
                new Vector2(4.00124f, -2.664724f), new Vector2(1.333887f, -2.663425f),
                new Vector2(1.329107f, 0.0009263754f), new Vector2(-1.329329f, -0.003419101f),
                new Vector2(-4.00317f, -0.7164398f), new Vector2(-4.00317f, 2.666523f),
                new Vector2(4.00428f, 2.665259f),
            });

            var polygon2Ds = new List <Polygon2D> {
                polygon0, polygon1
            };

            var unionResult = m_union.Union(polygon2Ds);

            Assert.AreEqual(35.57237, unionResult.Area, MathUtil.EPS * 100);
        }
Example #34
0
    // Not finished - still has some artifacts
    static public List <Vector2D> PreparePolygon(Polygon2D polygon)
    {
        Polygon2D newPolygon = new Polygon2D();

        polygon.Normalize();

        foreach (DoublePair2D pair in DoublePair2D.GetList(polygon.pointsList))
        {
            float rotA = Vector2D.Atan2(pair.B, pair.A);
            float rotC = Vector2D.Atan2(pair.B, pair.C);

            Vector2D pairA = new Vector2D(pair.A);
            pairA.Push(rotA - Mathf.PI / 2, precision);

            Vector2D pairC = new Vector2D(pair.C);
            pairC.Push(rotC + Mathf.PI / 2, precision);

            Vector2D vecA = new Vector2D(pair.B);
            vecA.Push(rotA - Mathf.PI / 2, precision);
            vecA.Push(rotA, 10f);

            Vector2D vecC = new Vector2D(pair.B);
            vecC.Push(rotC + Mathf.PI / 2, precision);
            vecC.Push(rotC, 10f);

            Vector2D result = Math2D.GetPointLineIntersectLine(new Pair2D(pairA, vecA), new Pair2D(pairC, vecC));

            if (result != null)
            {
                newPolygon.AddPoint(result);
                //	} else {
                //Debug.LogError("Null");
            }
        }
        return(newPolygon.pointsList);
    }
Example #35
0
        // Clips a single polygon and returns the clipped polygon as a new instance
        private Polygon2D ClipPolygon(Polygon2D polygon)
        {
            var vertices = polygon.GetVertices();

            foreach (var line in m_clipPoly.GetLines())
            {
                vertices = SutherlandHodgman(vertices, line);
                //We clipped all our vertices and got nothing back, meaning nothing will be displayed.
                //Just return nothing
                if (vertices.Count == 0)
                    return null;
            }
            vertices.ForEach(v => v.Round());
            return new Polygon2D(vertices);
        }
Example #36
0
		public void ChangeOutlineColor()
		{
			var polygon = new Polygon2D(Rectangle.One, Color.Red);
			polygon.Add(new OutlineColor(Color.Blue));
			Assert.AreEqual(Color.Blue, polygon.Get<OutlineColor>().Value);
		}
Example #37
0
        /// <summary>
        ///
        /// </summary>
        private void BuildPolygon()
        {
            Point2D[] points = new Point2D[Walls.Count];
            points[0] =  (Walls[0] as MazeWall).points[0];

            ArrayList wallsCopy = (ArrayList) Walls.Clone();
            wallsCopy.RemoveAt(0);

            for (int i = 1; i < points.Length; i++)
            {
                //find adjacent wall... the closest
                foreach (MazeWall wall in wallsCopy)
                {
                    if (Point2D.GetDistance(wall.points[0], points[i - 1]) < 0.01)
                    {
                        points[i] = wall.points[1];
                        wallsCopy.Remove(wall);
                        break;
                    }
                    else if (Point2D.GetDistance(wall.points[1], points[i - 1]) < 0.01)
                    {
                        points[i] = wall.points[0];
                        wallsCopy.Remove(wall);
                        break;
                    }
                }
            }
            roomPolygon = new Polygon2D(points);
        }
        private void btnPolyUnion2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearAllVisuals();

                // Create some random triangles
                Point[][] initialPolys = new Point[5][];

                for (int cntr = 0; cntr < initialPolys.Length; cntr++)
                {
                    Point[] triangle = Enumerable.Range(0, 3).
                        Select(o => Math3D.GetRandomVector_Circular(20d)).
                        Select(o => new Point(o.X, o.Y)).
                        ToArray();

                    initialPolys[cntr] = triangle;
                }

                // Separate into islands
                Polygon2D[] finalPolys = null;
                try
                {
                    finalPolys = GetPolyUnion(initialPolys);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                    finalPolys = new Polygon2D[0];
                }

                #region Draw

                Visual3D visual;

                // Lines
                foreach (Point[] polygon in initialPolys)
                {
                    visual = GetVisual_PolygonLines(polygon.Select(o => o.ToPoint3D(-2)).ToArray(), Colors.Gray, 1);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }

                foreach (Polygon2D polygon in finalPolys)
                {
                    visual = GetVisual_PolygonLines(polygon.Polygon.Select(o => o.ToPoint3D(2)).ToArray(), Colors.Black, 2);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);

                    foreach (Point[] hole in polygon.Holes)
                    {
                        visual = GetVisual_PolygonLines(hole.Select(o => o.ToPoint3D(2)).ToArray(), Colors.Red, 2);
                        _debugVisuals.Add(visual);
                        _viewport.Children.Add(visual);
                    }
                }

                // Polygons
                //foreach (Point[] polygon in finalPolys)       // can't do these, they are concave
                foreach (Point[] polygon in initialPolys)
                {
                    Color color = Color.FromArgb(10, Convert.ToByte(StaticRandom.Next(256)), Convert.ToByte(StaticRandom.Next(256)), Convert.ToByte(StaticRandom.Next(256)));

                    visual = GetVisual_Polygon_Convex(polygon.Select(o => o.ToPoint3D()).ToArray(), color);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Example #39
0
        // Calculate the uv points of the polygons.
        public Vector2[] renderpoints(Polygon polygon)
        {
            polygonsinalllayers [numberOfLayers] = maxPolygons;
            value = (polygonsinalllayers [1] - polygonsinalllayers [0])/4;
            Polygon2D b = new Polygon2D ();
            b.vertices.Add (new Vector2 (0, 0));
            b.vertices.Add (new Vector2 (0, 1));
            b.vertices.Add (new Vector2 (1, 1));
            b.vertices.Add (new Vector2 (1, 0));
            b.vertices.Add (new Vector2 (0, 0));
            if ((done && checkvalue<=value) || (checkvalue>(2*value) && checkvalue<=(3*value))) {
                Polygon2D b1 = new Polygon2D ();
                b1 = rotate90points (b);
                b = b1;
            }
            if (checkvalue >value && checkvalue <=(2*value)) {
                Polygon2D b2 = new Polygon2D ();
                b2 = rotate180points (b);
                b = b2;
            }
            if ((checkvalue >= 23 && checkvalue < 27) || (checkvalue >= 30 && checkvalue < 34) || (checkvalue >= 37 && checkvalue < 41)) {
                Polygon2D b2 = new Polygon2D ();
                b2 = rotate90points (b);
                b = b2;
            }
            if ((checkvalue >= 41 && checkvalue < 47) || (checkvalue >=51 && checkvalue<54) || (checkvalue >=58 && checkvalue<61)) {
                Polygon2D b2 = new Polygon2D ();
                b2 = rotate90points (b);
                b = b2;
            }
            if ((checkvalue >= 71 && checkvalue < 75) || (checkvalue >=78 && checkvalue<82) || (checkvalue >=85 && checkvalue<89)) {
                Polygon2D b2 = new Polygon2D ();
                b2 = rotate90points (b);
                b = b2;
            }
            if ((checkvalue >= 89 && checkvalue < 95) || (checkvalue >=99 && checkvalue<102) || (checkvalue >=106 && checkvalue<109)) {
                Polygon2D b2 = new Polygon2D ();
                b2 = rotate90points (b);
                b = b2;
            }

            checkvalue++;
            Polygon2D c = new Polygon2D ();
            c = subdivide2D (b, 10);
            Polygon2D d = new Polygon2D ();
            for(int i=0;i<c.vertices.Count-1;i++)
            {
                d.vertices.Add (c.vertices [i]);
                d.vertices.Add (c.vertices [i+1]);
                d.vertices.Add (new Vector2 (0.5f, 0.5f));

            }
            Vector2[] final = new Vector2[d.vertices.Count];
            for (int i = 0; i < final.Length; i++) {
                final [i] = d.vertices [i];
            }
            done = true;

            return final;
        }
Example #40
0
 // Rotates the polygon by 180 degrees.
 public Polygon2D rotate180points(Polygon2D polygon)
 {
     Polygon2D rotated180 = new Polygon2D ();
     Polygon2D rotated90 = new Polygon2D ();
     rotated90 = rotate90points (polygon);
     rotated180 = rotate90points (rotated90);
     return rotated180;
 }
        public void ReduceComplexity(string points, string reduced)
        {
            var testPoints = from x in points.Split(';') select Point2D.Parse(x);
            var expectedPoints = from x in reduced.Split(';') select Point2D.Parse(x);
            var poly = new Polygon2D(testPoints);
            var expected = new Polygon2D(expectedPoints);
            var thinned = poly.ReduceComplexity(0.00001);

            CollectionAssert.AreEqual(expected, thinned);
        }
        // This determines the order in which uvs are rendered...
        // This has to be modified if some other type of triply periodic polyhedron
        // is selected....This can be automatically setup by some GUI mechansim
        // which can be added....
        public Vector2[] GraphicsOrder()
        {
            Vector2[] final= new Vector2[5];
            final [0] = new Vector2 (1, 1);
            final [1] = new Vector2 (1, 0);
            final [2] = new Vector2 (0, 0);
            final [3] = new Vector2 (0, 1);
            final [4] = new Vector2 (1, 1);

            Polygon2D c = new Polygon2D ();

            for(int i=0;i<final.Length-1;i++)
            {
                c.vertices.Add (final[i]);
                c.vertices.Add (final[i+1]);
                c.vertices.Add (new Vector2 (0.5f, 0.5f));

            }
            Vector2[] finaluvs = new Vector2[vertices.Length];
            for (int i = 0; i < finaluvs.Length; i++) {
                finaluvs [i] = c.vertices [i];
            }

            return finaluvs;
        }
        private void btnPolyUnionTest3b_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearAllVisuals();

                // Create some random triangles
                Point[][] initialPolys = new Point[3][];

                initialPolys[0] = new Point[] {
                    new Point(-8.00822498773053,15.724908636816),
                    new Point(-10.9888146681928,9.99396633672174),
                    new Point(-1.35614945481762,1.2745135259889) };

                initialPolys[1] = new Point[] {
                    new Point(17.6394906227247,-1.78969639822036),
                    new Point(-0.944559908696264,8.15715451007008),
                    new Point(-16.1622843293672,0.442278789958519) }.Reverse().ToArray();

                initialPolys[2] = new Point[] {
                    new Point(-13.3366344395237,10.1599123251435),
                    new Point(4.03979352867374,6.13141576520923),
                    new Point(16.079651596552,9.28399147918985) };

                // Separate into islands
                Polygon2D[] finalPolys = null;
                try
                {
                    finalPolys = GetPolyUnion(initialPolys);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                    finalPolys = new Polygon2D[0];
                }

                #region Draw

                Visual3D visual;

                // Lines
                foreach (Point[] polygon in initialPolys)
                {
                    visual = GetVisual_PolygonLines(polygon.Select(o => o.ToPoint3D(-2)).ToArray(), Colors.Gray, 1);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }

                foreach (Polygon2D polygon in finalPolys)
                {
                    visual = GetVisual_PolygonLines(polygon.Polygon.Select(o => o.ToPoint3D(2)).ToArray(), Colors.Black, 2);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);

                    foreach (Point[] hole in polygon.Holes)
                    {
                        visual = GetVisual_PolygonLines(hole.Select(o => o.ToPoint3D(2)).ToArray(), Colors.Red, 2);
                        _debugVisuals.Add(visual);
                        _viewport.Children.Add(visual);
                    }
                }

                // Polygons
                //foreach (Point[] polygon in finalPolys)       // can't do these, they are concave
                //foreach (Point[] polygon in initialPolys)
                for (int cntr = 0; cntr < initialPolys.Length; cntr++)
                {
                    Color color = UtilityWPF.GetColorEGA(10, cntr);
                    //Color color = Color.FromArgb(10, Convert.ToByte(StaticRandom.Next(256)), Convert.ToByte(StaticRandom.Next(256)), Convert.ToByte(StaticRandom.Next(256)));

                    visual = GetVisual_Polygon_Convex(initialPolys[cntr].Select(o => o.ToPoint3D()).ToArray(), color);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void btnChainTest8b_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearAllVisuals();

                List<Tuple<Point, Point>> segments1 = new List<Tuple<Point, Point>>();

                segments1.Add(Tuple.Create(new Point(0, -5), new Point(4, -1)));

                segments1.Add(Tuple.Create(new Point(4, -1), new Point(5, 0)));
                segments1.Add(Tuple.Create(new Point(5, 0), new Point(4, 1)));
                segments1.Add(Tuple.Create(new Point(4, 1), new Point(3, 0)));
                segments1.Add(Tuple.Create(new Point(3, 0), new Point(4, -1)));
                segments1.Add(Tuple.Create(new Point(4, -1), new Point(4, 1)));

                segments1.Add(Tuple.Create(new Point(4, 1), new Point(0, 5)));
                segments1.Add(Tuple.Create(new Point(0, 5), new Point(-3, 2)));

                segments1.Add(Tuple.Create(new Point(-3, 2), new Point(-2, 1)));
                segments1.Add(Tuple.Create(new Point(-2, 1), new Point(-1, 0)));
                segments1.Add(Tuple.Create(new Point(-1, 0), new Point(-2, -1)));
                segments1.Add(Tuple.Create(new Point(-2, -1), new Point(-3, -2)));
                segments1.Add(Tuple.Create(new Point(-3, -2), new Point(-4, -1)));
                segments1.Add(Tuple.Create(new Point(-4, -1), new Point(-5, 0)));
                segments1.Add(Tuple.Create(new Point(-5, 0), new Point(-4, 1)));
                segments1.Add(Tuple.Create(new Point(-4, 1), new Point(-3, 2)));
                segments1.Add(Tuple.Create(new Point(-3, 2), new Point(-3, 0)));
                segments1.Add(Tuple.Create(new Point(-3, 0), new Point(-3, -2)));
                segments1.Add(Tuple.Create(new Point(-2, 1), new Point(-2, -1)));
                segments1.Add(Tuple.Create(new Point(-4, 1), new Point(-4, -1)));
                segments1.Add(Tuple.Create(new Point(-2, 1), new Point(-3, 0)));
                segments1.Add(Tuple.Create(new Point(-2, -1), new Point(-3, 0)));
                segments1.Add(Tuple.Create(new Point(-4, 1), new Point(-3, 0)));
                segments1.Add(Tuple.Create(new Point(-4, -1), new Point(-3, 0)));

                segments1.Add(Tuple.Create(new Point(-3, -2), new Point(0, -5)));

                //var segments = segments1;
                var segments = UtilityCore.RandomRange(0, segments1.Count).Select(o => segments1[o]).ToList();

                Polygon2D[] final = null;
                try
                {
                    final = PolyUnion2.StitchSegments2.Stitch(segments.ToArray());
                }
                catch (Exception)
                {
                    final = new Polygon2D[0];
                }

                #region Draw

                Visual3D visual;

                // Initial
                for (int cntr = 0; cntr < segments.Count; cntr++)
                {
                    double z = -2 - (cntr * .1);

                    visual = GetVisual_Line(segments[cntr].Item1.ToPoint3D(z), segments[cntr].Item2.ToPoint3D(z), Colors.Gray, 1);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }

                // Final
                foreach (Polygon2D polygon in final)
                {
                    visual = GetVisual_PolygonLines(polygon.Polygon.Select(o => o.ToPoint3D(0)).ToArray(), Colors.Black, 2);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);

                    foreach (Point[] hole in polygon.Holes)
                    {
                        visual = GetVisual_PolygonLines(hole.Select(o => o.ToPoint3D(0)).ToArray(), Colors.Red, 2);
                        _debugVisuals.Add(visual);
                        _viewport.Children.Add(visual);
                    }
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void btnPolyUnionTest5_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ClearAllVisuals();

                // Create some random triangles
                Point[][] initialPolys = new Point[4][];

                initialPolys[0] = new Point[] {
                    new Point(7.63258002821866,-1.71657720938275),
                    new Point(7.76145635305171,-1.67761318154907),
                    new Point(9.43425936393152,-1.17186363320799),
                    new Point(10.5549080800541,-1.71657720938275) };

                initialPolys[1] = new Point[] {
                    new Point(10.3058611604882,-1.59552300547356),
                    new Point(10.3269392559373,-1.71657720938275),
                    new Point(9.54274292483309,-1.71657720938275),
                    new Point(8.87261465785801,-0.898865063733471) };

                initialPolys[2] = new Point[] {
                    new Point(9.43425936393151,-1.17186363320798),
                    new Point(7.7614563530517,-1.67761318154907),
                    new Point(7.63258002821865,-1.71657720938275),
                    new Point(7.00040640990406,-1.71657720938275),
                    new Point(6.87244607297693,0.029306274277618),
                    new Point(6.96135493605682,0.0301407014836736) };

                initialPolys[3] = new Point[] {
                    new Point(6.87244607297693,0.0293062742776171),
                    new Point(6.96135493605682,0.0301407014836719),
                    new Point(6.86909824100712,0.0749839012871441) };



                var intersections = new Tuple<Point, Point, Point, Point, Point>[] {
                    Tuple.Create(new Point(9.16344177297135,-1.2537416985989), new Point(7.76145635305171,-1.67761318154907), new Point(9.43425936393152,-1.17186363320799), new Point(9.54274292483309,-1.71657720938275), new Point(8.87261465785801,-0.898865063733471)),
                    Tuple.Create(new Point(10.3058611604882,-1.59552300547359), new Point(9.43425936393152,-1.17186363320799), new Point(10.5549080800541,-1.71657720938275), new Point(10.3058611604882,-1.59552300547356), new Point(10.3269392559373,-1.71657720938275)),
                    Tuple.Create(new Point(10.3269392559373,-1.71657720938275), new Point(10.5549080800541,-1.71657720938275), new Point(7.63258002821866,-1.71657720938275), new Point(10.3058611604882,-1.59552300547356), new Point(10.3269392559373,-1.71657720938275)),
                    Tuple.Create(new Point(9.54274292483309,-1.71657720938275), new Point(10.5549080800541,-1.71657720938275), new Point(7.63258002821866,-1.71657720938275), new Point(9.54274292483309,-1.71657720938275), new Point(8.87261465785801,-0.898865063733471)),
                    Tuple.Create(new Point(9.16344177297134,-1.25374169859889), new Point(9.54274292483309,-1.71657720938275), new Point(8.87261465785801,-0.898865063733471), new Point(9.43425936393151,-1.17186363320798), new Point(7.7614563530517,-1.67761318154907)),
                    Tuple.Create(new Point(8.87261465785801,-0.898865063733473), new Point(9.54274292483309,-1.71657720938275), new Point(8.87261465785801,-0.898865063733471), new Point(6.96135493605682,0.0301407014836736), new Point(9.43425936393151,-1.17186363320798)),
                    Tuple.Create(new Point(6.87244607297693,0.0293062742776171), new Point(7.00040640990406,-1.71657720938275), new Point(6.87244607297693,0.029306274277618), new Point(6.87244607297693,0.0293062742776171), new Point(6.96135493605682,0.0301407014836719)),
                    Tuple.Create(new Point(6.96135493605682,0.0301407014836736), new Point(6.87244607297693,0.029306274277618), new Point(6.96135493605682,0.0301407014836736), new Point(6.96135493605682,0.0301407014836719), new Point(6.86909824100712,0.0749839012871441)) }.
                    Select(o => new { Intersect = o.Item1, Segment1 = Tuple.Create(o.Item2, o.Item3), Segment2 = Tuple.Create(o.Item4, o.Item5) }).ToArray();


                // Separate into islands
                Polygon2D[] finalPolys = null;
                try
                {
                    finalPolys = GetPolyUnion(initialPolys);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                    finalPolys = new Polygon2D[0];
                }

                #region Draw

                Visual3D visual;

                // Lines
                for (int cntr = 0; cntr < initialPolys.Length; cntr++)
                {
                    double z = -2 - (cntr * .1);

                    visual = GetVisual_PolygonLines(initialPolys[cntr].Select(o => o.ToPoint3D(z)).ToArray(), Colors.Gray, 1);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);

                    foreach (Point point in initialPolys[cntr])
                    {
                        visual = GetVisual_Dot(point.ToPoint3D(z), .01d, Colors.Gray);
                        _debugVisuals.Add(visual);
                        _viewport.Children.Add(visual);
                    }
                }


                for (int cntr = 0; cntr < intersections.Length; cntr++)
                {
                    double z = cntr * .1d;

                    visual = GetVisual_Dot(intersections[cntr].Intersect.ToPoint3D(z), .01d, Colors.Gray);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);

                    visual = GetVisual_Line(intersections[cntr].Segment1.Item1.ToPoint3D(z), intersections[cntr].Segment1.Item2.ToPoint3D(z), Colors.Gray, 2d);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);

                    visual = GetVisual_Line(intersections[cntr].Segment2.Item1.ToPoint3D(z), intersections[cntr].Segment2.Item2.ToPoint3D(z), Colors.Gray, 2d);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }


                foreach (Polygon2D polygon in finalPolys)
                {
                    visual = GetVisual_PolygonLines(polygon.Polygon.Select(o => o.ToPoint3D(2)).ToArray(), Colors.Black, 2);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);

                    foreach (Point[] hole in polygon.Holes)
                    {
                        visual = GetVisual_PolygonLines(hole.Select(o => o.ToPoint3D(2)).ToArray(), Colors.Red, 2);
                        _debugVisuals.Add(visual);
                        _viewport.Children.Add(visual);
                    }
                }

                // Polygons
                //foreach (Point[] polygon in finalPolys)       // can't do these, they are concave
                //foreach (Point[] polygon in initialPolys)
                for (int cntr = 0; cntr < initialPolys.Length; cntr++)
                {
                    Color color = UtilityWPF.GetColorEGA(10, cntr);
                    //Color color = Color.FromArgb(10, Convert.ToByte(StaticRandom.Next(256)), Convert.ToByte(StaticRandom.Next(256)), Convert.ToByte(StaticRandom.Next(256)));

                    visual = GetVisual_Polygon_Convex(initialPolys[cntr].Select(o => o.ToPoint3D(-2 - (cntr * .1))).ToArray(), color);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void btnPolyUnionTest4_Click(object sender, RoutedEventArgs e)
        {

            // The main problem is that lines are slicing through the entire polygon.  Need to detect that, also get rid of colinear points

            try
            {
                ClearAllVisuals();

                Point3D[][] initial3D = new Point3D[3][];

                initial3D[0] = new Point3D[] {
                    new Point3D(-16.1947246550931,-0.9149519332283,2.55976103387392),
                    new Point3D(-17.1107881600685,0.515445435128346,3.40133335668073),
                    new Point3D(-17.3596515210729,-0.829632679480615,4.42103762371103),
                    new Point3D(-17.3450427909807,-2.19357445878095,5.01957844043668),
                    new Point3D(-17.3283385073944,-2.17500700982897,4.98385833288751) };

                initial3D[1] = new Point3D[] {
                    new Point3D(-16.1947246550931,-0.914951933228302,2.55976103387392),
                    new Point3D(-17.3283385073944,-2.17500700982897,4.9838583328875),
                    new Point3D(-17.3450427909807,-2.19357445878095,5.01957844043668),
                    new Point3D(-17.341448388578,-2.52916527179157,5.16684630945675),
                    new Point3D(-15.3066235822732,-2.3016870563904,1.74387733292857) };

                initial3D[2] = new Point3D[] {
                    new Point3D(-17.3204018787836,-4.49416933689273,6.02915227870288),
                    new Point3D(-16.4203453178512,-4.5492595275131,4.58613381891047),
                    new Point3D(-14.928909934653,-2.89147215820253,1.39687808008519),
                    new Point3D(-15.3066235822732,-2.3016870563904,1.74387733292857),
                    new Point3D(-17.341448388578,-2.52916527179157,5.16684630945675) };

                // Create some random triangles
                Point[][] initialPolys = new Point[3][];

                initialPolys[0] = new Point[] {
                    new Point(-1.70753174254516,-10.6978382660948),
                    new Point(-0.726945738693094,-12.3201526746214),
                    new Point(-2.36588629353582,-12.7943243278674),
                    new Point(-3.85545055921974,-12.7943243278674),
                    new Point(-3.82425967044327,-12.7638803171038) };

                initialPolys[1] = new Point[] {
                    new Point(-1.70753174254517,-10.6978382660948),
                    new Point(-3.82425967044327,-12.7638803171038),
                    new Point(-3.85545055921974,-12.7943243278674),
                    new Point(-4.22195013459667,-12.7943243278674),
                    new Point(-2.65818579358351,-9.1250442852861) };

                initialPolys[2] = new Point[] {
                    new Point(-6.36793604229414,-12.7943243278674),
                    new Point(-5.84736974693176,-11.1743089731826),
                    new Point(-3.06250352298613,-8.45612745855601),
                    new Point(-2.65818579358351,-9.1250442852861),
                    new Point(-4.22195013459667,-12.7943243278674) };



                var feedsIntermediate = new Tuple<double, double, double, double>[]
                {
                    Tuple.Create(-1.70753174254516, -10.6978382660948, -0.726945738693094, -12.3201526746214),      //0
                    Tuple.Create(-0.726945738693094, -12.3201526746214, -2.36588629353582, -12.7943243278674),      //1
                    Tuple.Create(-2.36588629353582, -12.7943243278674, -3.85545055921974, -12.7943243278674),       //2
                    Tuple.Create(-3.85545055921974, -12.7943243278674, -3.82425967044327, -12.7638803171038),       //3
                    Tuple.Create(-3.82425967044327, -12.7638803171038, -1.70753174254516, -10.6978382660948),       //4
                    Tuple.Create(-1.70753174254517, -10.6978382660948, -3.82425967044327, -12.7638803171038),       //5
                    Tuple.Create(-3.82425967044327, -12.7638803171038, -3.85545055921974, -12.7943243278674),       //6
                    Tuple.Create(-3.85545055921974, -12.7943243278674, -4.22195013459667, -12.7943243278674),       //7
                    Tuple.Create(-4.22195013459667, -12.7943243278674, -2.65818579358351, -9.1250442852861),       //8
                    Tuple.Create(-2.65818579358351, -9.1250442852861, -1.70753174254517, -10.6978382660948),       //9
                    Tuple.Create(-6.36793604229414, -12.7943243278674, -5.84736974693176, -11.1743089731826),       //10
                    Tuple.Create(-5.84736974693176, -11.1743089731826, -3.06250352298613, -8.45612745855601),       //11
                    Tuple.Create(-3.06250352298613, -8.45612745855601, -2.65818579358351, -9.1250442852861),       //12
                    Tuple.Create(-2.65818579358351, -9.1250442852861, -4.22195013459667, -12.7943243278674),       //13
                    Tuple.Create(-4.22195013459667, -12.7943243278674, -6.36793604229414, -12.7943243278674)       //14
                }.Select(o => Tuple.Create(new Point(o.Item1, o.Item2), new Point(o.Item3, o.Item4))).ToArray();

                #region feeds intermediate dupes

                // Simplify duplication chains:
                //      Any point should only have one other match in the list
                //      If there is more than one match, the list should be able to be simplified

                List<Point> uniquePoints = new List<Point>();
                foreach (Point point in UtilityCore.Iterate(feedsIntermediate.Select(o => o.Item1), feedsIntermediate.Select(o => o.Item2)))
                {
                    if (!uniquePoints.Any(o => Math2D.IsNearValue(point, o)))
                    {
                        uniquePoints.Add(point);
                    }
                }

                var matchAttempt = uniquePoints
                    .Select(o => new { Point = o, Matches = feedsIntermediate.Where(p => Math2D.IsNearValue(o, p.Item1) || Math2D.IsNearValue(o, p.Item2)).ToArray() }).
                    OrderByDescending(o => o.Matches.Length).
                    ToArray();


                Tuple<Point, Point>[] feeds2 = feedsIntermediate.Select(o => o.Item1.X < o.Item2.X ? o : Tuple.Create(o.Item2, o.Item1)).ToArray();

                List<Tuple<Point, Point>> feeds3 = new List<Tuple<Point, Point>>();
                foreach (var feed in feeds2)
                {
                    if (!feeds3.Any(o => Math2D.IsNearValue(feed.Item1, o.Item1) && Math2D.IsNearValue(feed.Item2, o.Item2)))
                    {
                        feeds3.Add(feed);
                    }
                }


                var matchAttempt2 = uniquePoints
                    .Select(o => new { Point = o, Matches = feeds3.Where(p => Math2D.IsNearValue(o, p.Item1) || Math2D.IsNearValue(o, p.Item2)).ToArray() }).
                    OrderByDescending(o => o.Matches.Length).
                    ToArray();


                #endregion

                // This is what StitchSegments generates (should have just come up with one poly)
                Point[][] intermediate = new Point[1][];

                intermediate[0] = new Point[] {
                    new Point(-1.70753174254516,-10.6978382660948),     //0
                    new Point(-0.726945738693094,-12.3201526746214),        //1
                    new Point(-2.36588629353582,-12.7943243278674),     //2
                    new Point(-3.85545055921974,-12.7943243278674),     //3
                    new Point(-3.82425967044327,-12.7638803171038),     //4
                    new Point(-1.70753174254517,-10.6978382660948),     //5
                    new Point(-3.82425967044327,-12.7638803171038),     //6
                    new Point(-3.85545055921974,-12.7943243278674),     //7
                    new Point(-4.22195013459667,-12.7943243278674),     //8
                    new Point(-2.65818579358351,-9.1250442852861) };        //9

                //intermediate[1] = new Point[] {
                //    new Point(-6.36793604229414,-12.7943243278674),
                //    new Point(-5.84736974693176,-11.1743089731826),
                //    new Point(-3.06250352298613,-8.45612745855601),
                //    new Point(-2.65818579358351,-9.1250442852861),
                //    new Point(-4.22195013459667,-12.7943243278674) };

                #region intermediate distances

                List<Tuple<int, int, double, bool>> distances = new List<Tuple<int, int, double, bool>>();
                for (int outer = 0; outer < intermediate[0].Length - 1; outer++)
                {
                    for (int inner = outer + 1; inner < intermediate[0].Length; inner++)
                    {
                        double distance = (intermediate[0][outer] - intermediate[0][inner]).Length;

                        distances.Add(Tuple.Create(outer, inner, distance, Math1D.IsNearZero(distance)));
                    }
                }

                distances = distances.OrderBy(o => o.Item3).ToList();

                #endregion

                // Separate into islands
                Polygon2D[] finalPolys = null;
                try
                {
                    finalPolys = GetPolyUnion(initialPolys);
                }
                catch (Exception ex)
                {
                    //MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                    finalPolys = new Polygon2D[0];
                }

                #region Draw

                Visual3D visual;

                // Lines
                foreach (Point[] polygon in initialPolys)
                {
                    visual = GetVisual_PolygonLines(polygon.Select(o => o.ToPoint3D(-2)).ToArray(), Colors.Gray, 1);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }


                foreach (Point[] polygon in intermediate)
                {
                    visual = GetVisual_PolygonLines(polygon.Select(o => o.ToPoint3D(0)).ToArray(), Colors.Gray, 2);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }


                for (int cntr = 0; cntr < intermediate[0].Length; cntr++)
                {
                    visual = GetVisual_Dot(intermediate[0][cntr].ToPoint3D(2), .02 + (cntr * .01), UtilityWPF.GetColorEGA(64, cntr));
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }


                //foreach (Point3D[] poly in initial3D)
                //{
                //    visual = GetPolygonLinesVisual(poly, Colors.Black, 1);
                //    _debugVisuals.Add(visual);
                //    _viewport.Children.Add(visual);
                //}


                foreach (Polygon2D polygon in finalPolys)
                {
                    visual = GetVisual_PolygonLines(polygon.Polygon.Select(o => o.ToPoint3D(2)).ToArray(), Colors.Black, 2);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);

                    foreach (Point[] hole in polygon.Holes)
                    {
                        visual = GetVisual_PolygonLines(hole.Select(o => o.ToPoint3D(2)).ToArray(), Colors.Red, 2);
                        _debugVisuals.Add(visual);
                        _viewport.Children.Add(visual);
                    }
                }

                // Polygons
                //foreach (Point[] polygon in finalPolys)       // can't do these, they are concave
                //foreach (Point[] polygon in initialPolys)
                for (int cntr = 0; cntr < initialPolys.Length; cntr++)
                {
                    Color color = UtilityWPF.GetColorEGA(10, cntr);
                    //Color color = Color.FromArgb(10, Convert.ToByte(StaticRandom.Next(256)), Convert.ToByte(StaticRandom.Next(256)), Convert.ToByte(StaticRandom.Next(256)));

                    visual = GetVisual_Polygon_Convex(initialPolys[cntr].Select(o => o.ToPoint3D()).ToArray(), color);
                    _debugVisuals.Add(visual);
                    _viewport.Children.Add(visual);
                }

                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        // This block creates the gameobject based on the points obtained by constructcenterpolygon function
        // and adds a material to it.
        public void RenderSquare(Material mat)
        {
            tile=new GameObject("1000");
            Mesh msh = new Mesh();
            tile.AddComponent<MeshFilter> ();

            // if else block to render random color if not in modelling mode or render angels and demons pattern
            // if in the modelling_mode.
            if (twodhyperbolic.modelling_mode)
            {
                tile.AddComponent<MeshRenderer> ().material = mat;
            }
            else
            {
                Color newcolor = new Color (Random.Range(0f,1f), Random.Range(0f,1f), Random.Range(0f,1f));
                tile.AddComponent<MeshRenderer> ().material.color = newcolor;
            }

            tile.AddComponent<MeshCollider> ();
            tile.tag="Player";

            // generate names for each polygon of the triply periodic polygon starting from 1000.
            int name = 1000 + number;
            tile.name = name.ToString ();

            a = ConstructCenterPolygon (4, 6);
            Vector3 center = CenterofPolygon (a);
            print ("center " + center);
            Polygon b = new Polygon ();

            // setting up the vertices
            for (int i=0; i<a.vertices.Count-1; i++)
            {
                print ("i " + i);
                b.vertices.Add(ptok(a.vertices[i]));
                b.vertices.Add(ptok(a.vertices[i+1]));
                b.vertices.Add (ptok(center));
            }
            print ("final point" + b.vertices [b.vertices.Count-1]);
            vertices = new Vector3[b.vertices.Count];
            for (int i = 0; i < vertices.Length; i++) {
                vertices [i] = b.vertices [i];
                print ("b.verties" + b.vertices [i].x);
            }
            msh.vertices = vertices;

            // setting up the traingles
            int[] triangles = new int[b.vertices.Count];
            for (int i = 0; i < vertices.Length; i++) {
                triangles [i] = i;
                print ("tri[i]" + triangles [i]);
            }
            msh.triangles = triangles;
            tile.GetComponent<MeshCollider> ().sharedMesh = msh;

            // setting up the uvs.
            Vector2[] uv = new Vector2[5];
            uv [0] = new Vector2 (0, 1);
            uv [1] = new Vector2 (1, 1);
            uv [2] = new Vector2 (1, 0);
            uv [3] = new Vector2 (0, 0);
            uv [4] = new Vector2 (0, 1);

            Polygon2D c = new Polygon2D ();

            for(int i=0;i<uv.Length-1;i++)
            {
                c.vertices.Add (uv[i]);
                c.vertices.Add (uv[i+1]);
                c.vertices.Add (new Vector2 (0.5f, 0.5f));

            }
            Vector2[] finaluvs = new Vector2[vertices.Length];
            for (int i = 0; i < finaluvs.Length; i++) {
                finaluvs [i] = c.vertices [i];
            }
            if (twodhyperbolic.modelling_mode)
            {
                    msh.uv = finaluvs;
            }

            // setting up the normals
            Vector3[] normals=new Vector3[vertices.Length];
            for (int i = 0; i < normals.Length; i++) {
                normals [i] = Vector3.down;
            }
            msh.normals = normals;
            tile.GetComponent<MeshFilter>().mesh = msh;

            // move the triply periodic polyhedron away from the hyperbolic tessellation.
            tile.transform.position = new Vector3 (0f,0f,9f);
        }
Example #48
0
        // subdivides the points of UV mapping . It takes 2D points of polygons and number of parts it should be divided into.
        public Polygon2D subdivide2D(Polygon2D q,int numberofpoints)
        {
            Polygon2D r = new Polygon2D ();
            for (int i=0; i<q.vertices.Count-1; i++)
            {
                Vector3 end1=q.vertices[i];
                Vector3 end2=q.vertices[i+1];
                float dx=end2.x-end1.x;
                float dy=end2.y-end1.y;
                float stepx = dx / numberofpoints;
                float stepy = dy / numberofpoints;
                float px = end1.x ;
                float py = end1.y ;

                for (int ix = 0; ix <= numberofpoints; ix++)
                {
                    Vector2 point=new Vector2(px,py);
                    r.vertices.Add(point);
                    px = px + stepx;
                    py = py + stepy;
                }
            }
            return r;
        }
Example #49
0
 public Point3D[] GetPointsWithinRegion(Polygon2D polygon, bool byRatio)
 {
     return null;
 }
Example #50
0
        // Rotates the polygon by 90 degrees.
        public Polygon2D rotate90points(Polygon2D polygon)
        {
            Polygon2D rotated = new Polygon2D ();
            for (int i = 1; i < sides + 1; i++) {
                rotated.vertices.Add (polygon.vertices[i]);
            }
            rotated.vertices.Add (polygon.vertices[1]);

            return rotated;
        }