我正在尝试创建一个在特定顶点之间绘制一些边的图形。我的想法是创建随机顶点,并指定“发射功率”整数。它的工作方式是,如果两个节点之间的距离小于或等于发射功率,则会在它们之间划出一条线。
我正在使用MSDN图表控件来执行此操作。虽然有线条绘制的问题。我不断收到以下错误消息:
Error 2 Argument 2: cannot convert from 'System.Windows.Forms.DataVisualization.Charting.DataPoint' to 'System.Drawing.Point'
试图破解这个,所以我想登录到这里,看看是否有其他人可能知道该怎么做。我找不到有关如何转换它的任何内容。
以下是一些代码示例:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void createNodes(int x, int y, int Nodes, int dgNodes)
{
Random rdn = new Random();
for (int i = 0; i < (Nodes - dgNodes); i++)
{
chtGraph.Series["NoDG"].Points.AddXY
(rdn.Next(x), rdn.Next(y));
}
for (int i = 0; i <= dgNodes - 1; i++)
{
chtGraph.Series["DG"].Points.AddXY
(rdn.Next(x), rdn.Next(y));
}
}
public void buildGraph(int x, int y, int Nodes, int dgNodes)
{
//set the min/max axis on the chart
chtGraph.ChartAreas["ChartArea1"].AxisX.Maximum = x;
chtGraph.ChartAreas["ChartArea1"].AxisX.Minimum = 0;
chtGraph.ChartAreas["ChartArea1"].AxisY.Maximum = y;
chtGraph.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
chtGraph.ChartAreas["ChartArea1"].AxisX.Interval = x / 10;
chtGraph.ChartAreas["ChartArea1"].AxisY.Interval = y / 10;
chtGraph.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;
chtGraph.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;
//build all the nodes
createNodes(x, y, Nodes, dgNodes);
}
public void drawEdges(int intNumNodes, int intTransPower)
{
ChartGraphics gr = new ChartGraphics();
Pen pen = new Pen(Color.Black, 1);
//System.Drawing.Point[] pts = new Point[4];
//pts[0] = new Point(20, 20);
//pts[1] = new Point(20, 140);
//pts[2] = new Point(60, 60);
//pts[3] = new Point(180, 80);
//gr.DrawLine(pen, pts[1], pts[2]);
DataPoint[] pts = new DataPoint[intNumNodes];
int i = 0;
//Gather all the data points into an array
foreach (DataPoint p in chtGraph.Series[0].Points)
{
pts[i] = p;
i++;
}
i = 0;
//loop through all the data points
foreach (DataPoint p in pts)
{
//examine all the other data points for each data point visited
for (int j = 0; j < pts.Length; j++)
{
//convert the y values
int yval = Convert.ToInt32(p.YValues[0]);
int yValNeighbors = Convert.ToInt32(pts[j].YValues[0]);
//if the distance from the parent node (p) to the neighbor node is less than the transmit power, then draw a line
if (Math.Sqrt(Math.Pow((p.XValue - pts[j].XValue), 2) + Math.Pow((yval - yValNeighbors), 2)) <= intTransPower)
{
gr.Graphics.DrawLine(pen, p, pts[j);
}
}
}
}
private void btnExecute_Click(object sender, EventArgs e)
{
if (txtDG.Text == "" || txtNodes.Text == "" || txtStorage.Text == "" || txtTransPower.Text == ""
|| txtXAxis.Text == "" || txtXAxis.Text == "")
{
lblError.Text = "Please enter in all inputs!";
lblError.Visible = true;
return;
}
//create variables for use through program
int intTransPower = Convert.ToInt32(txtTransPower.Text);
int intXAxis = Convert.ToInt32(txtXAxis.Text);
int intYAxis = Convert.ToInt32(txtYAxis.Text);
int intNum_DG = Convert.ToInt32(txtDG.Text);
int intNumNodes = Convert.ToInt32(txtNodes.Text);
int intStorage = Convert.ToInt32(txtStorage.Text);
lblError.Visible = false;
lblError.Text = "";
if (txtDG.Text == "" || txtNodes.Text == "" || txtStorage.Text == "" || txtTransPower.Text == ""
|| txtXAxis.Text == "" || txtXAxis.Text == "")
{
lblError.Text = "Please enter in all inputs!";
lblError.Visible = true;}
chtGraph.Series["NoDG"].Points.Clear();
chtGraph.Series["DG"].Points.Clear();
buildGraph(intXAxis, intYAxis, intNumNodes, intNum_DG);
drawEdges(intNumNodes, intTransPower);
}
}
}
答案 0 :(得分:1)
DataPoint和Point根本不是同一类型。 但是,您可以将DataPoint字段复制到Point中。粗略的例子:
Point point = new Point ();
DataPoint dataPoint = new DataPoint() ;
.....
point.X = dataPoint.X ;
point.Y = dataPoint.Y ;
答案 1 :(得分:0)
如果我弄错了,请原谅我,但DataPoint不是Point的子类。它们无法自动转换为System.Drawing.Point。为什么不创建新积分?
//if the distance from the parent node (p) to the neighbor node is less than the transmit power, then draw
if (Math.Sqrt(Math.Pow((p.XValue - pts[j].XValue), 2) + Math.Pow((yval - yValNeighbors), 2)) <= intTransPower)
{
var point = new System.Drawing.Point((int)p.XValue, (int)p.YValue);
gr.Graphics.DrawLine(pen, point, pts[j);
}
答案 2 :(得分:0)
您可以制作并引用此扩展程序,
namespace DrawingExtensions
{
using System.Windows.Forms.DataVisualization.Charting;
using System.Drawing;
public static class Forms
{
public PointF ToPointF(this DataPoint source)
{
return new PointF(
Convert.ToSingle(source.XValue),
Convert.ToSingle(source.YValue));
}
public Point ToPoint(this DataPoint source)
{
return new Point(
Convert.ToInt32(source.XValue),
Convert.ToInt32(source.YValue));
}
}
}
你明显会使用这个,
var point = dataPoint.ToPoint();
var pointF = dataPoint.ToPointF();
或者,对于整个阵列,
var points = dataPoints.Select(p => p.ToPoint()).ToArray();
var pointFs = dataPoints.Select(p => p.ToPointF()).ToArray();