0

I'm currently working on a 2 part homework assignment - first part being writing a program that generates values based on functions and then displays them in a table. The second part being taking these generated values and drawing them in a seperate 'Form2' that displays it on an X/Y axis.

Now, the first part is complete but I really cant figure out how to get the second part done and was hoping someone could point me in the right direction.

The form1 code:

namespace MD2._5
{
    public partial class Form1 : Form
    {
        int step;
        int xEnd, xBegin;
        double a, b, c;


        public delegate double DY(double a, double b, double c, int x); //declares delegate

        public static double Y0(double a, double b, double c, int x) //Functions that do the calculations
        {
            return Math.Round(a * (x * x) + (b * x + c), 2);
        }

        public static double Y1(double a, double b, double c, int x)
        {
            return Math.Round((a / (x * x)) + (b / x + c), 2);
        }

        public static double Y2(double a, double b, double c, int x)
        {
            return Math.Round(((a * x) + b) / ((a * x) + c), 2);
        }

        public DY[] Y = new DY[3] { Y0, Y1, Y2 }; //New array of the above functions


        private void radioButton1_Click(object sender, EventArgs e)
        {
            int buttonPressed = Convert.ToInt32(((RadioButton)sender).Tag);

            DoTable(Y[buttonPressed]);
        }

        private void DoTable(DY y) //Prints the table
        {
            richTextBox1.Clear();
            richTextBox1.AppendText("     X                      Y \n");

            c = Convert.ToInt32(textBoxC.Text);
            b = Convert.ToInt32(textBoxB.Text);
            a = Convert.ToInt32(textBoxA.Text);
            step = Convert.ToInt32(stepText.Text);
            xEnd = Convert.ToInt32(x_end.Text);
            xBegin = Convert.ToInt32(x_begin.Text);

            for (int x = xBegin; x <= xEnd; x += step)
                {
                    richTextBox1.AppendText("\n    " + x.ToString() + "\t           " + y(a, b, c, x).ToString());
                }
        }



        Form2 f2; //creates an instance of form2
        private void button2_Click(object sender, EventArgs e) //Graphics button that generates a new form
        {
            f2 = new Form2();
            f2.ShowDialog();
        }
    }
}

Now currently it is displaying this:

enter image description here

And when you click on the 'Graphics' button it comes up with: enter image description here

But it should be something like this (the dots representing the values from the previous screen):

enter image description here

Sorry its a bit long, but thought its necessary to really encapsulate the whole thing.

Now im struggling with understanding how I would translate the whole thing to a drawn picture.

Can anyone point me in the right direction?

Thank you

edit:

Actually managed to get a little further. Created form 2 and included this code:

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();

        }

        public delegate double fu(double x);

        public void ShowFu(fu F, int m)
        {
            int xc = pictureBox1.Width / 2;   
            int yc = pictureBox1.Height / 2;

            int xe, ye;     
            double x, y;   
            double step = 0.05;    

            Graphics G = pictureBox1.CreateGraphics();
            G.Clear(System.Drawing.Color.White);

            Pen myPen = new Pen(Color.Silver);
            G.DrawLine(myPen, 10, yc, 2 * xc - 10, yc);  
            G.DrawLine(myPen, xc, 10, xc, 2 * yc - 10);

            myPen = new Pen(Color.Black);

            x = -Math.PI;

            while (x < Math.PI)
            {
                try   
                {
                    y = F(x);   //This is the new delegate - is there a way to use the delegate from form 1?
                    xe = (int)(xc + m * x);
                    ye = (int)(yc - m * y);
                    G.DrawEllipse(myPen, xe, ye, 1, 1);
                }
                catch { }
                x += step;

            }
        }


        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ShowFu(Math.Sin, 30);
        }
    }

Now this generates a picture of: enter image description here

Obviously this isnt data from Form 1. So the question now is, how do I get the data generated on form 1 to be implemented on this form2?

B. Baxter
  • 133
  • 1
  • 10
  • this might help: https://learn.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/shapes-and-basic-drawing-in-wpf-overview – fYre Nov 24 '19 at 18:55
  • https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form – Joel Wiklund Nov 24 '19 at 20:28

0 Answers0