如何使用Drawing 2D类绘制多项式图? [英] How to use Drawing 2D class to draw polynomial graph?

查看:48
本文介绍了如何使用Drawing 2D类绘制多项式图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..我能知道如何使用Drawing 2D类绘制多项式图吗?数据是从数组中获取的.我只是不知道它的工作方式. class ="h2_lin">解决方案

我想去 Transform [ ^ ] Graphics对象.

我将使用面板派生的自定义类进行绘制.将绘图代码放在重写的OnPaint()方法中.确保绘画代码在任何给定的时间都能正常工作,因为Windows会在需要时立即调用绘画代码.

  public   class  MyClass:System.Windows.Forms.Panel
{
    私有 Pen _pen = Pens.Black;
    私有 PointF [] _points = ;


    受保护的 覆盖  void  OnPaint(PaintEventArgs e)
    {
        // 像其他面板一样绘制边框和背景.
        // 如果您不希望这样做,请将其删除.
         .OnPaint(e);

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

        如果(_points!= )
        {
            e.Graphics.DrawLines(_pen,_points);
        }
    }


    // 使点数组可从外部更改
    公共 PointF []点
    {
        获取 {返回(_ points); }
        设置
        {
            _points = // 告诉操作系统,更新可视化效果将会
            // 数据更改后真的很酷.
             .Invalidate();
        }
    } 

注释了对Transform的赋值,因为它(对我而言)是高度实验性的.您可以使用它来反转y轴(希望如此).因此,较大的y值应在屏幕上向上移动一个点.如果没有它,y将从面板顶部的0开始.它还将缩放您的值,因此两个轴的范围都在[-2; 2],而不是0分别改为number_of_pixels_wide或number_of_pixels_high.


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..

解决方案

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.

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.


这篇关于如何使用Drawing 2D类绘制多项式图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