I have been trying to get a simple linear equation to draw. I can draw the axis fine but the equation will not draw. I think one would need a save/refresh function(s), but I don't know how to go about doing this:

pw = pictureBox1.Size.Width;
            ph = pictureBox1.Size.Height;
            w = pictureBox1.Size.Width / 2;
            h = pictureBox1.Size.Height / 2;
            Graphics objGraphics = this.pictureBox1.CreateGraphics();
            Pen pen = new Pen(Color.Black);
            b = 2;
            m = 2;
            for (x = 0; x < 426; x++)
            {
                y = m * x + b;
                x = (y - b) / m;
                objGraphics.DrawLine(pen, x, y, x, y);
                System.Drawing.Drawing2D.GraphicsState graph = objGraphics.Save();
                objGraphics.Restore(graph);
            }

           

            objGraphics.DrawLine(pen, 0, h, pw, h);
            objGraphics.DrawLine(pen, w, 0, w, ph);

Thanks for any help. I should mention that I did declare all variables (x, y, m, and b). I'm just starting out with VC#, so that might explain the sloppy or simple code.

That is because you are plotting a point right on top of the original point.

I changed this line...

objGraphic.DrawLine(pen, x, y, -x, -y);

Here is the code I used...

int picBoxWidth = pictureBox1.Size.Width;
            int picBoxHeight = pictureBox1.Size.Height;
            int halfWidth = pictureBox1.Size.Width / 2;
            int halfHeight = pictureBox1.Size.Height / 2;

            Graphics objGraphic = this.pictureBox1.CreateGraphics();

            Pen pen = new Pen(Color.Black);

            int b = 2;
            int m = 2;
            for (int x = 0; x < 426; x++)
            {
                int y = m * x + b;
                x = (y - b) / m;
                objGraphic.DrawLine(pen, x, y, -x, -y);
                System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
                objGraphic.Restore(graph);
            }

            objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
            objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);

Hope that helps :)

That is because you are plotting a point right on top of the original point.

I changed this line...

objGraphic.DrawLine(pen, x, y, -x, -y);

Here is the code I used...

int picBoxWidth = pictureBox1.Size.Width;
            int picBoxHeight = pictureBox1.Size.Height;
            int halfWidth = pictureBox1.Size.Width / 2;
            int halfHeight = pictureBox1.Size.Height / 2;

            Graphics objGraphic = this.pictureBox1.CreateGraphics();

            Pen pen = new Pen(Color.Black);

            int b = 2;
            int m = 2;
            for (int x = 0; x < 426; x++)
            {
                int y = m * x + b;
                x = (y - b) / m;
                objGraphic.DrawLine(pen, x, y, -x, -y);
                System.Drawing.Drawing2D.GraphicsState graph = objGraphic.Save();
                objGraphic.Restore(graph);
            }

            objGraphic.DrawLine(pen, 0, halfHeight, picBoxWidth, halfHeight);
            objGraphic.DrawLine(pen, halfWidth, 0, halfWidth, picBoxHeight);

Hope that helps :)

Thanks that did help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.