Click here to Skip to main content
15,891,607 members
Articles / Programming Languages / C#

User-driven applications

Rate me:
Please Sign up or sign in to vote.
4.88/5 (24 votes)
10 Apr 2010CPOL136 min read 33K   5   78  
User-driven applications are the programs in which full control is given to the users. Designers of such programs are responsible only for developing an instrument for solving some task, but they do not enforce users to work with this instrument according with a predefined scenario.
using System;
using System .Collections .Generic;
using System .Drawing;
using System .Windows .Forms;
using Microsoft .Win32;

using MoveGraphLibrary;

namespace UserDrivenApplications
{
    public enum FuncAuthor { Developer, User };

    public class FunctionDesigned
    {
        int version = 605;
        long id;
        string name;
        FuncAuthor origin;
        int iPredefined;                    // only for origin = FuncOrigin.FromDesigner
        double fX_L, fX_R, fY_T, fY_B;
        double fFrom, fTo, fStep;
        string str_Y;       // for Y(x) or Y(r)
        string str_X;       // used only for X(r)
        MSPlot msplot;
        List<Elem> Polish_Y = new List<Elem> ();
        List<Elem> Polish_X = new List<Elem> ();

        // -------------------------------------------------        constructor for Predefined functions
        public FunctionDesigned (string nm, int iNum, double xL, double xR, double yT, double yB)
        {
            id = Auxi_Common .UniqueID;
            name = CheckedName (nm);
            origin = FuncAuthor .Developer;
            iPredefined = iNum;
            fX_L = xL;
            fX_R = xR;
            fY_T = yT;
            fY_B = yB;
            str_X = "";
            str_Y = "";
            msplot = null;
        }
        // -------------------------------------------------        constructor for New functions Y(x)
        public FunctionDesigned (string nm, string strY, List<Elem> polishY, double xL, double xR, double yT, double yB)
        {
            id = Auxi_Common .UniqueID;
            name = CheckedName (nm);
            origin = FuncAuthor .User;
            iPredefined = -1;
            fX_L = xL;
            fX_R = xR;
            fY_T = yT;
            fY_B = yB;
            str_X = "";
            str_Y = strY;
            Polish_Y = polishY;
            msplot = null;
        }
        // -------------------------------------------------        constructor for New functions {X(r), Y(r)}
        public FunctionDesigned (string nm, string strX, string strY, List<Elem> polishX, List<Elem> polishY,
                               double xL, double xR, double yT, double yB, double from, double to, double step)
        {
            id = Auxi_Common .UniqueID;
            name = CheckedName (nm);
            origin = FuncAuthor .User;
            iPredefined = -1;
            fX_L = xL;
            fX_R = xR;
            fY_T = yT;
            fY_B = yB;
            fFrom = from;
            fTo = to;
            fStep = step;
            str_X = strX;
            str_Y = strY;
            Polish_X = polishX;
            Polish_Y = polishY;
            msplot = null;
        }
        // -------------------------------------------------        CheckedName
        public string CheckedName (string nm)
        {
            if (!string .IsNullOrEmpty (nm))
            {
                return (nm);
            }
            else
            {
                return ("No name");
            }
        }
        // -------------------------------------------------        ID
        public long ID
        {
            get { return (id); }
            set { id = value; }
        }
        // -------------------------------------------------        Name
        public string Name
        {
            get { return (name); }
        }
        // -------------------------------------------------        Origin
        public FuncAuthor Origin
        {
            get { return (origin); }
        }
        // -------------------------------------------------        PredefinedFunctionNumber
        public int PredefinedFunctionNumber
        {
            get { return (iPredefined); }
        }
        // -------------------------------------------------        YText
        public string YText
        {
            get { return (str_Y); }
        }
        // -------------------------------------------------        XText
        public string XText
        {
            get { return (str_X); }
        }
        // -------------------------------------------------        Arg_Left
        public double Arg_Left
        {
            get { return (fX_L); }
            set { fX_L = value; }
        }
        // -------------------------------------------------        Arg_Right
        public double Arg_Right
        {
            get { return (fX_R); }
            set { fX_R = value; }
        }
        // -------------------------------------------------        Val_Top
        public double Val_Top
        {
            get { return (fY_T); }
            set { fY_T = value; }
        }
        // -------------------------------------------------        Val_Bottom
        public double Val_Bottom
        {
            get { return (fY_B); }
            set { fY_B = value; }
        }
        // -------------------------------------------------        ValuesOnBorders
        public ValuesOnBorders ValuesOnBorders
        {
            get { return (new ValuesOnBorders (fX_L, fX_R, fY_T, fY_B)); }
        }
        // -------------------------------------------------        From
        public double From
        {
            get { return (fFrom); }
            set { fFrom = value; }
        }
        // -------------------------------------------------        To
        public double To
        {
            get { return (fTo); }
            set { fTo = value; }
        }
        // -------------------------------------------------        Step
        public double Step
        {
            get { return (fStep); }
            set { fStep = value; }
        }
        // -------------------------------------------------        ParamTrio
        public void ParamTrio (double from, double to, double step)
        {
            if (from != to && step != 0.0)
            {
                fFrom = from;
                fTo = to;
                fStep = step;
            }
        }
        // -------------------------------------------------        MSPlot
        public MSPlot MSPlot
        {
            get { return (msplot); }
            set { msplot = value; }
        }
        // -------------------------------------------------        Draw
        public void Draw (Graphics grfx, MSPlot plot, int iOnPlot)
        {
            MSPlotAuxi auxi = new MSPlotAuxi (iOnPlot, 0, 0, SegmentLocation .Partly_Inside);
            if (origin == FuncAuthor .Developer)
            {
                DrawPredefinedFunction (grfx, plot, auxi, iPredefined);
            }
            else
            {
                if (Polish_X .Count <= 0)
                {
                    if (Polish_Y .Count > 0)
                    {
                        plot .DrawYofX (grfx, auxi, Polish_Y);
                    }
                }
                else
                {
                    if (Polish_Y .Count > 0)
                    {
                        auxi .ParamTrio (fFrom, fTo, fStep);
                        plot .DrawParamFunc (grfx, auxi, Polish_X, Polish_Y);
                    }
                }
            }
        }
        // -------------------------------------------------        Draw
        public void DrawPredefinedFunction (Graphics grfx, MSPlot plot, MSPlotAuxi auxi, int iPredefFunc)
        {
            switch (iPredefFunc)
            {
                case 0:
                    plot .DrawYofX (grfx, auxi, Math .Sin);
                    break;
                case 1:
                    plot .DrawYofX (grfx, auxi, Math .Cos);
                    break;
                case 2:
                    plot .DrawYofX (grfx, auxi, Math .Tan);
                    break;
                case 3:
                    plot .DrawYofX (grfx, auxi, Trigon_0);
                    break;
                case 4:
                    plot .DrawYofX (grfx, auxi, Trigon_1);
                    break;
                case 5:
                    plot .DrawYofX (grfx, auxi, Sample_Func .RepeatDampedVibrations, new double [] { 3, 2, 11 });
                    break;
                case 6:
                    auxi .ParamTrio (0.0, 29.0, 0.02);
                    plot .DrawParamFunc (grfx, auxi, Sample_Func .ParamFunc_X_0, Sample_Func .ParamFunc_Y_0);
                    // x = t * cos (t * PI)    y = t * sin (1.5 * t * PI)
                    break;
                case 7:
                    auxi .ParamTrio (-10, 10, 0.02);
                    plot .DrawParamFunc (grfx, auxi, Sample_Func .ParamFunc_X_1, Sample_Func .ParamFunc_Y_1);
                    // x = t + 2*sin(2t)    y = t + 2*cos(5t)
                    break;
                case 8:
                    auxi .ParamTrio (0, 6.3, 0.02);
                    plot .DrawParamFunc (grfx, auxi, SinNx, new double [] { 5 }, CosNx, new double [] { 9 });
                    break;
                case 9:
                    plot .DrawYofX (grfx, auxi, Trigon_2);
                    // (1.7*sin(x) + 0.1*cos(x)) / tg(0.3*x)
                    break;
                case 10:
                    auxi .ParamTrio (-4, 4, 0.01);
                    plot .DrawParamFunc (grfx, auxi, Trigon_10X, Trigon_10Y);
                    // x = -0.2 + cos (r)    y = -0.2 * tg (r) + sin (r)
                    break;
            }
        }
        // -------------------------------------------------        Trigon_0
        // 2*sin(0.7*x - 0.9) +0.2*sin(20*x)
        public double Trigon_0 (double fx)
        {
            return (2.0 * Math .Sin (0.7 * fx - 0.9) + 0.2 * Math .Sin (20.0 * fx));
        }
        // -------------------------------------------------        Trigon_1
        // sin(x) + 0.6 * cos(4.5*x) -1.5
        public double Trigon_1 (double fx)
        {
            return (Math .Sin (fx) + 0.6 * Math .Cos (4.5 * fx) - 1.5);
        }
        // -------------------------------------------------        SinNx
        // sin(nx)
        public double SinNx (double fx, double [] coef)
        {
            return (Math .Sin (fx * coef [0]));
        }
        // -------------------------------------------------        CosNx
        // cos(nx)
        public double CosNx (double fx, double [] coef)
        {
            return (Math .Cos (fx * coef [0]));
        }
        // -------------------------------------------------        Trigon_2
        // (1.7*sin(x) + 0.1*cos(x)) / tg(0.3*x)
        public double Trigon_2 (double fx)
        {
            return ((1.7 * Math .Sin (fx) + 0.1 * Math .Cos (fx)) / Math .Tan (0.3 * fx));
        }
        // -------------------------------------------------        Trigon_10X
        // -0.2 + cos (r)
        public double Trigon_10X (double fr)
        {
            return (-0.2 + Math .Cos (fr));
        }
        // -------------------------------------------------        Trigon_10Y
        // -0.2 * tg (r) + sin (r)
        public double Trigon_10Y (double fr)
        {
            return (-0.2 * Math .Tan (fr) + Math .Sin (fr));
        }

