Exemple #1
0
        /// <summary>
        /// Attempts to find a TNumber value for the TType given. If there is no error, but the TNumber reference
        /// given is still null after calling, then the TType is a TString (i.e. some arithmetic will work (+)).
        /// </summary>
        /// <param name="interpreter">The interpreter that the method is being called from.</param>
        /// <param name="number">The TNumber reference to assign the result to.</param>
        /// <param name="value">The TType to get a TNumber out of.</param>
        /// <returns>An exception if there was an error, otherwise null.</returns>
        private static TException AssignNumberValue(Interpreter interpreter, out TNumber number, TType value)
        {
            // Attempt to cast the TType 'value' argument to a TNumber. Failing that, check if it's a TString.
            // If the value is a TNumber or a TString, return null (i.e. no exception). If it's a TVariable then
            // work on the value of the TVariable, otherwise return an exception.


            number = value as TNumber;
            if ((number != null) || (value is TString))
            {
                return(null);
            }

            TVariable variable = value as TVariable;

            if (variable != null)
            {
                value  = variable.Value;
                number = value as TNumber;
                if ((number != null) || (value is TString))
                {
                    return(null);
                }
                return(new TException(interpreter, "Value of '" + variable.Identifier + "' is not a number",
                                      "it is of type '" + value.TypeName + "'"));
            }

            return(new TException(interpreter, "'" + value.ToCSString() + "' is not a number",
                                  "it is of type '" + value.TypeName + "'"));
        }
Exemple #2
0
        private void DrawParabola(
            Graphics g, TNumber startX, TNumber endX, TNumber activeStartX, TNumber activeEndX,
            TVector2 focus, TNumber sweepY, bool isHighlighted)
        {
//                Console.WriteLine($"Drawing Parabola ({focus}, {sweepY}): [{startX}, {endX}) [{activeStartX}, {activeEndX})");

            DrawPointSquare(g, focus, 3);

            if (MathUtil.ApproximatelyEqual(focus.Y, sweepY))
            {
                sweepY += 0.01f;
            }

            for (int i = 0; i + startX < endX; i++)
            {
                TNumber x            = startX + i;
                var     p            = MathUtil.ComputeParabolaPointGivenX(x, sweepY, focus);
                var     point        = new PointF((float)p.X, (float)p.Y);
                var     activeBrush  = isHighlighted ? _highlightedActiveParabolaBrush : _unhighlightedActiveParabolaBrush;
                var     passiveBrush = isHighlighted ? _highlightedInactiveParabolaBrush : _unhighlightedInactiveParabolaBrush;
                if (activeStartX <= x && x <= activeEndX)
                {
                    g.FillRectangle(activeBrush, point.X - 1, point.Y - 1, 3, 3);
                }
                else
                {
                    g.FillRectangle(passiveBrush, point.X, point.Y, isHighlighted ? 3 : 1, isHighlighted ? 3 : 1);
                }
            }
        }
Exemple #3
0
 public static TNumber Average(TNumber a, TNumber b)
 {
     if (a > b)
     {
         return(Average(b, a));
     }
     return(a + (b - a) / 2);
 }
Exemple #4
0
        private void UpdateStateString()
        {
            TNumber tNumber = BLLControl.GetNumber();

            tssFriendMessage.Text = tNumber.FriendMsgNum.ToString();

            tssMessage.Text = tNumber.ProvideMsgNum.ToString();
        }
Exemple #5
0
 public static TNumber Det3(
     TNumber m11, TNumber m12, TNumber m13,
     TNumber m21, TNumber m22, TNumber m23,
     TNumber m31, TNumber m32, TNumber m33)
 {
     return(m11 * m22 * m33 + m12 * m23 * m31 + m13 * m21 * m32 - (
                m31 * m22 * m13 + m32 * m23 * m11 + m33 * m21 * m12
                ));
 }
Exemple #6
0
        /// <summary>
        /// 获取用户的几个内容量集
        /// </summary>
        /// <returns></returns>
        public static TNumber GetNumber()
        {
            ClientAdapt.Open();

            TVssService.Client client = ClientAdapt.GetClient();

            TNumber tNumber = client.GetNumber(GetValidator());

            ClientAdapt.Close();

            return(tNumber);
        }
        public static Node FindDeepestNodeAtX(this Node root, TNumber x, TNumber sweepY)
        {
            var current = root;

            while (!current.IsLeaf())
            {
                var ray      = (EdgeRayData)current.Data;
                var parabola = (ParabolaData)current.Left.GetRightmost().Data;
                var point    = MathUtil.ComputeRayParabolaIntersection(ray.Origin, ray.Direction, parabola.Focus, sweepY);
                current = x < point.X ? current.Left : current.Right;
            }
            return(current);
        }
Exemple #8
0
        /// <summary>
        /// The method that represents the 'random' function in the language.
        /// Generates a random TInteger between 0 and the given value - 1.
        /// </summary>
        /// <param name="interpreter">The interpreter that the method is being called from.</param>
        /// <param name="args">The arguments being passed to the function as a TArgumentList.</param>
        /// <returns>An TInteger with a value between 0 and the given value - 1</returns>
        public static TType Random(Interpreter interpreter, TArgumentList args)
        {
            // Check if the argument passed is a number, and return a new TInteger from the random value generated
            TNumber maximum = args[0] as TNumber;

            if (maximum == null)
            {
                return(new TException(interpreter,
                                      "Arguments of type '" + args[0].TypeName + "' cannot be used by random function"));
            }

            return(new TInteger(randomNumberGenerator.Next((int)maximum.TIntegerValue)));
        }
