Пример #1
0
 public AlgPoly(TrigPoly f)
 {
     p = (Complex[])f.c.Clone();
 }
Пример #2
0
            /// <summary>
            /// Constructing the curve which is approximating a polygon.
            /// </summary>
            /// <param name="polygon"> The polygon it approximates. </param>
            /// <param name="N"> The last index of the coefficients. </param>
            public FourierCurve(Polygon polygon, int N = 10)
            {
                LastInd = N;
                x       = new TrigPoly(N);
                y       = new TrigPoly(N);
                List <Vector2> vertices = new List <Vector2>();
                List <float>   t        = new List <float>();
                float          length   = 0;

                t.Add(0);
                vertices.Add(polygon[0]);

                int n = 1;

                for (int i = 1; i < polygon.Count; i++)
                {
                    float d = Vector2.Distance(polygon[i], vertices[n - 1]);
                    if (d > Mathf.Epsilon)
                    {
                        length += d;
                        t.Add(2 * Mathf.PI * length);
                        vertices.Add(polygon[i]);
                        n++;
                    }
                }

                t[0] /= length;
                for (int i = 1; i < n; i++)
                {
                    t[i]   /= length;
                    x.a[0] += (t[i] - t[i - 1]) * (vertices[i].x + vertices[i - 1].x) / (4 * Mathf.PI);
                    y.a[0] += (t[i] - t[i - 1]) * (vertices[i].y + vertices[i - 1].y) / (4 * Mathf.PI);
                }

                for (int k = 1; k <= N; k++)
                {
                    x.a[k] = x.b[k] = y.a[k] = y.b[k] = 0;
                    float denom = (k * k * Mathf.PI);
                    float dt, dx, dy, dCos, dSin;
                    for (int i = 0; i < n - 1; i++)
                    {
                        dt   = t[i + 1] - t[i];
                        dx   = vertices[i + 1].x - vertices[i].x;
                        dy   = vertices[i + 1].y - vertices[i].y;
                        dCos = Mathf.Cos(k * t[i + 1]) - Mathf.Cos(k * t[i]);
                        dSin = Mathf.Sin(k * t[i + 1]) - Mathf.Sin(k * t[i]);

                        x.a[k] += (dx / dt) * dCos / denom;
                        x.b[k] += (dx / dt) * dSin / denom;
                        y.a[k] += (dy / dt) * dCos / denom;
                        y.b[k] += (dy / dt) * dSin / denom;
                    }
                }

                x.ComputeComplexCoeffs();
                y.ComputeComplexCoeffs();

                transform = new Transform2D(new Vector2(0, 0), 1, 0);

                AABB = polygon.AABB;
            }