问题
I've added a horizontal stripline to a sinusoidal type graph which doesn't have many datapoints. Is there a way to find the x-coordinate intersections?
回答1:
You can either tackle it analytically, i.e. if your data are derived from a formula you can use math to solve the intersection set.
Or you can us an approximation with a little help from GDI+
.
As you have found using the DataPoints
directly in a thinly populated set of points will not work well.
But there is an interesting and simple workaround that can create an enlarged set of points for you.
For this you need to use a flattened GraphicsPath
:
Let's assume your values are in a List<PointF> points
:
List<PointF> points = new List<PointF>();
for (int i = 0; i < 10; i++) points.Add(new PointF(i, (float)Math.Sin(i)));
Now you first create a GraphicsPath
from it:
using System.Drawing.Drawing2D;
..
..
GraphicsPath gp = new GraphicsPath();
gp.AddCurve(points.ToArray());
Then you flatten it:
Matrix m = new Matrix(); // identity
gp.Flatten(m, yourFlatness);
This changes the GraphicsPath
from a series of curves, (which are identical to the Chart
's spline curves, btw,) to a series of line segments. The 'flatness' determines how much the lines may deviate from the curve. so, the smaller the flatness you use (default is 0.25f), the more segments you get.
We have started with 10 DataPoints
shown below in red. After flattening with 0.1f
, 0.01f
and 0.001f
we get 19
, 55
and 152
points/line segments respectively..:
You can acces them in the gp.PathPoints
array and will get much closer to the real intersections. Add a little interpolation and you should be close enough for jazz..
来源:https://stackoverflow.com/questions/37786327/charts-stripline-and-curve-intersection-points