Exemple #9
0
        public static void ComputeParabolaIntersections(
            TVector2 focusA, TVector2 focusB, TNumber directrixY,
            out TVector2 leftIntersection, out TVector2 rightIntersection)
        {
            // Let a and b the foci of two parabolae (the near-pretentious plural of parabola).
            // Let result be a point r (rx, ry) located at equal distances between the foci and directrix.
            // Of course, with two parabolas it's easy to see there can be two such intersections.
            //
            // First convert to standard form (y = a x^2 + bx + c).
            //    Let p be a point (px, py) on the parabola.
            //    Let f be a point (fx, fy) representing the parabola's focus
            //    Let dy be the y-coordinate of the directrix.
            //
            //        dy - py = |p - f|
            //        dy - py = sqrt((px - fx)^2 + (py - fy)^2)
            //        (dy - py)^2 = (px - fx)^2 + (py - fy)^2
            //        dy^2 - 2 dy py + py^2 = px^2 - 2 px fx + fx^2 + py^2 - 2 py fy + fy^2
            //        dy^2 - 2 dy py = px^2 - 2 px fx + fx^2 - 2 py fy + fy^2
            //        - 2 dy py + 2 py fy = px^2 - 2 px fx + fx^2 + fy^2 - dy^2
            //        py (-2dy + 2fy) = px^2 - 2 px fx + fx^2 + fy^2 - dy^2
            //        py = (px^2 - 2 px fx + fx^2 + fy^2 - dy^2) / (-2dy + 2fy)
            //        py = (1 / (-2dy + 2fy))(px^2) + (-2fx / (-2dy + 2fy)) px + (fx^2 + fy^2 - dy^2) / (-2dy + 2fy)
            //             a                  px^2  + b                     px + c
            //
            // Given two parabolas in standard form (y1 = a1 x^2 + b1 x + c1, y2 = a2 x^2 + b2 x + c2),
            // solve the system of equations...
            //
            //                        y1 = y2
            //        a1 x^2 + b1 x + c1 = a2 x^2 + b2 x + c2
            //                         0 = (a2 - a1) x^2 + (b2 - b1) x + (c2 - c1)
            //
            //                         x = -(b2 - b1) +/- sqrt((b2 - b1)^2 - 4(a2 - a1)(c2 - c1))
            //                             ------------------------------------------------------
            //                              2(a2 - a1)
            TNumber a1, b1, c1, a2, b2, c2;

            ComputeParabolaStandardForm(focusA, directrixY, out a1, out b1, out c1);
            ComputeParabolaStandardForm(focusB, directrixY, out a2, out b2, out c2);

            TNumber a = a2 - a1, b = b2 - b1, c = c2 - c1;
            var     discriminant = b * b - 4 * a * c;
            var     xCenter      = -b / (2 * a);
            var     xOffset      = (TNumber)Math.Abs(Math.Sqrt(discriminant) / (2 * a));
            var     leftX        = xCenter - xOffset;
            var     rightX       = xCenter + xOffset;

            leftIntersection  = new Vector2(leftX, a1 * leftX * leftX + b1 * leftX + c1);
            rightIntersection = new Vector2(rightX, a1 * rightX * rightX + b1 * rightX + c1);
        }
Exemple #10
0
        private static void ComputeParabolaStandardForm(
            TVector2 focus, TNumber directrixY,
            out TNumber a, out TNumber b, out TNumber c)
        {
            if (ApproximatelyEqual(focus.Y, directrixY))
            {
                a = b = 0;
                c = directrixY;
                return;
            }
            var denominator = -2 * directrixY + 2 * focus.Y;

            a = 1.0f / denominator;
            b = -2 * focus.X / denominator;
            c = (focus.X * focus.X + focus.Y * focus.Y - directrixY * directrixY) / denominator;
        }
Exemple #11
0
        public static TVector2 ComputeParabolaPointGivenX(TNumber rx, TNumber directrixY, TVector2 focus)
        {
            // Let result be a point r (rx, ry) located between the focus's y and directrix y.
            // directrixY - ry = sqrt((rx - focusX)^2 + (ry - focusY)^2)
            // (directrixY - ry)^2 = (rx - focusX)^2 + (ry - focusY)^2
            // directrixY^2 - 2 directrixY ry + ry^2 = rx^2 - 2 rx focusX + focusX^2 + ry^2 - 2 ry focusY + focusY^2
            // -2 directrixY ry + ry^2 = rx^2 - 2 rx focusX + focusX^2 + ry^2 - 2 ry focusY + focusY^2 - directrixY^2
            // -2 directrixY ry + ry^2 - ry^2 + 2 ry focusY = rx^2 - 2 rx focusX + focusX^2 + focusY^2 - directrixY^2
            // -2 directrixY ry + 2 ry focusY = rx^2 - 2 rx focusX + focusX^2 + focusY^2 - directrixY^2
            // ry (-2 directrixY + 2 focusY) = rx^2 - 2 rx focusX + focusX^2 + focusY^2 - directrixY^2
            // ry = (rx^2 - 2 rx focusX + focusX^2 + focusY^2 - directrixY^2) / (-2 directrixY + 2 focusY)
            var numerator   = rx * rx - 2 * rx * focus.X + focus.X * focus.X + focus.Y * focus.Y - directrixY * directrixY;
            var denominator = -2 * directrixY + 2 * focus.Y;
            var ry          = numerator / denominator;

            return(new TVector2(rx, ry));
        }
Exemple #12
0
        void HandleAddCircleEvent(
            FortunesAlgorithmState state,
            Node leftParabola, Node centerParabola, Node rightParabola,
            TNumber sweepY)
        {
            var leftParabolaFocus   = ((ParabolaData)leftParabola.Data).Focus;
            var centerParabolaFocus = ((ParabolaData)centerParabola.Data).Focus;
            var rightParabolaFocus  = ((ParabolaData)rightParabola.Data).Focus;

            // center gets swallowed by left/right when sweep line hits bottom of foci circumcircle
            TVector2 circumcenter;
            TNumber  radius;

            MathUtil.FindCircumcircle(
                leftParabolaFocus, centerParabolaFocus, rightParabolaFocus,
                out circumcenter, out radius);

            // All three points have a circumcenter - the parabola-swallowing
            // circle event will only happen if edge rays point towards the circumcenter.
            Node leftAncestor, rightAncestor;

            centerParabola.FindDirectionalAncestors(out leftAncestor, out rightAncestor);

            var leftEdgeRayData               = (EdgeRayData)leftAncestor.Data;
            var rightEdgeRayData              = (EdgeRayData)rightAncestor.Data;
            var leftEdgeOriginToCircumcenter  = circumcenter - leftEdgeRayData.Origin;
            var rightEdgeOriginToCircumcenter = circumcenter - rightEdgeRayData.Origin;

            if (TVector2.Dot(leftEdgeOriginToCircumcenter, leftEdgeRayData.Direction) <= 0 ||
                TVector2.Dot(rightEdgeOriginToCircumcenter, rightEdgeRayData.Direction) <= 0)
            {
                return;
            }

            if (circumcenter.Y + radius > sweepY)
            {
                state.EventQueue.Enqueue(
                    new CircleEvent(
                        circumcenter,
                        radius,
                        centerParabola
                        ));
            }
        }
