Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..Can I know how to use Drawing 2D class to draw a polynomial graph?the data get from the array..I just don't have idea how it is going to work..Thanks..
Posted

1 solution

I'd go for System.Drawing.Graphics.DrawLines()[^] to draw multiple lines sequentially. You would take the individual corner points' co-ordinates from your array.

Depending on what exactly that array contains, you may have to Transform[^] the Graphics object.

I would use a panel-derived custom class to do the drawing. Place the drawing code within an overridden OnPaint() method. Make sure that at any given time, the painting code will work because Windows will call the drawing code whenever it feels so.
C#
public class MyClass : System.Windows.Forms.Panel
{
    private Pen _pen = Pens.Black;
    private PointF[] _points = null;


    protected override void OnPaint(PaintEventArgs e)
    {
        // Paint borders and background like any other panel.
        //   Remove if you don't want that done.
        base.OnPaint(e);

        //e.Graphics.Transform = new System.Drawing.Drawing2D.Matrix(
        //    this.ClientRectangle,
        //    new Point[]{
        //        new Point(-2,2),
        //        new Point(2,2),
        //        new Point(-2,-2)
        //    }
        //);

        if( _points != null)
        {
            e.Graphics.DrawLines(_pen, _points);
        }
    }


    // Make point array changable from outside
    public PointF[] Points
    {
        get{ return(_points); }
        set
        {
            _points = value;

            // Tell OS that updating visualization would be
            //   really cool after data change.
            this.Invalidate();
        }
    }
The assignment to Transform is commented out because it is (for me) highly experimental. You can use it to invert the y-axis (I hope). So larger y values should move a point up on your screen. Without it, y will start at 0 at the top of your panel. It will also scale your values so both axis range from [-2 ; 2] instead of 0 to number_of_pixels_wide or number_of_pixels_high respectively.
 
Share this answer
 
v2
Comments
Esmond90 19-Jul-12 22:14pm    
Thanks for reply.Would you provide me some code for drawing graph line as for reference?Thanks for the help.=)
lukeer 20-Jul-12 3:04am    
I updated my answer.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900