/// <summary>
        /// Goal must be slope or intercept
        /// </summary>
        /// <param name="pt"></param>
        /// <param name="goal"></param>
        /// <returns></returns>
        public static LineSymbol GenerateLine(Point pt, double?slope, double?intercept)
        {
            Debug.Assert(pt.Concrete);
            double X, Y;

            LogicSharp.IsDouble(pt.XCoordinate, out X);
            LogicSharp.IsDouble(pt.YCoordinate, out Y);

            if (slope != null)
            {
                double intercept1 = Y - slope.Value * X;
                var    line       = new Line(slope, intercept1);
                line.InputType = LineType.Relation;
                return(new LineSymbol(line));
            }
            if (intercept != null)
            {
                double slope1 = 0.0;
                slope1 = (Y - intercept.Value) / X;
                var line = new Line(slope1, intercept);
                line.InputType = LineType.Relation;
                return(new LineSymbol(line));
            }
            throw new Exception("Cannot reach here!");
        }
Example #2
0
        public Line(string label, object slope, object intercept)
            : base(ShapeType.Line, label)
        {
            InputType = LineType.SlopeIntercept;

            _slope     = slope;
            _intercept = intercept;

            double d;

            if (LogicSharp.IsDouble(_slope, out d))
            {
                _slope = Math.Round(d, 2);
            }
            if (LogicSharp.IsDouble(_intercept, out d))
            {
                _intercept = Math.Round(d, 2);
            }

            if (_slope is string)
            {
                _slope = new Var(_slope);
            }
            if (_intercept is string)
            {
                _intercept = new Var(_intercept);
            }
            if (_intercept == null)
            {
                _intercept = 0.0d;
            }

            Calc_SlopeIntercept_General();
            PropertyChanged += Line_PropertyChanged;
        }
        private static bool SatisfyGeneralForm(Equation equation,
                                               out Circle circle)
        {
            circle = null;
            var term = equation.Lhs as Term;

            if (term != null && term.Op.Method.Name.Equals("Add"))
            {
                var lst = term.Args as List <object>;
                if (lst != null && lst.Count == 3)
                {
                    bool isNum = LogicSharp.IsNumeric(lst[2]);

                    if (isNum)
                    {
                        double dNum;
                        LogicSharp.IsDouble(lst[2], out dNum);
                        dNum *= -1;
                        object coeffX, coeffY;
                        bool   xTerm1 = IsXSquareTerm(lst[0], out coeffX);
                        bool   yTerm1 = IsYSquareTerm(lst[1], out coeffY);
                        if (xTerm1 && yTerm1)
                        {
                            var pt = new Point(coeffX, coeffY);
                            circle = new Circle(pt, Math.Pow(dNum, 0.5));
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
Example #4
0
        public static Expr ToCoord(string coord)
        {
            int  number;
            bool result = LogicSharp.IsInt(coord, out number);

            if (result)
            {
                if (number < 0)
                {
                    var abs = Math.Abs(number);
                    return(new CompositeExpr(WellKnownSym.minus,
                                             new Expr[] { new IntegerNumber(abs) }));
                }
                return(new IntegerNumber(coord));
            }

            double dNumber;

            result = LogicSharp.IsDouble(coord, out dNumber);

            if (result)
            {
                if (number < 0)
                {
                    var abs = Math.Abs(number);
                    return(new CompositeExpr(WellKnownSym.minus,
                                             new Expr[] { new DoubleNumber(abs) }));
                }
                return(new DoubleNumber(dNumber));
            }



            return(new WordSym(coord));
        }
Example #5
0
        private void Calc_SlopeIntercept_General()
        {
            //slope and intercept known
            Debug.Assert(Slope != null);
            Debug.Assert(Intercept != null);


            double dSlope;
            double dIntercept;
            bool   cond1 = LogicSharp.IsDouble(Slope, out dSlope);
            bool   cond2 = LogicSharp.IsDouble(Intercept, out dIntercept);

            if (cond1 && cond2)
            {
                if (dSlope > 0.0)
                {
                    A = Slope;
                    B = -1;
                    C = Intercept;
                }
                else
                {
                    A = -1 * dSlope;
                    B = 1;
                    C = -1 * dIntercept;
                }
            }
        }
        private static bool SatisfySpecialForm(Equation equation, out Circle circle)
        {
            circle = null;

            bool isRhsNum = LogicSharp.IsNumeric(equation.Rhs);

            if (!isRhsNum)
            {
                return(false);
            }
            double dNum;

            LogicSharp.IsDouble(equation.Rhs, out dNum);
            var term = equation.Lhs as Term;

            if (term == null)
            {
                return(false);
            }
            if (!term.Op.Method.Name.Equals("Add"))
            {
                return(false);
            }
            var lst = term.Args as List <object>;

            if (lst == null || lst.Count != 2)
            {
                return(false);
            }

            object coeffX, coeffY;
            bool   xTerm = IsXSquareTerm(lst[0], out coeffX);
            bool   yTerm = IsYSquareTerm(lst[1], out coeffY);

            if (xTerm && yTerm)
            {
                var pt = new Point(coeffX, coeffY);
                circle = new Circle(pt, Math.Pow(dNum, 0.5));
                return(true);
            }
            xTerm = false;
            yTerm = false;
            xTerm = IsXSquareTerm(lst[1], out coeffX);
            yTerm = IsYSquareTerm(lst[0], out coeffY);
            if (xTerm && yTerm)
            {
                var pt = new Point(coeffX, coeffY);
                circle = new Circle(pt, Math.Pow(dNum, 0.5));
                return(true);
            }
            return(false);
        }
Example #7
0
        public override string ToString()
        {
            var circle = Shape as Circle;

            Debug.Assert(circle != null);

            var builder = new StringBuilder();

            builder.Append("(x");

            double dNum;
            bool   isDouble = LogicSharp.IsDouble(circle.CenterPt.XCoordinate, out dNum);

            if (isDouble)
            {
                if (dNum > 0)
                {
                    builder.Append("-").Append(dNum);
                }
                else
                {
                    builder.Append("+").Append(Math.Abs(dNum));
                }
            }
            else
            {
                builder.Append("-").Append(circle.CenterPt.XCoordinate);
            }
            builder.Append(")^2+");

            builder.Append("(y");
            isDouble = LogicSharp.IsDouble(circle.CenterPt.YCoordinate, out dNum);
            if (isDouble)
            {
                if (dNum > 0)
                {
                    builder.Append("-").Append(dNum);
                }
                else
                {
                    builder.Append("+").Append(Math.Abs(dNum));
                }
            }
            else
            {
                builder.Append("-").Append(circle.CenterPt.YCoordinate);
            }
            builder.Append(")^2");
            builder.Append("=").Append(circle.Radius).Append("^2");

            return(builder.ToString());
        }
Example #8
0
        public Line(string label, object a, object b, object c)
            : base(ShapeType.Line, label)
        {
            InputType = LineType.GeneralForm;
            _a        = a;
            _b        = b;
            _c        = c;

            double d;

            if (LogicSharp.IsDouble(a, out d))
            {
                _a = Math.Round(d, 1);
            }

            if (LogicSharp.IsDouble(b, out d))
            {
                _b = Math.Round(d, 1);
            }

            if (LogicSharp.IsDouble(c, out d))
            {
                _c = Math.Round(d, 1);
            }

            if (_a is string)
            {
                _a = new Var(_a);
            }
            if (_b is string)
            {
                _b = new Var(_b);
            }
            if (_c is string)
            {
                _c = new Var(_c);
            }

            if (_c == null)
            {
                _c = 0.0d;
            }

            Calc_General_SlopeIntercept();
            PropertyChanged += Line_PropertyChanged;
        }
Example #9
0
        public Point(string label, object xcoordinate, object ycoordinate)
            : base(ShapeType.Point, label)
        {
            _xCoord = xcoordinate;
            _yCoord = ycoordinate;

            double d;

            if (LogicSharp.IsDouble(xcoordinate, out d))
            {
                _xCoord = Math.Round(d, 1);
            }

            if (LogicSharp.IsDouble(ycoordinate, out d))
            {
                _yCoord = Math.Round(d, 1);
            }
        }
        private static bool IsYWithConstTerm(object obj, out object coeff)
        {
            coeff = null;
            var term = obj as Term;

            if (term == null)
            {
                return(false);
            }

            if (term.Op.Method.Name.Equals("Add") ||
                term.Op.Method.Name.Equals("Substract"))
            {
                var lst = term.Args as List <object>;
                if (lst == null)
                {
                    return(false);
                }
                bool isYTerm = IsYTerm(lst[0], out coeff);
                if (coeff == null || !coeff.ToString().Equals("1"))
                {
                    return(false);
                }
                if (!isYTerm)
                {
                    return(false);
                }
                bool isRhsNum = LogicSharp.IsNumeric(lst[1]);
                if (!isRhsNum)
                {
                    return(false);
                }
                double dNum;
                LogicSharp.IsDouble(lst[1], out dNum);
                if (term.Op.Method.Name.Equals("Add"))
                {
                    dNum *= -1;
                }
                coeff = dNum;
                return(true);
            }
            return(false);
        }
        public static PointSymbol GenerateMidPoint(Point pt1, Point pt2)
        {
            if (pt1.Equals(pt2))
            {
                return(null);
            }
            Debug.Assert(pt1.Concrete);
            Debug.Assert(pt2.Concrete);
            double p1x, p1y, p2x, p2y;

            LogicSharp.IsDouble(pt1.XCoordinate, out p1x);
            LogicSharp.IsDouble(pt1.YCoordinate, out p1y);
            LogicSharp.IsDouble(pt2.XCoordinate, out p2x);
            LogicSharp.IsDouble(pt2.YCoordinate, out p2y);
            var midX     = (p1x + p2x) / 2;
            var midY     = (p1y + p2y) / 2;
            var midPoint = new Point(midX, midY);

            return(new PointSymbol(midPoint));
        }
Example #12
0
        /// <summary>
        /// construct a line through a point and a goal,
        /// e.g A(1,2) ^ S = 2=> Conjunctive Norm Form
        /// </summary>
        /// <param name="pt1"></param>
        /// <param name="goal"></param>
        /// <returns></returns>
        public static LineSymbol Unify(PointSymbol pt, EqGoal goal)
        {
            var variable1 = goal.Lhs as Var;

            Debug.Assert(variable1 != null);

            if (LineAcronym.EqualSlopeLabels(variable1.ToString()))
            {
                double dValue;
                bool   result = LogicSharp.IsDouble(goal.Rhs, out dValue);
                if (result)
                {
                    if (!pt.Shape.Concrete)
                    {
                        return(null);
                    }
                    var line = LineGenerationRule.GenerateLine((Point)pt.Shape, dValue, null);
                    if (pt.Traces.Count != 0)
                    {
                        line.Traces.AddRange(pt.Traces);
                    }
                    if (goal.Traces.Count != 0)
                    {
                        line.Traces.AddRange(goal.Traces);
                    }

                    TraceInstructionalDesign.FromPointSlopeToLine(pt, goal, line);

                    line.OutputType = LineType.SlopeIntercept;
                    return(line);
                }
                else
                {
                    var line = new Line(null); //ghost line
                    var ls   = new LineSymbol(line);
                    ls.OutputType = LineType.SlopeIntercept;
                    return(ls);
                }
            }
            return(null);
        }
Example #13
0
        private static bool SatisfySpecialForm(Equation equation, out Line line)
        {
            line = null;

            bool isRhsNum = LogicSharp.IsNumeric(equation.Rhs);

            if (!isRhsNum)
            {
                return(false);
            }

            double dNum;

            LogicSharp.IsDouble(equation.Rhs, out dNum);

            double rhs = -1 * dNum;
            object xCoeff;
            bool   result = IsXTerm(equation.Lhs, out xCoeff);

            if (result)
            {
                line = new Line(null, xCoeff, null, rhs);
                return(true);
            }
            object yCoeff;

            result = IsYTerm(equation.Lhs, out yCoeff);
            if (result)
            {
                line = new Line(null, null, yCoeff, rhs);
                return(true);
            }
            result = IsXYTerm(equation.Lhs, out xCoeff, out yCoeff);
            if (result)
            {
                line = new Line(null, xCoeff, yCoeff, rhs);
                return(true);
            }
            return(false);
        }
        public static object Unify(this LineSegmentSymbol lss, object constraint)
        {
            var label  = constraint as string;
            var eqGoal = constraint as EqGoal;

            //forward solving
            if (label != null)
            {
                switch (label)
                {
                case LineSegmentAcronym.Distance1:
                case LineSegmentAcronym.Distance2:
                    return(lss.InferDistance(label));
                }
            }

            //backward solving
            if (eqGoal != null)
            {
                var    rhs = eqGoal.Rhs;
                double dNum;
                bool   isDouble = LogicSharp.IsDouble(rhs, out dNum);
                if (!isDouble)
                {
                    return(null);
                }

                var lhs = eqGoal.Lhs.ToString();
                switch (lhs)
                {
                case LineSegmentAcronym.Distance1:
                case LineSegmentAcronym.Distance2:
                    return(lss.InferDistance(dNum));
                }
            }
            return(null);
        }
Example #15
0
        /// <summary>
        /// construct a line through two goals
        /// e.g  m=2, k=3 => conjunctive norm form
        /// </summary>
        /// <param name="goal1"></param>
        /// <param name="goal2"></param>
        /// <returns></returns>
        public static LineSymbol Unify(EqGoal goal1, EqGoal goal2)
        {
            var variable1 = goal1.Lhs as Var;
            var variable2 = goal2.Lhs as Var;

            Debug.Assert(variable1 != null);
            Debug.Assert(variable2 != null);

            var dict = new Dictionary <string, object>();

            string slopeKey     = "slope";
            string interceptKey = "intercept";

            if (LineAcronym.EqualSlopeLabels(variable1.ToString()))
            //if (variable1.ToString().Equals(LineAcronym.Slope1))
            {
                dict.Add(slopeKey, goal1.Rhs);
            }
            if (LineAcronym.EqualInterceptLabels(variable1.ToString()))
            //if (variable1.ToString().Equals(LineAcronym.Intercept1))
            {
                dict.Add(interceptKey, goal1.Rhs);
            }
            if (LineAcronym.EqualSlopeLabels(variable2.ToString()))
            //if (variable2.ToString().Equals(LineAcronym.Slope1))
            {
                if (dict.ContainsKey(slopeKey))
                {
                    return(null);
                }
                dict.Add(slopeKey, goal2.Rhs);
            }
            if (LineAcronym.EqualInterceptLabels(variable2.ToString()))
            //if (variable2.ToString().Equals(LineAcronym.Intercept1))
            {
                if (dict.ContainsKey(interceptKey))
                {
                    return(null);
                }
                dict.Add(interceptKey, goal2.Rhs);
            }

            if (dict.Count == 2 &&
                dict[slopeKey] != null &&
                dict[interceptKey] != null)
            {
                if (LogicSharp.IsNumeric(dict[slopeKey]) &&
                    LogicSharp.IsNumeric(dict[interceptKey]))
                {
                    double dSlope, dIntercept;
                    LogicSharp.IsDouble(dict[slopeKey], out dSlope);
                    LogicSharp.IsDouble(dict[interceptKey], out dIntercept);
                    var line = LineGenerationRule.GenerateLine(dSlope, dIntercept);
                    var ls   = new LineSymbol(line)
                    {
                        OutputType = LineType.SlopeIntercept
                    };

                    TraceInstructionalDesign.FromSlopeInterceptToLineSlopeIntercept(goal1, goal2, ls);
                    return(ls);
                }
                else
                {
                    //lazy evaluation
                    //Constraint solving on Graph
                    var line = new Line(null); //ghost line
                    var ls   = new LineSymbol(line);
                    ls.OutputType = LineType.SlopeIntercept;
                    return(ls);
                }
            }
            return(null);
        }
Example #16
0
        private static Line UnifyLineTerm(List <object> args, int index,
                                          Dictionary <string, object> dict)
        {
            if (index == args.Count)
            {
                object xCord = dict.ContainsKey(A) ? dict[A] : null;
                object yCord = dict.ContainsKey(B) ? dict[B] : null;
                object cCord = dict.ContainsKey(C) ? dict[C] : null;
                return(new Line(xCord, yCord, cCord));
            }

            object currArg     = args[index];
            bool   finalResult = false;

            object coeff;
            bool   result = IsXTerm(currArg, out coeff);

            if (result)
            {
                if (!dict.ContainsKey(A))
                {
                    dict.Add(A, coeff);
                    finalResult = true;
                }
            }
            result = IsYTerm(currArg, out coeff);
            if (result)
            {
                if (!dict.ContainsKey(B))
                {
                    dict.Add(B, coeff);
                    finalResult = true;
                }
            }
            double d;

            result = LogicSharp.IsDouble(currArg, out d);
            if (result)
            {
                if (!dict.ContainsKey(C))
                {
                    dict.Add(C, d);
                    finalResult = true;
                }
            }

            if (currArg is string)
            {
                if (!dict.ContainsKey(C))
                {
                    dict.Add(C, new Var(currArg));
                    finalResult = true;
                }
            }

            if (finalResult)
            {
                return(UnifyLineTerm(args, index + 1, dict));
            }
            else
            {
                return(null);
            }
        }
Example #17
0
        public static object TransformString2(string str)
        {
            char[] charArr = str.ToCharArray();
            if (charArr.Length == 1)
            {
                return(new Var(str));
            }
            bool isNeg = charArr[0].Equals('-');

            var lst = new List <object>();
            int j   = -1;

            for (int i = charArr.Count() - 1; i >= 0; i--)
            {
                if (Char.IsLetter(charArr[i]))
                {
                    lst.Insert(0, new Var(charArr[i]));
                }
                else
                {
                    j = i;
                    break;
                }
            }
            if (isNeg)
            {
                if (j == 0)
                {
                    var term1 = new Term(Expression.Multiply, new List <object>()
                    {
                        -1, lst[0]
                    });
                    lst[0] = term1;

                    if (lst.Count == 1)
                    {
                        return(lst[0]);
                    }
                    return(new Term(Expression.Multiply, lst));
                }
                var  subStr = str.Substring(1, j);
                bool result = LogicSharp.IsNumeric(subStr);
                if (!result)
                {
                    return(null);
                }

                int  iNum;
                bool result000 = LogicSharp.IsInt(subStr, out iNum);
                if (result000)
                {
                    lst.Insert(0, -1 * iNum);
                }
                else
                {
                    double dNum;
                    LogicSharp.IsDouble(subStr, out dNum);
                    lst.Insert(0, -1 * dNum);
                }
            }
            else
            {
                if (j == -1)
                {
                    if (lst.Count == 1)
                    {
                        return(lst[0]);
                    }
                    return(new Term(Expression.Multiply, lst));
                }
                var subStr = str.Substring(0, j + 1);

                bool result = LogicSharp.IsNumeric(subStr);
                if (!result)
                {
                    return(null);
                }

                int  iNum;
                bool result000 = LogicSharp.IsInt(subStr, out iNum);
                if (result000)
                {
                    lst.Insert(0, iNum);
                }
                else
                {
                    double dNum;
                    LogicSharp.IsDouble(subStr, out dNum);
                    lst.Insert(0, dNum);
                }
            }
            return(new Term(Expression.Multiply, lst));
        }
Example #18
0
        /// <summary>
        /// The purpose of parse string is to re-format the str
        /// x, 2x, -2x, ax, -ax,2ax, 3y,34y, y25
        /// </summary>
        /// <param name="str"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static object TransformString(string str)
        {
            char[] charArr = str.ToCharArray();
            if (charArr.Length == 1)
            {
                return(new Var(str));
            }

            bool   isNeg = false;
            string parseStr;

            if (charArr[0].Equals('-'))
            {
                isNeg    = true;
                parseStr = str.Substring(1, str.Length - 1);
            }
            else
            {
                parseStr = str;
            }

            //TODO tackle decimal number
            string[] strs = Regex.Split(parseStr, "(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
            //string[] strs = Regex.Split(parseStr, "(?<=\\D)(?=\\d)|(?<=(\\d+\\.\\d+))(?=\\D)");
            var lst = new List <object>();

            for (var i = 0; i < strs.Length; i++)
            {
                if (strs[i].Equals("."))
                {
                    throw new Exception("Cannot be decimal input");
                }

                if (i == 0)
                {
                    if (LogicSharp.IsNumeric(strs[i]))
                    {
                        int  iNum;
                        bool result000 = LogicSharp.IsInt(strs[i], out iNum);
                        if (result000)
                        {
                            iNum = isNeg ? iNum * -1 : iNum;
                            lst.Add(iNum);
                        }
                        else
                        {
                            double dNum;
                            LogicSharp.IsDouble(strs[i], out dNum);
                            dNum = isNeg ? dNum * -1 : dNum;
                            lst.Add(dNum);
                        }
                    }
                    else
                    {
                        char[] tempArr = strs[i].ToCharArray();
                        if (isNeg)
                        {
                            lst.Add(-1);
                        }
                        lst.AddRange(tempArr.Select(c => new Var(c)).Cast <object>());
                    }
                }
                else
                {
                    if (LogicSharp.IsNumeric(strs[i]))
                    {
                        int  iNum;
                        bool result000 = LogicSharp.IsInt(strs[i], out iNum);
                        if (result000)
                        {
                            lst.Add(iNum);
                        }
                        else
                        {
                            double dNum;
                            LogicSharp.IsDouble(strs[i], out dNum);
                            lst.Add(dNum);
                        }
                    }
                    else
                    {
                        char[] tempArr = strs[i].ToCharArray();
                        lst.AddRange(tempArr.Select(c => new Var(c)).Cast <object>());
                    }
                }
            }
            if (lst.Count == 1)
            {
                return(lst[0]);
            }
            return(new Term(Expression.Multiply, lst));
        }
Example #19
0
        //forward chaining
        public static void FromLineSegmentToDistance(LineSegmentSymbol lss)
        {
            //1. Substitute two points coordinates into the distance function.
            //2. Manipulate the expression to derive the goal.
            ////////////////////////////////////////////////////////
            //1.
            lss.Traces.Add(DistanceSubstitution(lss));

            ////////////////////////////////////////////////////////
            //2.
            var ls       = lss.Shape as LineSegment;
            var variable = new Var('d');
            var lhs      = new Term(Expression.Power, new List <object>()
            {
                variable, 2.0
            });
            var term1 = new Term(Expression.Subtract, new List <object>()
            {
                ls.Pt1.XCoordinate, ls.Pt2.XCoordinate
            });
            var term11 = new Term(Expression.Power, new List <object>()
            {
                term1, 2.0
            });
            var term2 = new Term(Expression.Subtract, new List <object>()
            {
                ls.Pt1.YCoordinate, ls.Pt2.YCoordinate
            });
            var term22 = new Term(Expression.Power, new List <object>()
            {
                term2, 2.0
            });
            var rhs = new Term(Expression.Add, new List <object>()
            {
                term11, term22
            });
            var eq = new Equation(lhs, rhs);

            object obj1;
            bool   result = eq.IsEqGoal(out obj1);
            EqGoal eqGoal = null;

            Debug.Assert(result);
            var lst1 = obj1 as List <object>;

            foreach (var temp in lst1)
            {
                var tempGoal = temp as EqGoal;
                if (tempGoal == null)
                {
                    continue;
                }
                double dNum;
                bool   tempResult = LogicSharp.IsDouble(tempGoal.Rhs, out dNum);
                if (tempResult && dNum > 0)
                {
                    eqGoal = tempGoal;
                    break;
                }
            }
            Debug.Assert(eqGoal != null);
            lss.Traces.AddRange(eqGoal.Traces);
        }
        public static LineSymbol GenerateLine(Point pt1, Point pt2)
        {
            if (pt1.Equals(pt2))
            {
                return(null);
            }

            Debug.Assert(pt1.Concrete);
            Debug.Assert(pt2.Concrete);

            /* if (pt1.XCoordinate.Equals(pt2.XCoordinate))
             * {
             *   double d;
             *   LogicSharp.IsDouble(pt1.XCoordinate, out d);
             *   var line1 = new Line(1, null, -1 * d);
             *   return new LineSymbol(line1);
             * }
             *
             * if (pt1.YCoordinate.Equals(pt2.YCoordinate))
             * {
             *   double d;
             *   LogicSharp.IsDouble(pt1.YCoordinate, out d);
             *   var line2 = new Line(null, 1, -1 * d);
             *   return new LineSymbol(line2);
             * }*/

            //Strategy: y = mx+b, find slope m and intercept b
            //step 1: calc slope

            double p1x, p1y, p2x, p2y;

            LogicSharp.IsDouble(pt1.XCoordinate, out p1x);
            LogicSharp.IsDouble(pt1.YCoordinate, out p1y);
            LogicSharp.IsDouble(pt2.XCoordinate, out p2x);
            LogicSharp.IsDouble(pt2.YCoordinate, out p2y);


            var a = p1y - p2y;
            var b = p2x - p1x;
            var c = (p1x - p2x) * p1y + (p2y - p1y) * p1x;

            int  intB;
            int  intC;
            bool cond1 = LogicSharp.IsInt(b / a, out intB);
            bool cond2 = LogicSharp.IsInt(c / a, out intC);

            if (cond1 && cond2)
            {
                a = 1;
                b = intB;
                c = intC;
            }

            if (a < 0.0d)
            {
                a = -1 * a;
                b = -1 * b;
                c = -1 * c;
            }

            var line = new Line(a, b, c)
            {
                InputType = LineType.Relation
            };

            return(new LineSymbol(line));
        }
Example #21
0
        public static void LineSlopeIntercepToGraph(LineSymbol ls)
        {
            string strategy = strategy_graphing;

            //Plotting shapes
            //1. plotting Y-Intercept
            //2. plotting X-Intercept
            //3. plotting the line

            //////////////////////////////////////////////////////////////
            // Step 1:
            var pt    = new Point(0, ls.SymIntercept);
            var ptSym = new PointSymbol(pt);

            var ts0 = new TraceStep(null, ptSym, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ptSym));

            ls._innerLoop.Add(ts0);
            //////////////////////////////////////////////////////////////
            // Step 2:

            var line = ls.Shape as Line;

            Debug.Assert(line != null);

            Equation eq;

            if (line.B == null)
            {
            }
            else
            {
                var x = new Var("x");
                //step 2.1
                var term = new Term(Expression.Multiply, new List <object>()
                {
                    line.Slope, x
                });
                var term1 = new Term(Expression.Add, new List <object>()
                {
                    term, line.Intercept
                });
                eq = new Equation(term1, 0);
                object obj;
                EqGoal gGoal  = null;
                bool   result = eq.IsEqGoal(out obj);
                if (result)
                {
                    gGoal = obj as EqGoal;
                }
                if (gGoal != null)
                {
                    double dX;
                    LogicSharp.IsDouble(gGoal.Rhs, out dX);
                    var pt1    = new Point(dX, 0);
                    var ptSym1 = new PointSymbol(pt1);
                    var ts1    = new TraceStep(null, ptSym1, null, PlottingRule.PlottingStrategy, PlottingRule.Plot(ptSym1));
                    ls._innerLoop.Add(ts1);
                }
            }

            /////////////////////////////////////////////////////////////////

            const string step1MetaRule    = "Given the line slope-intercept form y=mx+b, plot the line by passing points (0,b) and (-b/m,0).";
            string       step1AppliedRule = String.Format("Plotting the line passing through (0,{0}) and ({1},0) ", ls.SymIntercept, ls.SymC);
            //var ts = new TraceStep(null, ls.SlopeInterceptForm, step1MetaRule, step1AppliedRule);


            string kc = GeometryScaffold.KC_LineGraphing;

            var ts = new TraceStep(null, ls, kc, step1MetaRule, step1AppliedRule);

            ls._innerLoop.Add(ts);

            //////////////////////////////////////////////////////////////////

            ls.GenerateATrace(strategy);
        }
Example #22
0
        public static Expr Generate(object obj)
        {
            var boolValue = obj as bool?;

            if (boolValue != null)
            {
                return(new WordSym(boolValue.ToString()));
            }

            var ss = obj as ShapeSymbol;

            if (ss != null)
            {
                return(Generate(ss));
            }

            var eqGoal = obj as EqGoal;

            if (eqGoal != null)
            {
                return(Generate(eqGoal));
            }

            var term = obj as Term;

            if (term != null)
            {
                return(Generate(term));
            }

            var equation = obj as Equation;

            if (equation != null)
            {
                return(Generate(equation));
            }

            var variable = obj as Var;

            if (variable != null)
            {
                return(new WordSym(variable.Token.ToString()));
            }

            int integer;

            if (LogicSharp.IsInt(obj, out integer))
            {
                var integerExpr = new IntegerNumber(integer.ToString());
                if (integer < 0)
                {
                    return(new CompositeExpr(WellKnownSym.times, new Expr[] { integerExpr }));
                }
                else
                {
                    return(integerExpr);
                }
            }

            double dNumber;

            if (LogicSharp.IsDouble(obj, out dNumber))
            {
                var doubleExpr = new WordSym(dNumber.ToString());
                //var doubleExpr = new DoubleNumber(dNumber);

                if (dNumber < 0)
                {
                    return(new CompositeExpr(WellKnownSym.times, new Expr[] { doubleExpr }));
                }
                return(doubleExpr);
            }

            return(null);
        }
Example #23
0
        private static Line UnifyLineSlopeInterceptTerm(List <object> args, int index,
                                                        Dictionary <string, object> dict)
        {
            if (index == args.Count)
            {
                object slope     = dict.ContainsKey(A) ? dict[A] : null;
                object intercept = dict.ContainsKey(C) ? dict[C] : null;
                if (slope == null)
                {
                    return(null);
                }
                return(new Line(slope, intercept));
            }

            object currArg     = args[index];
            bool   finalResult = false;

            object coeff;
            bool   result = IsXTerm(currArg, out coeff);

            if (result)
            {
                if (dict.ContainsKey(A))
                {
                    throw new Exception("cannot contain two terms with same var");
                }
                dict.Add(A, coeff);
                finalResult = true;
            }
            double d;

            result = LogicSharp.IsDouble(currArg, out d);
            if (result)
            {
                if (dict.ContainsKey(C))
                {
                    dict[C] = d;
                }
                else
                {
                    dict.Add(C, d);
                }
                finalResult = true;
            }

            if (currArg is string)
            {
                if (dict.ContainsKey(C))
                {
                    dict[C] = currArg;
                }
                else
                {
                    dict.Add(C, new Var(currArg));
                }

                finalResult = true;
            }

            var variable = currArg as Var;

            if (variable != null)
            {
                if (dict.ContainsKey(C))
                {
                    dict[C] = variable;
                }
                else
                {
                    dict.Add(C, variable);
                }

                finalResult = true;
            }

            if (finalResult)
            {
                return(UnifyLineSlopeInterceptTerm(args, index + 1, dict));
            }
            else
            {
                return(null);
            }
        }
Example #24
0
        public static object Unify(this LineSymbol ls, object constraint)
        {
            var refObj = constraint as string;
            var eqGoal = constraint as EqGoal;

            if (refObj != null)
            {
                #region forward searching

                if (LineAcronym.EqualSlopeLabels(refObj))
                {
                    return(ls.InferSlope(refObj));
                }

                if (LineAcronym.EqualInterceptLabels(refObj))
                {
                    return(ls.InferIntercept(refObj));
                }

                if (LineAcronym.EqualGeneralFormLabels(refObj))
                {
                    return(ls.InferGeneralForm(refObj));
                }

                if (LineAcronym.EqualSlopeInterceptFormLabels(refObj))
                {
                    return(ls.InferSlopeInterceptForm());
                }

                if (LineAcronym.GraphLine.Equals(refObj))
                {
                    return(ls.InferGraph());
                }
                #endregion
            }

            if (eqGoal != null)
            {
                var    rhs = eqGoal.Rhs;
                double dNum;
                bool   isDouble = LogicSharp.IsDouble(rhs, out dNum);
                if (!isDouble)
                {
                    return(null);
                }

                var lhs = eqGoal.Lhs.ToString();

                if (LineAcronym.EqualSlopeLabels(lhs))
                {
                    var obj    = ls.InferSlope(dNum);
                    var lstObj = obj as List <object>;
                    Debug.Assert(lstObj != null);
                    var eqGoal1 = lstObj[0] as EqGoal;
                    if (eqGoal1 != null)
                    {
                        var newTraces = new List <Tuple <object, object> >();
                        newTraces.AddRange(eqGoal.Traces);
                        newTraces.AddRange(eqGoal1.Traces);
                        eqGoal1.Traces = newTraces;
                    }
                    return(obj);
                }
            }
            return(null);
        }