Exemple #13
0
        private void DrawBeachlineNode(Graphics g, Node node, TNumber sweepY, Node highlightedNode = null)
        {
            if (node == null)
            {
                return;
            }

            if (node.IsLeaf())
            {
                var parabolaData = (ParabolaData)node.Data;

                TNumber renderStartX = -Padding.Left, renderEndX = BoardSize.Width + Padding.Right;
                TNumber activeStartX = 0, activeEndX = BoardSize.Width;
                Node    leftEdgeRayNode, rightEdgeRayNode;
                node.FindDirectionalAncestors(out leftEdgeRayNode, out rightEdgeRayNode);
                if (leftEdgeRayNode != null)
                {
                    var leftEdgeRayData = (EdgeRayData)leftEdgeRayNode.Data;
                    var intersect       = MathUtil.ComputeRayParabolaIntersection(
                        leftEdgeRayData.Origin, leftEdgeRayData.Direction,
                        parabolaData.Focus, sweepY);
                    activeStartX = intersect.X;
                    g.DrawLine(_inProgressVoronoiEdgePen, leftEdgeRayData.Origin.X, leftEdgeRayData.Origin.Y, intersect.X, intersect.Y);
                }
                if (rightEdgeRayNode != null)
                {
                    var rightEdgeRayData = (EdgeRayData)rightEdgeRayNode.Data;
                    var intersect        = MathUtil.ComputeRayParabolaIntersection(
                        rightEdgeRayData.Origin, rightEdgeRayData.Direction,
                        parabolaData.Focus, sweepY);
                    activeEndX = intersect.X;
                    g.DrawLine(_inProgressVoronoiEdgePen, rightEdgeRayData.Origin.X, rightEdgeRayData.Origin.Y, intersect.X, intersect.Y);
                }
                DrawParabola(g, renderStartX, renderEndX, activeStartX, activeEndX, parabolaData.Focus, sweepY, node == highlightedNode);
            }
            else
            {
                DrawBeachlineNode(g, node.Left, sweepY, highlightedNode);
                DrawBeachlineNode(g, node.Right, sweepY, highlightedNode);
            }
        }
Exemple #14
0
        internal static MatrixL <TNumber> TMatrixToMatrixL(TNumber src)
        {
            if (src.obj.Type != BaseEntryType.Matrix)
            {
                return(null);
            }
            int r = src.Rows().ToInt32();
            int c = src.Cols().ToInt32();
            MatrixL <TNumber> res    = new MatrixL <TNumber>(r, c, new TNumber(0, 1));
            TMatrix           matrix = (TMatrix)src.obj;

            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    res[i, j] = matrix[i, j];
                }
            }

            return(res);
        }
