我正在编写一个简单的WinForm应用程序,它应该根据输入边缘绘制图形。
在一个例子中,我输入5条边(标记为:0,1,2,3,4)图表为每条边绘制了8个额外的点,就像一条线。我怀疑WinForm正在尝试进行一些图表规范化,这会根据现有的点生成额外的点,但我找不到导致这种情况的选项。
绘制积分的代码:
foreach (int pt in points)
{
double dx = pointsToDraw[pt].X;
double dy = pointsToDraw[pt].Y;
chart1.Series[0].Points.AddXY(dx, dy);
chart1.Series[0].Points[pt].Label = pt.ToString();
}
在foreach语句中指向数组:
[0] 0 int
[1] 1 int
[2] 2 int
[3] 3 int
[4] 4 int
pointsToDraw列表,包含所有带附加坐标的点:
+ [0] {X = 260 Y = 385} System.Drawing.Point
+ [1] {X = 320 Y = 385} System.Drawing.Point
+ [2] {X = 240 Y = 385} System.Drawing.Point
+ [3] {X = 250 Y = 370} System.Drawing.Point
+ [4] {X = 400 Y = 370} System.Drawing.Point
编辑:
这很奇怪,因为chart1_Paint方法执行不周,这就是为什么有这么多点。这是方法:
private void chart1_Paint(object sender, PaintEventArgs e)
{
List<Point> pointsToDraw = new List<Point>();
List<int> pointsList = new List<int>();
int[] points;
if (Tree != null)
{
foreach(Tuple<int,int> tp in Tree)
{
pointsList.Add(tp.Item1);
pointsList.Add(tp.Item2);
}
IEnumerable<int> pointsDistinct = pointsList.Distinct();
pointsDistinct = pointsDistinct.OrderBy(s => s);
points = pointsDistinct.ToArray();
for (int i = 0; i < points.Count(); i++)
{
Point pt = new Point(begX, begY);
pointsToDraw.Add(pt);
}
int y = 1;
List<int> pointsAdded = new List<int>();
for (int i = 0; i < points.Count(); i++)
{
foreach (Tuple<int, int> tp in Tree)
{
if(points[i] == tp.Item1)
{
if(pointsAdded.Where(x => x == points[i]).Count() == 0)
{
pointsToDraw[tp.Item1] = new Point(begX, begY);
begX = begX + 10;
begY = begY - 15;
pointsAdded.Add(points[i]);
}
if (pointsAdded.Where(x => x == tp.Item2).Count() == 0)
{
pointsToDraw[tp.Item2] = new Point(pointsToDraw[tp.Item1].X+(10*y), pointsToDraw[tp.Item1].Y-15);
pointsAdded.Add(tp.Item2);
}
}
if (points[i] == tp.Item2)
{
if (pointsAdded.Where(x => x == points[i]).Count() == 0)
{
pointsToDraw[tp.Item2] = new Point(begX, begY);
begX = begX - 10;
begY = begY - 15;
pointsAdded.Add(points[i]);
}
if (pointsAdded.Where(x => x == tp.Item1).Count() == 0)
{
pointsToDraw[tp.Item2] = new Point(pointsToDraw[tp.Item1].X + (10 * y), pointsToDraw[tp.Item1].Y - 15);
pointsAdded.Add(tp.Item2);
}
}
y++;
}
}
foreach (int pt in points)
{
double dx = pointsToDraw[pt].X;//chart1.ChartAreas[0].AxisX.PixelPositionToValue(pointsToDraw[pt].X);
double dy = pointsToDraw[pt].Y;//chart1.ChartAreas[0].AxisY.PixelPositionToValue(pointsToDraw[pt].Y);
chart1.Series[0].Points.AddXY(dx, dy);
chart1.Series[0].Points[pt].Label = pt.ToString();
}
if (pointsToDraw.Count > 1)
using (Pen pen = new Pen(Color.Red, 2.5f))
foreach (Tuple<int, int> tp in Tree)
{
Point[] eds = new Point[] { pointsToDraw[tp.Item1], pointsToDraw[tp.Item2] };
e.Graphics.DrawLines(pen, eds);
}
}
}
我通过单击按钮并执行chart1.Invalidate()方法来运行它。