попробуйте код:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
float W = panel1.Width, H = panel1.Height;
float halfW = W / 2, halfH = H / 2;
// оси координат
e.Graphics.DrawLine(Pens.Black, halfW, 0, halfW, H);
e.Graphics.DrawLine(Pens.Black, 0, halfH, W, halfH);
// координаты предыдущей точки
int ixPrev = -1, iyPrev = (int)halfH;
// тангенс на интервале x=[-Pi..Pi]
// проходим по всем точкам на форме, вычисляем x и y=tg(x)
for (int ix = 0; ix < W; ix++)
{
// переводим x в диапазон -1..1
float x = (ix - halfW) / halfW;
// переводим x в -pi..pi
x *= (float)Math.PI;
// получаем tg(x)
float y = (float)Math.Tan(x);
// переводим y из -1..1 в пикселы на форме
int iy = (int)(halfH - y * halfH);
// вуаля
e.Graphics.DrawLine(Pens.Red, ixPrev, iyPrev, ix, iy);
ixPrev = ix;
iyPrev = iy;
}
}
}
}
пример:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1 {
public partial class Form1 : Form {
private const float diapazon_min = -100;
private const float diapazon_max = 100;
private const float inc_step = 1;
PointF[] arr;
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
arr = new PointF[(int)(((diapazon_max - diapazon_min) / inc_step) + 1)];
int c = 0;
for (float x = diapazon_min; x < diapazon_max; x++) {
arr[c] = new PointF(x, Func(x));
c++;
}
}
private float Func(float x) {
return x*x*x;
}
private void pic_Paint(object sender, PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
using (Pen pen = new Pen(Color.Blue)) {
Matrix mat = new Matrix(1, 0, 0, -1, 0, 0); // отражение
mat.Translate(pic.Width / 2, pic.Height / 2, MatrixOrder.Append);
//mat.Scale(2, 2);
e.Graphics.MultiplyTransform(mat);
e.Graphics.DrawLine(pen, diapazon_min, 0, diapazon_max, 0);
e.Graphics.DrawLine(pen, 0, diapazon_min, 0, diapazon_max);
if (arr != null) {
e.Graphics.DrawLines(pen, arr);
}
}
}
}
}