Exemple #15
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session["UserLogin"] == null)
        {
            //这里也可以根据类型不同,返回适当的情况!
            Response.Write("NOLOGIN");
            Response.End();
            return;
        }

        TValidator tValidator = (TValidator)HttpContext.Current.Session["UserLogin"];

        ClientAdapt.Open();

        TVssService.Client client  = ClientAdapt.GetClient();
        TNumber            tNumber = client.GetNumber(tValidator);

        ClientAdapt.Close();

        Response.Write(JsonConvert.SerializeObject(tNumber));
        Response.End();
    }
        public bool TryEvaluateExpression(Entry value, Store context, out Entry result)
        {
            //list
            if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "list")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);

                TNumber tmp = Computation.NumericCalculation(arg1, context);
                TDouble n   = (TDouble)tmp.obj;
                //List<string> vector = new List<string>(Utilites.EntryMatrix2ArrStr(tmp.obj));
                //vector.RemoveAt(vector.Count - 1);
                //vector.RemoveAt(vector.Count - 1);
                //List<string> distinct = vector.Distinct().ToList();

                List <Term> answer = new List <Term>();
                if (n.D <= 0)
                {
                    answer.AddRange(TermsConverter.ToTerms(0.ToString()));
                }
                else
                {
                    for (int i = 0; i < n.D; i++)
                    {
                        answer.AddRange(TermsConverter.ToTerms("0"));
                    }

                    answer.AddRange(TermsConverter.ToTerms(n.D.ToString()));
                }

                answer.AddRange(TermsConverter.ToTerms(1.ToString()));
                answer.Add(new Term(Functions.Mat, TermType.Function, 2 + (int)n.D));

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            //listLength
            if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "listLength")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);

                List <string> vector = new List <string>(Utilites.EntryMatrix2ArrStr(arg1));

                List <Term> answer = new List <Term>();

                if (vector.Count <= 2)
                {
                    answer.AddRange(TermsConverter.ToTerms("0"));
                }
                else
                {
                    answer.AddRange(TermsConverter.ToTerms(vector[vector.Count - 2]));
                }

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            //listDistinct
            if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "listDistinct")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);

                TNumber tmp = Computation.NumericCalculation(arg1, context);

                List <string> vector = new List <string>(Utilites.EntryMatrix2ArrStr(tmp.obj));
                //vector.RemoveAt(vector.Count - 1);
                //vector.RemoveAt(vector.Count - 1);
                List <string> distinct = vector.Distinct().ToList();

                List <Term> answer = new List <Term>();
                foreach (string item in distinct)
                {
                    answer.AddRange(TermsConverter.ToTerms(item));
                }

                answer.AddRange(TermsConverter.ToTerms(distinct.Count.ToString()));
                answer.AddRange(TermsConverter.ToTerms(1.ToString()));
                answer.Add(new Term(Functions.Mat, TermType.Function, 2 + distinct.Count));

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            //listSortAsText
            if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "listSortAsText")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);

                TNumber tmp = Computation.NumericCalculation(arg1, context);

                BaseEntry be  = tmp.obj;
                Term[]    res = be.ToTerms(16, 16, FractionsType.Decimal, true);

                tmp = Computation.NumericCalculation(Entry.Create(res), context);

                List <string> vector = new List <string>(Utilites.EntryMatrix2ArrStr(tmp.obj));
                if (be.Type == BaseEntryType.Matrix)
                {
                    vector.RemoveAt(vector.Count - 1);
                    vector.RemoveAt(vector.Count - 1);
                    vector.Sort();
                }

                List <Term> answer = new List <Term>();
                foreach (string item in vector)
                {
                    answer.AddRange(TermsConverter.ToTerms(item));
                }

                answer.AddRange(TermsConverter.ToTerms(vector.Count.ToString()));
                answer.AddRange(TermsConverter.ToTerms(1.ToString()));
                answer.Add(new Term(Functions.Mat, TermType.Function, 2 + vector.Count));

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            //listAdd
            if (value.Type == TermType.Function && value.ArgsCount == 2 && value.Text == "listAdd")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);

                TNumber tmp, tmp1;
                TMatrix m;
                try
                {
                    tmp  = Computation.NumericCalculation(arg1, context);
                    tmp1 = Computation.NumericCalculation(arg2, context);
                    TNumber count = tmp.Rows();
                    m = tmp.Stack(new TNumber[] { new TNumber(0, 1) });
                    m[count.ToInt32(), 0] = tmp1;
                }
                catch
                {
                    tmp1    = Computation.NumericCalculation(arg2, context);
                    m       = new TMatrix(new TNumber[, ] {
                    });
                    m[0, 0] = tmp1;
                }

                result = Entry.Create(m.ToTerms());

                return(true);
            }

            //listNonZeros
            if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "listNonZeros")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);

                TNumber tmp = Computation.NumericCalculation(arg1, context);

                List <string> vector = new List <string>(Utilites.EntryMatrix2ArrStr(tmp.obj));
                vector.RemoveAt(vector.Count - 1);
                vector.RemoveAt(vector.Count - 1);
                IEnumerable <string> select = from t in vector // определяем каждый объект из teams как t
                                              where t != "0"   //фильтрация по критерию
                                              select t;        // выбираем объект
                List <string> nonZeros = new List <string>(select);
                List <Term>   answer   = new List <Term>();
                foreach (string item in nonZeros)
                {
                    answer.AddRange(TermsConverter.ToTerms(item));
                }

                answer.AddRange(TermsConverter.ToTerms(nonZeros.Count.ToString()));
                answer.AddRange(TermsConverter.ToTerms(1.ToString()));
                answer.Add(new Term(Functions.Mat, TermType.Function, 2 + nonZeros.Count));

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            //if (value.Type == TermType.Function && value.ArgsCount == 2 && value.Text == "listDistinct")
            //{
            //   Entry arg1 = Computation.Preprocessing(value.Items[0], context);
            //   Entry arg2 = Computation.Preprocessing(value.Items[1], context);

            //   int dir = Utilites.Entry2Int(arg2);
            //   TNumber tmp = Computation.NumericCalculation(arg1, context);

            //   List<string> vector = new List<string>(Utilites.EntryMatrix2ArrStr(tmp.obj));
            //   vector.RemoveAt(vector.Count - 1);
            //   vector.RemoveAt(vector.Count - 1);
            //   List<string> distinct = vector.Distinct().ToList();

            //   //string res = "null";

            //   List<Term> answer = new List<Term>();
            //   foreach (string item in distinct)
            //   {
            //      answer.AddRange(TermsConverter.ToTerms(item));
            //   }

            //   if (dir==0)
            //   {
            //      answer.AddRange(TermsConverter.ToTerms(distinct.Count.ToString()));
            //      answer.AddRange(TermsConverter.ToTerms(1.ToString()));
            //      answer.Add(new Term(Functions.Mat, TermType.Function, 2 + distinct.Count));
            //   }
            //   else
            //   {
            //      answer.AddRange(TermsConverter.ToTerms(1.ToString()));
            //      answer.AddRange(TermsConverter.ToTerms(distinct.Count.ToString()));
            //      answer.Add(new Term(Functions.Mat, TermType.Function, 2 + distinct.Count));
            //   }

            //   result = Entry.Create(answer.ToArray());
            //   return true;
            //}

            if (value.Type == TermType.Function && value.ArgsCount == 2 && value.Text == "listRemoveAt")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);

                BaseEntry be1 = tmp1.obj;
                BaseEntry be2 = tmp2.obj;

                List <string> vector = new List <string>(Utilites.EntryMatrix2ArrStr(be1));
                if (be1.Type == BaseEntryType.Matrix)
                {
                    vector.RemoveAt(vector.Count - 1);
                    vector.RemoveAt(vector.Count - 1);
                    vector.RemoveAt((int)be2.ToDouble() - 1);
                }

                List <Term> answer = new List <Term>();
                foreach (string item in vector)
                {
                    answer.AddRange(TermsConverter.ToTerms(item));
                }

                answer.AddRange(TermsConverter.ToTerms(vector.Count.ToString()));
                answer.AddRange(TermsConverter.ToTerms(1.ToString()));
                answer.Add(new Term(Functions.Mat, TermType.Function, 2 + vector.Count));

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "listRemoveRange")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                BaseEntry be1 = tmp1.obj;
                BaseEntry be2 = tmp2.obj;
                BaseEntry be3 = tmp3.obj;

                List <string> vector = new List <string>(Utilites.EntryMatrix2ArrStr(be1));
                if (be1.Type == BaseEntryType.Matrix)
                {
                    vector.RemoveAt(vector.Count - 1);
                    vector.RemoveAt(vector.Count - 1);
                    vector.RemoveRange((int)be2.ToDouble() - 1, (int)be3.ToDouble());
                }

                List <Term> answer = new List <Term>();
                foreach (string item in vector)
                {
                    answer.AddRange(TermsConverter.ToTerms(item));
                }

                answer.AddRange(TermsConverter.ToTerms(vector.Count.ToString()));
                answer.AddRange(TermsConverter.ToTerms(1.ToString()));
                answer.Add(new Term(Functions.Mat, TermType.Function, 2 + vector.Count));

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "listInsert")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                BaseEntry be1 = tmp1.obj;
                BaseEntry be2 = tmp2.obj;
                BaseEntry be3 = tmp3.obj;

                List <string> vector = new List <string>(Utilites.EntryMatrix2ArrStr(be1));

                if (be1.Type == BaseEntryType.Matrix)
                {
                    vector.RemoveAt(vector.Count - 1);
                    vector.RemoveAt(vector.Count - 1);
                    vector.Insert((int)be2.ToDouble() - 1, be3.ToString());
                }

                List <Term> answer = new List <Term>();
                foreach (string item in vector)
                {
                    answer.AddRange(TermsConverter.ToTerms(item));
                }

                answer.AddRange(TermsConverter.ToTerms(vector.Count.ToString()));
                answer.AddRange(TermsConverter.ToTerms(1.ToString()));
                answer.Add(new Term(Functions.Mat, TermType.Function, 2 + vector.Count));

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "listInsertRange")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                BaseEntry be1 = tmp1.obj;
                BaseEntry be2 = tmp2.obj;
                BaseEntry be3 = tmp3.obj;

                List <string> vector  = new List <string>(Utilites.EntryMatrix2ArrStr(be1));
                List <string> vector1 = new List <string>(Utilites.EntryMatrix2ArrStr(be3));
                if (be1.Type == BaseEntryType.Matrix)
                {
                    vector.RemoveAt(vector.Count - 1);
                    vector.RemoveAt(vector.Count - 1);
                    vector1.RemoveAt(vector.Count - 1);
                    vector1.RemoveAt(vector.Count - 1);
                    vector.InsertRange((int)be2.ToDouble() - 1, vector1);
                }

                List <Term> answer = new List <Term>();
                foreach (string item in vector)
                {
                    answer.AddRange(TermsConverter.ToTerms(item));
                }

                answer.AddRange(TermsConverter.ToTerms(vector.Count.ToString()));
                answer.AddRange(TermsConverter.ToTerms(1.ToString()));
                answer.Add(new Term(Functions.Mat, TermType.Function, 2 + vector.Count));

                result = Entry.Create(answer.ToArray());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "insertRows")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                for (int i = 0; i < tmp3.ToInt32(); i++)
                {
                    matrixL.InsertRow(tmp2.ToInt32() - 1);
                }

                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "insertRow")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                List <TNumber>    vectorL = Utilites.TMatrixToMatrixL(tmp3).ToList();
                matrixL.InsertRow(tmp2.ToInt32() - 1, vectorL);
                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "insertCols")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                for (int i = 0; i < tmp3.ToInt32(); i++)
                {
                    matrixL.InsertCol(tmp2.ToInt32() - 1);
                }
                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "insertCol")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                List <TNumber>    vectorL = Utilites.TMatrixToMatrixL(tmp3).ToList();
                matrixL.InsertCol(tmp2.ToInt32() - 1, vectorL);
                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }


            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "removeRows")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                for (int i = 0; i < tmp3.ToInt32(); i++)
                {
                    matrixL.RemoveRowAt(tmp2.ToInt32() - 1);
                }
                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }


            if (value.Type == TermType.Function && value.ArgsCount == 3 && value.Text == "removeCols")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                for (int i = 0; i < tmp3.ToInt32(); i++)
                {
                    matrixL.RemoveColAt(tmp2.ToInt32() - 1);
                }
                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }


            if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "nonZerosRows")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                List <int>        indexes = new List <int>(matrixL.R);
                List <TNumber>    row;
                List <double>     rowD;
                int j = 0;
                for (int i = 0; i < matrixL.R; i++)
                {
                    row  = matrixL.Row(i);
                    rowD = new List <double>(matrixL.C);
                    foreach (TNumber item in row)
                    {
                        if (item.obj.Type != BaseEntryType.Double)
                        {
                            break;
                        }
                        //if (item.obj.ToDouble() != 0) break;
                        try
                        {
                            rowD.Add(item.obj.ToDouble());
                        }
                        catch
                        {
                            break;
                        }
                    }
                    rowD = rowD.Distinct().ToList();
                    if (rowD.Count == 1 && rowD[0] == 0)
                    {
                        indexes.Add(i - j);
                        j++;
                    }
                }
                if (indexes.Count > 0)
                {
                    foreach (int item in indexes)
                    {
                        matrixL.RemoveRowAt(item);
                    }
                }
                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "nonZerosRowsCols")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                List <int>        indexes = new List <int>(matrixL.R);
                List <TNumber>    row;
                List <double>     rowD;
                int j = 0;
                for (int i = 0; i < matrixL.R; i++)
                {
                    row  = matrixL.Row(i);
                    rowD = new List <double>(matrixL.C);
                    foreach (TNumber item in row)
                    {
                        if (item.obj.Type != BaseEntryType.Double)
                        {
                            break;
                        }
                        //if (item.obj.ToDouble() != 0) break;
                        try
                        {
                            rowD.Add(item.obj.ToDouble());
                        }
                        catch
                        {
                            break;
                        }
                    }
                    rowD = rowD.Distinct().ToList();
                    if (rowD.Count == 1 && rowD[0] == 0)
                    {
                        indexes.Add(i - j);
                        j++;
                    }
                }
                if (indexes.Count > 0)
                {
                    foreach (int item in indexes)
                    {
                        matrixL.RemoveRowAt(item);
                    }
                }

                indexes = new List <int>(matrixL.C);
                List <TNumber> col;
                List <double>  colD;
                j = 0;
                for (int i = 0; i < matrixL.C; i++)
                {
                    col  = matrixL.Col(i);
                    colD = new List <double>(matrixL.R);
                    foreach (TNumber item in col)
                    {
                        if (item.obj.Type != BaseEntryType.Double)
                        {
                            break;
                        }
                        //if (item.obj.ToDouble() != 0) break;
                        try
                        {
                            colD.Add(item.obj.ToDouble());
                        }
                        catch
                        {
                            break;
                        }
                    }
                    colD = colD.Distinct().ToList();
                    if (colD.Count == 1 && colD[0] == 0)
                    {
                        indexes.Add(i - j);
                        j++;
                    }
                }
                if (indexes.Count > 0)
                {
                    foreach (int item in indexes)
                    {
                        matrixL.RemoveColAt(item);
                    }
                }
                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }

            if (value.Type == TermType.Function && value.ArgsCount == 1 && value.Text == "nonZerosCols")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);

                MatrixL <TNumber> matrixL = Utilites.TMatrixToMatrixL(tmp1);
                List <int>        indexes = new List <int>(matrixL.C);
                List <TNumber>    col;
                List <double>     colD;
                int j = 0;
                for (int i = 0; i < matrixL.C; i++)
                {
                    col  = matrixL.Col(i);
                    colD = new List <double>(matrixL.R);
                    foreach (TNumber item in col)
                    {
                        if (item.obj.Type != BaseEntryType.Double)
                        {
                            break;
                        }
                        //if (item.obj.ToDouble() != 0) break;
                        try
                        {
                            colD.Add(item.obj.ToDouble());
                        }
                        catch
                        {
                            break;
                        }
                    }
                    colD = colD.Distinct().ToList();
                    if (colD.Count == 1 && colD[0] == 0)
                    {
                        indexes.Add(i - j);
                        j++;
                    }
                }
                if (indexes.Count > 0)
                {
                    foreach (int item in indexes)
                    {
                        matrixL.RemoveColAt(item);
                    }
                }
                TMatrix m = Utilites.MatrixLToTMatrix(matrixL);

                result = Entry.Create(m.ToTerms());
                return(true);
            }


            if (value.Type == TermType.Function && value.ArgsCount == 4 && value.Text == "putMatrix")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);
                Entry arg4 = Computation.Preprocessing(value.Items[3], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);
                TNumber tmp4 = Computation.NumericCalculation(arg4, context);

                if (tmp3.ToInt32() < 1 || tmp4.ToInt32() < 1)
                {
                    throw new ArgumentException("Неверно задана индексация");
                }

                int adressR = tmp3.ToInt32() - 1;
                int adressC = tmp4.ToInt32() - 1;

                MatrixL <TNumber> matrixL1 = Utilites.TMatrixToMatrixL(tmp1);
                MatrixL <TNumber> matrixL2 = Utilites.TMatrixToMatrixL(tmp2);

                int n = matrixL1.R - adressR;
                int m = matrixL1.C - adressC;
                if (n > matrixL2.R)
                {
                    n = matrixL2.R;
                }
                if (m > matrixL2.C)
                {
                    m = matrixL2.C;
                }

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        matrixL1[adressR + i, adressC + j] = matrixL2[i, j];
                    }
                }

                TMatrix mat = Utilites.MatrixLToTMatrix(matrixL1);

                result = Entry.Create(mat.ToTerms());
                return(true);
            }


            if (value.Type == TermType.Function && value.ArgsCount == 4 && value.Text == "insertMatrix")
            {
                Entry arg1 = Computation.Preprocessing(value.Items[0], context);
                Entry arg2 = Computation.Preprocessing(value.Items[1], context);
                Entry arg3 = Computation.Preprocessing(value.Items[2], context);
                Entry arg4 = Computation.Preprocessing(value.Items[3], context);

                TNumber tmp1 = Computation.NumericCalculation(arg1, context);
                TNumber tmp2 = Computation.NumericCalculation(arg2, context);
                TNumber tmp3 = Computation.NumericCalculation(arg3, context);
                TNumber tmp4 = Computation.NumericCalculation(arg4, context);

                if (tmp3.ToInt32() < 1 || tmp4.ToInt32() < 1)
                {
                    throw new ArgumentException("Неверно задана индексация");
                }

                int adressR = tmp3.ToInt32() - 1;
                int adressC = tmp4.ToInt32() - 1;

                MatrixL <TNumber> matrixL1 = Utilites.TMatrixToMatrixL(tmp1);
                MatrixL <TNumber> matrixL2 = Utilites.TMatrixToMatrixL(tmp2);

                for (int i = 0; i < matrixL2.R; i++)
                {
                    matrixL1.InsertRow(adressR);
                }
                for (int i = 0; i < matrixL2.C; i++)
                {
                    matrixL1.InsertCol(adressC);
                }

                int n = matrixL1.R - adressR;
                int m = matrixL1.C - adressC;
                if (n > matrixL2.R)
                {
                    n = matrixL2.R;
                }
                if (m > matrixL2.C)
                {
                    m = matrixL2.C;
                }

                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j < m; j++)
                    {
                        matrixL1[adressR + i, adressC + j] = matrixL2[i, j];
                    }
                }

                TMatrix mat = Utilites.MatrixLToTMatrix(matrixL1);

                result = Entry.Create(mat.ToTerms());
                return(true);
            }


            result = null;
            return(false);
        }
