2012-10-01 33 views
1

我正在尝试创建一个绘制特定顶点之间的某些边的图。这个想法是我创建了随机顶点,并指定了“发射功率”整数。它的工作方式是,如果两个节点之间的距离小于或等于发射功率,则会在它们之间画出一条线。将数据点转换为点

我正在使用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

它看起来像你没有使用Drawing.Point,它可能是你的使用声明关闭了吗? –

回答

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); 
} 
1

数据点和点仅仅是不相同的类型。 但是,您可以执行的操作是将DataPoint字段复制到Point中。粗糙的例子:

Point point = new Point(); 
DataPoint dataPoint = new DataPoint() ; 
..... 
point.X = dataPoint.X ; 
point.Y = dataPoint.Y ; 
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();