コード例 #1
0
        /// <summary>
        /// Create and add new control point to the given curve at a given Vec2F gpt</summary>
        /// <param name="curve">Curve to modify</param>
        /// <param name="gpt">Location on the x-axis at which to add a control point. Whether
        /// or not the y-coordinate is used depends on 'insert' parameter.</param>
        /// <param name="insert">If true, the y-value is inserted on the existing curve, using
        /// curve.Evaluate(gpt.X). If false, the y-value comes from gpt.Y.</param>
        /// <param name="computeTangent">Whether ComputeTangent() should be called</param>
        public static void AddControlPoint(ICurve curve, Vec2F gpt, bool insert, bool computeTangent)
        {
            if (curve == null)
            {
                throw new ArgumentNullException("curve");
            }

            // add/insert new control point to the given curve. Using the following rules.
            // if adding then add the point at gpt and set is tangent type to spline.
            // if inserting then add the point at (gpt.x,curve.eval(gpt.x)) and compute its tangent at the point.
            // recompute tangents for the curve.
            // find insersion index.
            int index = GetValidInsertionIndex(curve, gpt.X);

            if (index >= 0)
            {
                IControlPoint p = curve.CreateControlPoint();
                p.EditorData.SelectedRegion = PointSelectionRegions.None;
                if (insert)
                {
                    p.X = gpt.X;
                    ICurveEvaluator cv = CurveUtils.CreateCurveEvaluator(curve);

                    float prevY = cv.Evaluate(gpt.X - s_epsilone);
                    float nextY = cv.Evaluate(gpt.X + s_epsilone);
                    p.Y              = cv.Evaluate(gpt.X);
                    p.TangentInType  = CurveTangentTypes.Fixed;
                    p.TangentOutType = CurveTangentTypes.Fixed;
                    Vec2F tanIn = new Vec2F(s_epsilone, p.Y - prevY);
                    tanIn.Normalize();
                    Vec2F tanOut = new Vec2F(s_epsilone, nextY - p.Y);
                    tanOut.Normalize();
                    p.TangentIn  = tanIn;
                    p.TangentOut = tanOut;
                }
                else
                {
                    p.X              = gpt.X;
                    p.Y              = gpt.Y;
                    p.TangentInType  = CurveTangentTypes.Spline;
                    p.TangentOutType = CurveTangentTypes.Spline;
                }
                curve.InsertControlPoint(index, p);
                if (computeTangent)
                {
                    ComputeTangent(curve);
                }
            }
        }
コード例 #2
0
ファイル: CurveUtils.cs プロジェクト: BeRo1985/LevelEditor
        /// <summary>
        /// Create and add new control point to the given curve at a given Vec2F gpt</summary>
        /// <param name="curve">Curve to modify</param>
        /// <param name="gpt">Location on the x-axis at which to add a control point. Whether
        /// or not the y-coordinate is used depends on 'insert' parameter.</param>
        /// <param name="insert">If true, the y-value is inserted on the existing curve, using
        /// curve.Evaluate(gpt.X). If false, the y-value comes from gpt.Y.</param>
        /// <param name="computeTangent">Whether ComputeTangent() should be called</param>
        public static void AddControlPoint(ICurve curve, Vec2F gpt, bool insert, bool computeTangent)
        {
            if (curve == null)
                throw new ArgumentNullException("curve");

            // add/insert new control point to the given curve. Using the following rules.
            // if adding then add the point at gpt and set is tangent type to spline.
            // if inserting then add the point at (gpt.x,curve.eval(gpt.x)) and compute its tangent at the point.            
            // recompute tangents for the curve.
            // find insersion index.
            int index = GetValidInsertionIndex(curve, gpt.X);
            if (index >= 0)
            {
                IControlPoint p = curve.CreateControlPoint();
                p.EditorData.SelectedRegion = PointSelectionRegions.None;
                if (insert)
                {
                    p.X = gpt.X;
                    ICurveEvaluator cv = CurveUtils.CreateCurveEvaluator(curve);
                    
                    float prevY = cv.Evaluate(gpt.X - s_epsilone);
                    float nextY = cv.Evaluate(gpt.X + s_epsilone);
                    p.Y = cv.Evaluate(gpt.X);
                    p.TangentInType = CurveTangentTypes.Fixed;
                    p.TangentOutType = CurveTangentTypes.Fixed;
                    Vec2F tanIn = new Vec2F(s_epsilone, p.Y - prevY);
                    tanIn.Normalize();
                    Vec2F tanOut = new Vec2F(s_epsilone, nextY - p.Y);
                    tanOut.Normalize();
                    p.TangentIn = tanIn;
                    p.TangentOut = tanOut;                                                            
                }
                else
                {
                    p.X = gpt.X;
                    p.Y = gpt.Y;
                    p.TangentInType = CurveTangentTypes.Spline;
                    p.TangentOutType = CurveTangentTypes.Spline;
                }               
                curve.InsertControlPoint(index, p);
                if (computeTangent)
                    ComputeTangent(curve);
            }
        }