Exemple #17
0
        public static void Main(string[] args)
        {
            var points = new HashSet <Vector2>
            {
                new Vector2(100, 100),
                new Vector2(200, 100),
                new Vector2(120, 150),
                new Vector2(180, 150)
            };

//            var scale = 1f;
//            var points = new HashSet<Vector2>
//            {
//                new Vector2(100 * scale, 100 * scale),
//                new Vector2(250 * scale, 100 * scale),
//                new Vector2(160 * scale, 150 * scale),
//                new Vector2(180 * scale, 160 * scale)
//            };

            var random = new Random(1);

            points = new HashSet <Vector2>(
                Enumerable.Range(0, 300)
                .Select(i => {
                var y = (TNumber)random.NextDouble() * 1200;
                var x = (TNumber)random.NextDouble() * 500 + y / 2;
                return(new TVector2(x, y));
            })
                );

//            points = new HashSet<TVector2>(
//                from i in Enumerable.Range(0, 4)
//                from j in Enumerable.Range(0, 4)
//                let shift = i % 2 == 0 ? 25 : 0
//                let spacing = 50
//                select new TVector2(j * spacing + shift, i * spacing)
//            );

            var renderer = FortunesAlgorithmRenderer.Create(points);
            var display  = ImageDisplay.CreateAndRunInBackground(renderer.ImageSize);

            var state = FortunesAlgorithmState.CreateAndInitialize(points);
            var fortunesAlgorithmExecutor = new FortunesAlgorithmExecutor();

//            // show end result
//            fortunesAlgorithmExecutor.ExecuteToCompletion(state);
//            display.AddFrame(renderer.Render(state));

            // animate
            var frames = new ObservableCollection <Bitmap>();

            frames.CollectionChanged += (s, e) => display.AddFrame((Bitmap)e.NewItems[0]);

            TNumber     sweepY = -renderer.Padding.Top;
            ISweepEvent sweepEvent;
            var         frame = 0;

            while (state.EventQueue.TryPeek(out sweepEvent))
            {
//                while (sweepY < sweepEvent.Y)
//                {
//                    TNumber stepSize = 1;
//                    var bottomY = renderer.BoardSize.Height + renderer.Padding.Bottom;
//                    if (sweepY > bottomY)
//                    {
//                        var speed = (TNumber)Math.Pow(sweepY - bottomY, 1.01);
//                        stepSize = Math.Max(speed, stepSize);
//                    }
//                    frames.Add(renderer.Render(state, sweepY));
//                    sweepY += stepSize;
//                }

                fortunesAlgorithmExecutor.ExecuteNextEvent(state);
                frame++;
                if (frame % 10 == 0)
                {
                    frames.Add(renderer.Render(state, sweepEvent.Y + 0.1f));
                }
                if (frames.Count == 115)
                {
                    var old     = renderer.ImageSize;
                    var padding = renderer.Padding = new Padding(3200, 3200, 3200, 3200);
                    renderer.ImageSize = new Size(renderer.BoardSize.Width + padding.Horizontal, renderer.BoardSize.Height + padding.Vertical);
                    renderer.Render(state, sweepEvent.Y + 0.1f).Save(@"V:\my-repositories\miyu\voronoi\image.png", ImageFormat.Png);
                    Environment.Exit(0);
                }

                if (state.EventQueue.Count <= 5)
                {
                    fortunesAlgorithmExecutor.ExecuteToCompletion(state);
                }
            }
//            while (sweepY < renderer.BoardSize.Height + renderer.Padding.Bottom)
//                frames.Add(renderer.Render(state, sweepY++));

            fortunesAlgorithmExecutor.ProcessCleanup(state);
            frames.Add(renderer.Render(state));

            if (!Directory.Exists("output"))
            {
                Directory.CreateDirectory("output");
            }

//            int frame = 0;
//            for (int i = 0; i < 10; i++)
//                frames.Last().Save($"output/{frame++}.gif", ImageFormat.Gif);
//            for (int i = frames.Count - 1; i >= 0; i -= 14)
//                frames[i].Save($"output/{frame++}.gif", ImageFormat.Gif);
//            for (int i = 0; i < frames.Count; i += 3)
//                frames[i].Save($"output/{frame++}.gif", ImageFormat.Gif);

//            using (var outputGifStream = File.OpenWrite("output.gif"))
//            using (var gifEncoder = new GifEncoder(
//                    outputGifStream, renderer.ImageSize.Width, renderer.ImageSize.Height))
//            {
//                gifEncoder.AddFrame(frames.Last(), TimeSpan.FromMilliseconds(1000));
//                for (int i = frames.Count - 1; i >= 0; i -= 14)
//                    gifEncoder.AddFrame(frames[i], TimeSpan.FromMilliseconds(10));
//                for (int i = 0; i < frames.Count; i += 3)
//                    gifEncoder.AddFrame(frames[i], TimeSpan.FromMilliseconds(10));
//            }
//            using (MagickImageCollection collection = new MagickImageCollection())
//            {
//                collection.Add(new MagickImage(frames.Last()));
//                collection[collection.Count - 1].AnimationDelay = 1000;
//
//                for (int i = frames.Count - 1; i >= 0; i -= 10)
//                {
//                    collection.Add(new MagickImage(frames[i]));
//                    collection[collection.Count - 1].AnimationDelay = 10;
//                }
//                for (int i = 0; i < frames.Count; i++)
//                {
//
//                    collection.Add(new MagickImage(frames[i]));
//                    collection[collection.Count - 1].AnimationDelay = 10;
//                }
//            }
        }
