Use this method to create a simple 2D vector plot. This example simulates the electric field distribution from two opposite charges. The formula is not exact, I purposely change the denominator power to 0.8 (should be 1.5) in order to create a better arrow lines. You can create your own vector plot by replacing it with your vector function
Code Sample --------------
usingSystem;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace VectorChart
{
public partial class Form1 : Form
{
private DataCollection dc;
private ChartStyle cs;
public Form1()
{
InitializeComponent();
SetStyle(ControlStyles.ResizeRedraw, true);
this.BackColor = Color.White;
//set Form1 size:
this.Width = 350;
this.Height = 350;
dc = new DataCollection();
cs = new ChartStyle(this);
cs.XLimMin = -8f;
cs.XLimMax = 8f;
cs.YLimMin = -8f;
cs.YLimMax = 8f;
cs.IsXGrid = false;
cs.IsYGrid = false;
cs.XTick = 4f;
cs.YTick = 4f;
cs.Title = "Vector Chart";
}
private void AddData(Graphics g)
{
dc.DataSeriesList.Clear();
for (int i = 1; i < 16; i++)
{
for (int j = 1; j < 16; j++)
{
double x = i - 8;
double y = j - 8;
double a = 4;
float ex = (float)((x - a) / Math.Pow((x - a) * (x - a) + y * y, 0.8) -
(x + a) / Math.Pow((x + a) * (x + a) + y * y, 0.8));
float ey = (float)(y / Math.Pow((x - a) * (x - a) + y * y, 0.8) -
y / Math.Pow((x + a) * (x + a) + y * y, 0.8));
float em = (float)Math.Sqrt(ex * ex + ey * ey);
dc.AddVectorChart(g, cs, 0.7f*em, new PointF((float)x, (float)y), ex, ey);
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
cs.ChartArea = this.ClientRectangle;
SetPlotArea(g);
cs.AddChartStyle(g);
AddData(g);
g.Dispose();
}
}
}
Here is the code of the AddVectorChart method:
public void AddVectorChart(Graphics g, ChartStyle cs, float length,
PointF center, float xValue, float yValue)
{
float sn = (float)(yValue / Math.Sqrt(xValue * xValue + yValue * yValue));
float cn = (float)(xValue / Math.Sqrt(xValue * xValue + yValue * yValue));
PointF pt1 = new PointF(center.X, center.Y);
PointF pt2 = new PointF(center.X + length * cn, center.Y + length * sn);
Pen aPen = new Pen(Color.DarkBlue, 3f);
aPen.EndCap = LineCap.ArrowAnchor;
g.DrawLine(aPen, cs.Point2D(pt1), cs.Point2D(pt2));
aPen.Dispose();
}
|
No responses found. Be the first to respond and make money from revenue sharing program.
|