        const string nameMain = "FuncView_";
        // -------------------------------------------------        IntoRegistry
        public void IntoRegistry (RegistryKey regkey, string strF)
        {
            try
            {
                regkey .SetValue (nameMain + strF, new string [] {  version .ToString (),       // 0
                                                                    id .ToString (),                // 1
                                                                    name,                           // 2
                                                                    ((int) origin) .ToString (),    // 3
                                                                    iPredefined .ToString (),       // 4
                                                                    fX_L .ToString (),          // 5
                                                                    fX_R .ToString (),          // 6
                                                                    fY_T .ToString (),          // 7
                                                                    fY_B .ToString (),          // 8
                                                                    fFrom .ToString (),     // 9
                                                                    fTo .ToString (),       // 10
                                                                    fStep .ToString (),     // 11
                                                                    str_Y,              // 12
                                                                    str_X,              // 13
                                                                 },
                                                    RegistryValueKind .MultiString);
                msplot .IntoRegistry (regkey, strF);
            }
            catch
            {
            }
            finally
            {
            }
        }
        // -------------------------------------------------        FromRegistry
        public static FunctionDesigned FromRegistry (Form form, RegistryKey regkey, string strF)
        {
            try
            {
                string [] strs = (string []) regkey .GetValue (nameMain + strF);
                if (form == null ||
                    strs == null || strs .Length < 14 ||
                    Convert .ToInt32 (strs [0]) != 605 ||   // version
                    string .IsNullOrEmpty (strs [2]))       // name
                {
                    return (null);
                }
                FunctionDesigned func = null;
                FuncAuthor orig = (FuncAuthor) Convert .ToInt32 (strs [3]);
                if (orig == FuncAuthor .Developer)
                {
                    func = new FunctionDesigned (strs [2], Convert .ToInt32 (strs [4]),
                                                 Convert .ToDouble (strs [5]), Convert .ToDouble (strs [6]),
                                                 Convert .ToDouble (strs [7]), Convert .ToDouble (strs [8]));
                    if (func == null)
                    {
                        return (null);
                    }
                }
                else
                {
                    if (string .IsNullOrEmpty (strs [12]))
                    {
                        return (null);
                    }
                    List<Elem> polishY = new List<Elem> ();
                    int iErr, iErrPlace;
                    bool bCorrectFunc = FunctionInterpreter .Analyse (strs [12], ref polishY, out iErr, out iErrPlace);
                    if (!bCorrectFunc)
                    {
                        return (null);
                    }
                    if (string .IsNullOrEmpty (strs [13]))
                    {
                        func = new FunctionDesigned (strs [2], strs [12], polishY,
                                                     Convert .ToDouble (strs [5]), Convert .ToDouble (strs [6]),
                                                     Convert .ToDouble (strs [7]), Convert .ToDouble (strs [8]));
                        if (func == null)
                        {
                            return (null);
                        }
                    }
                    else
                    {
                        List<Elem> polishX = new List<Elem> ();
                        bCorrectFunc = FunctionInterpreter .Analyse (strs [13], ref polishX, out iErr, out iErrPlace);
                        if (!bCorrectFunc)
                        {
                            return (null);
                        }
                        func = new FunctionDesigned (strs [2], strs [13], strs [12], polishX, polishY,
                                                     Convert .ToDouble (strs [5]), Convert .ToDouble (strs [6]),
                                                     Convert .ToDouble (strs [7]), Convert .ToDouble (strs [8]),
                                                     Convert .ToDouble (strs [9]), Convert .ToDouble (strs [10]), Convert .ToDouble (strs [11]));
                    }
                    if (func == null)
                    {
                        return (null);
                    }
                }
                MSPlot plot = MSPlot .FromRegistry (form, regkey, strF);
                if (plot == null)
                {
                    return (null);
                }
                func .MSPlot = plot;
                func .ID = Convert .ToInt64 (strs [1]);
                return (func);
            }
            catch
            {
                return (null);
            }
            finally
            {
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions