Beispiel #1
0
        public void ChangeVertices(int n)
        {
            baseVertices = n;

            if (_vertices.Count > 6)
            {
                _vertices.RemoveRange(6, _vertices.Count - 6);
            }

            if (_edges.Count > 9)
            {
                _edges.RemoveRange(9, _edges.Count - 9);
            }

            if (Faces.Count > 5)
            {
                Faces.RemoveRange(5, Faces.Count - 5);
            }

            if (n == 0)
            {
                InitializeBasicObject();
                return;
            }

            var newv = new List <Vertex>();
            var newe = new List <Edge>();
            var newf = new List <Face>();

            for (var i = 0; i < n; i++)
            {
                newf.Add(new Face());
            }

            for (var i = 0; i < n * 2; i++)
            {
                newv.Add(new Vertex {
                    X = 50, Y = i % 2 == 0 ? -25 : 25
                });
            }

            for (var i = 0; i < n * 3; i++)
            {
                var e = new Edge {
                    Left = newf[i / 3]
                };

                switch (i % 3)
                {
                case 0:
                    e.Left       = Faces[0];
                    e.Init       = newv[i / 3 * 2];
                    e.End        = i == 0 ? _vertices[0] : newv[(i - 1) / 3 * 2];
                    e.Right      = newf[i / 3];
                    e.Right.Edge = e;
                    break;

                case 1:
                    e.Right = i == n * 3 - 2 ? Faces[3] : newf[i / 3 + 1];
                    e.Init  = newv[i / 3 * 2];
                    e.End   = newv[i / 3 * 2 + 1];
                    break;

                case 2:
                    e.Right = Faces[4];
                    e.End   = i == 2 ? _vertices[3] : newv[(i - 3) / 3 * 2 + 1];
                    e.Init  = newv[i / 3 * 2 + 1];
                    break;
                }
                e.Init.Edge = e;
                e.End.Edge  = e;
                newe.Add(e);
            }

            for (var i = 0; i < n * 3; i++)
            {
                switch (i % 3)
                {
                case 0:
                    if (i == 0)
                    {
                        newe[i].UpperRight   = _edges[3];
                        newe[i].UpperLeft    = _edges[0];
                        _edges[0].LowerLeft  = newe[i];
                        _edges[3].LowerRight = newe[i];
                    }
                    else
                    {
                        newe[i].UpperRight = newe[i - 2];
                        newe[i].UpperLeft  = newe[i - 3];
                    }
                    if (i == (n - 1) * 3)
                    {
                        newe[i].LowerLeft   = _edges[2];
                        _edges[2].UpperLeft = newe[i];
                    }
                    else
                    {
                        newe[i].LowerLeft = newe[i + 3];
                    }
                    newe[i].LowerRight = newe[i + 1];
                    break;

                case 1:
                    newe[i].UpperLeft = newe[i + 1];
                    newe[i].LowerLeft = newe[i - 1];
                    if (i == n * 3 - 2)
                    {
                        newe[i].LowerRight   = _edges[2];
                        newe[i].UpperRight   = _edges[8];
                        _edges[2].UpperRight = newe[i];
                        _edges[8].UpperLeft  = newe[i];
                    }
                    else
                    {
                        newe[i].LowerRight = newe[i + 2];
                        newe[i].UpperRight = newe[i + 4];
                    }
                    break;

                case 2:
                    if (i == 2)
                    {
                        newe[i].UpperLeft    = _edges[3];
                        newe[i].UpperRight   = _edges[6];
                        _edges[3].UpperRight = newe[i];
                        _edges[6].LowerRight = newe[i];
                    }
                    else
                    {
                        newe[i].UpperLeft  = newe[i - 4];
                        newe[i].UpperRight = newe[i - 3];
                    }
                    newe[i].LowerLeft = newe[i - 1];

                    newe[i].LowerRight = i == n * 3 - 1 ? _edges[8] : newe[i + 3];
                    break;
                }
            }

            _edges[8].UpperRight = newe[n * 3 - 1];
            _edges[8].End        = newv[n * 2 - 1];
            _edges[2].End        = newv[(n - 1) * 2];
            _edges[3].Right      = n == 0 ? Faces[3] : newf[0];

            var angle = 360.0 / (n + 3);
            var total = n * 2 - 2;

            for (var i = 0; i < newv.Count; i += 2)
            {
                var m  = Matrix.YRotationMatrix(-angle * ((total - i) / 2 + 3));
                var p1 = m * (Point)newv[i];

                newv[i].X     = p1.X;
                newv[i + 1].X = p1.X;
                newv[i].Z     = p1.Z;
                newv[i + 1].Z = p1.Z;
            }

            _vertices[1].X = _vertices[4].X = _vertices[2].X = _vertices[5].X = _vertices[0].X;
            _vertices[1].Z = _vertices[4].Z = _vertices[2].Z = _vertices[5].Z = _vertices[0].Z;

            for (var i = 1; i < 3; i++)
            {
                var m  = Matrix.YRotationMatrix(-angle * i);
                var p1 = m * (Point)_vertices[i];

                _vertices[i].X     = p1.X;
                _vertices[i + 3].X = p1.X;
                _vertices[i].Z     = p1.Z;
                _vertices[i + 3].Z = p1.Z;
            }

            _vertices.AddRange(newv);
            _edges.AddRange(newe);
            Faces.AddRange(newf);
        }