Exemple #18
0
        public static TVector2 ComputeRayParabolaIntersection(
            TVector2 rayOrigin, TVector2 rayDirection,
            TVector2 parabolaFocus, TNumber directrixY)
        {
            if (ApproximatelyEqual(0, rayDirection.X))
            {
                var     x = rayOrigin.X;
                TNumber sa, sb, sc;
                ComputeParabolaStandardForm(parabolaFocus, directrixY, out sa, out sb, out sc);
                return(new Vector2(x, sa * x * x + sb * x + sc));
            }

            if (MathUtil.ApproximatelyEqual(directrixY, parabolaFocus.Y))
            {
                directrixY += 0.2f;
            }

            // Given an intersecting ray R and parabola P, find the first point of intersection.
            //
            // Let ro be the ray origin point (rox, roy)
            //     rd be the ray direction vector (rdx, rdy)
            //     pf be the parabola focus point (pfx, pfy)
            //     dy be the directrix y-coordinate
            //
            // Let t be the ray parameterization variable in [0, inf)
            //     s be the solution point (sx, sy), s = ro + rd * t
            //
            // By definition of parabola:
            //     dy - sy = |s - f|
            //     (dy - sy)^2 = |s - f|^2
            //
            // Expand (dy - sy)^2:
            //     (dy - sy)^2
            //     (dy - (roy + rdy * t))^2
            //     ((dy - roy) - rdy * t)^2
            //     (dy - roy)^2 - 2 (dy - roy) (rdy * t) + (rdy * t)^2
            //     (dy - roy)^2 - 2 (dy - roy) (rdy) t + (rdy^2) t^2
            //
            // Expand |s - f|^2:
            //     (sx - fx)^2 + (sy - fy)^2
            //     ((rox + rdx * t) - fx)^2 + ((roy + rdy * t) - fy)^2
            //
            //     Expand ((rox + rdx * t) - fx)^2:
            //         ((rdx * t) + (rox - fx))^2
            //         (rdx * t)^2 + 2 (rdx * t) (rox - fx) + (rox - fx)^2
            //         (rdx^2) t^2 + t (2 rdx (rox - fx)) + (rox - fx)^2
            //
            //     Expand ((roy + rdy * t) - fy)^2: (likewise)
            //         (rdy^2) t^2 + t (2 rdy (roy - fy)) + (roy - fy)^2
            //
            //     (rdx^2) t^2 + t (2 rdx (rox - fx)) + (rox - fx)^2 + (rdy^2) t^2 + t (2 rdy (roy - fy)) + (roy - fy)^2
            //     (rdx^2 + rdy^2) t^2 + t (2 rdx (rox - fx) + 2 rdy (roy - fy)) + (rox - fx)^2 + (roy - fy)^2
            //
            // Back to: dy - sy = |s - f|
            //     (dy - sy)^2 = |s - f|^2
            //
            //     (dy - roy)^2 - 2 (dy - roy) (rdy) t + (rdy^2) t^2 =
            //         (rdx^2 + rdy^2) t^2 + t (2 rdx (rox - fx) + 2 rdy (roy - fy)) + (rox - fx)^2 + (roy - fy)^2
            //
            //     0 = t^2 (rdx^2 + rdy^2 - rdy^2) +
            //         t   (2 rdx (rox - fx) + 2 rdy (roy - fy) + 2 (dy - roy) (rdy)) +
            //         1   ((rox - fx)^2 + (roy - fy)^2 - (dy - roy)^2)
            //
            //     0 = t^2 (rdx^2) +
            //         t   (2 rdx rox - 2 rdx fx + 2 rdy roy - 2 rdy fy + 2 rdy dy - 2 rdy roy) +
            //         1   (rox^2 - 2 rox fx + fx^2 + roy^2 - 2 roy fy + fy^2 - dy^2 + 2 dy roy - roy^2)
            //
            //     0 = t^2 (rdx^2) +
            //         t   (2 rdx rox - 2 rdx fx - 2 rdy fy + 2 rdy dy) +
            //         1   (rox^2 - 2 rox fx + fx^2 - 2 roy fy + fy^2 - dy^2 + 2 dy roy)

            var roxMinusFx = rayOrigin.X - parabolaFocus.X;
            var royMinusFy = rayOrigin.Y - parabolaFocus.Y;
            var dyMinusRoy = directrixY - rayOrigin.Y;
            var a          = rayDirection.X * rayDirection.X;
            var b          = 2 * (rayDirection.X * roxMinusFx
                                  - rayDirection.Y * (parabolaFocus.Y - directrixY));
            var     c       = roxMinusFx * roxMinusFx + royMinusFy * royMinusFy - dyMinusRoy * dyMinusRoy;
            TNumber pickedT = default(TNumber);

            if (!MathUtil.ApproximatelyEqual(0, a))
            {
                var centerT = -b / (2 * a);
                var offsetT = (TNumber)Math.Sqrt(b * b - 4 * a * c) / (2 * a);
                var lowerT  = centerT - offsetT;
                var upperT  = centerT + offsetT;
                pickedT = lowerT >= 0 ? lowerT : upperT;
            }
            if (TNumber.IsNaN(pickedT))
            {
//                Debugger.Break();
                pickedT = 0;
            }
            return(rayOrigin + pickedT * rayDirection);
        }
        public void DataTypeTest()
        {
            TestDB db;

            //db = new TestDB("DATA SOURCE=vpc1;PERSIST SECURITY INFO=True;USER ID=SYSTEM;PASSWORD=TEST")
            //         {
            //             Log = Console.Out
            //         };
            //if (db.DatabaseExists())
            //{
            //    db.DeleteDatabase();
            //}
            //db.CreateDatabase();

            db = new TestDB(GetProviderType())
            {
                Log = Console.Out
            };
            //var guid = new Guid("0ce0823c-a758-4da2-8aa2-25aa3830684d");
            //var item = new T1
            //               {
            //                   PK = db.T1.Max(o => o.PK) + 1,
            //                   Blob = Encoding.Default.GetBytes("Hello World"),
            //                   Guid1 = guid,
            //                   Guid2 = guid,
            //                   OracleTimeStampTZ = new DateTime(1999, 11, 1),
            //                   OracleTimeStampLTZ = new DateTime(1999, 11, 1, 1, 1, 1)
            //               };
            //db.T1.InsertOnSubmit(item);
            //db.SubmitChanges();
            //var id = item.PK;
            //item = db.T1.Where(o => o.PK == id).Single();

            //Assert.AreEqual(item.OracleTimeStampTZ, new DateTime(1999, 11, 1));
            var contactsXML = new XElement("Contacts",
                                           new XElement("Contact", new XElement("Name", "Patrick Hines"),
                                                        new XElement("Phone", "206-555-0144")),
                                           new XElement("Contact",
                                                        new XElement("Name", "Ellen Adams"),
                                                        new XElement("Phone", "206-555-0155")));

            {
                var item = new TString();
                item.XmlType = contactsXML;
                db.TString.InsertOnSubmit(item);
                db.SubmitChanges();
                //db.TString.Where(o => o.NVARCHAR2 != null).Select(o => o.NVARCHAR2.Substring(10)).ToList();
                var id = db.TString.Max(o => o.ID);
                //var result = db.TString.Where(o => o.XmlType != null)
                //                       .Select(o => o.XmlType.Element("Contact")).ToList();
            }

            {
                var item = new TNumber();
                item.Char = 'A';
                db.TNumber.InsertOnSubmit(item);
                db.SubmitChanges();

                item = db.TNumber.Where(o => o.PK == item.PK).Single();
                Assert.AreEqual('A', item.Char);
            }
        }
