I’m trying to create a graph that draws some edges between specific vertices. The idea is that I am creating random vertices, and specifying a “transmit power” integer. The way it works is that if the distance between two nodes is less than or equal to the transmit power, a line will get drawn between them.
I’m using the MSDN chart control to do this. Having problems getting lines drawn though. I keep getting the following error message:
Error 2 Argument 2: cannot convert from 'System.Windows.Forms.DataVisualization.Charting.DataPoint' to 'System.Drawing.Point'
Been trying to crack this one so figured I’d log on to here and see if anyone else might know what to do here. I can’t find anything regarding how to convert this.
Here is some code example:
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);
}
}
}
Forgive me if I got this wrong, but DataPoint does not subclass Point. They cant be converted to System.Drawing.Point automatically. Why not create new Points?