Exemple #20
0
 public CircleEvent(TVector2 circumcenter, TNumber radius, Node swallowedParabolaNode)
 {
     Circumcenter          = circumcenter;
     Radius                = radius;
     SwallowedParabolaNode = swallowedParabolaNode;
 }
Exemple #21
0
        public static TVector2 ComputeRayPointAtY(TVector2 rayOrigin, TVector2 rayDirection, TNumber y)
        {
            var dy = y - rayOrigin.Y;

            return(rayOrigin + (rayDirection * dy) / rayDirection.Y);
        }
Exemple #22
0
        public static void FindCircumcircle(TVector2 p1, TVector2 p2, TVector2 p3, out TVector2 circumcenter, out TNumber radius)
        {
            var p1DotP1 = p1.X * p1.X + p1.Y * p1.Y;
            var p2DotP2 = p2.X * p2.X + p2.Y * p2.Y;
            var p3DotP3 = p3.X * p3.X + p3.Y * p3.Y;
            var a       = Det3(
                p1.X, p1.Y, 1,
                p2.X, p2.Y, 1,
                p3.X, p3.Y, 1
                );
            var bx = -Det3(
                p1DotP1, p1.Y, 1,
                p2DotP2, p2.Y, 1,
                p3DotP3, p3.Y, 1
                );
            var by = Det3(
                p1DotP1, p1.X, 1,
                p2DotP2, p2.X, 1,
                p3DotP3, p3.X, 1
                );
            var c = -Det3(
                p1DotP1, p1.X, p1.Y,
                p2DotP2, p2.X, p2.Y,
                p3DotP3, p3.X, p3.Y
                );

            radius       = (TNumber)Math.Sqrt(bx * bx + by * by - 4 * a * c) / (2 * Math.Abs(a));
            circumcenter = new TVector2(-bx / (2 * a), -by / (2 * a));
        }
Exemple #23
0
 public IntVector2(TNumber x, TNumber y)
 {
     X = x;
     Y = y;
 }