Using a chart control on a windows form in C#, with visual studio 2010 IDE.
I'm creating a chart that plots random points. Once points are generated, I run a loop for each point in the chart, looking at all the other point coordinates. Inside that loop, I calculate the distance from the parent point to it's neighbor. If the distance is <= some distance that I specify, I want a line to be drawn showing a connection between the two. Problem I'm having though is actually drawing that line. Further more, I then need to find a way to walk the shortest path in that chart.
So this is really two questions:
1. How do I draw the lines on the chart? (current dilemma)
2. How do I walk the graph to find the shortest path?
Here is a snipper of some code I'm using to accomplish this, along with the current error:
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["Series1"].Points.AddXY
(rdn.Next(x), rdn.Next(y));
}
for (int i = 0; i <= dgNodes - 1; i++)
{
chtGraph.Series["Series2"].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)
{
Pen pen = new Pen(Color.Black, 1);
Graphics g = chtGraph.CreateGraphics();
Point[] pts = new Point[intNumNodes];
int i = 0;
//Gather all the data generator data points into a point array
foreach (DataPoint p in chtGraph.Series[0].Points)
{
Point point = new Point((int)p.XValue, (int)p.YValues[0]);
pts[i] = point;
i++;
}
//Gather all the non data generator into the same point array
foreach (DataPoint p in chtGraph.Series[1].Points)
{
Point point = new Point((int)p.XValue, (int)p.YValues[0]);
pts[i] = point;
i++;
}
//loop through all the data points
foreach (Point p in pts)
{
//examine all the other data points for each data point visited
for (int j = 0; j < pts.Length; j++)
{
//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.X - pts[j].X), 2) + Math.Pow((p.Y - pts[j].Y), 2)) <= intTransPower)
{
//gr.DrawLine(pen, p, pts[j]);
//gr.Graphics.DrawLine(pen, p.X, p.Y, pts[j].X, pts[j].Y);
}
}
}
}
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["Series1"].Points.Clear();
chtGraph.Series["Series2"].Points.Clear();
buildGraph(intXAxis, intYAxis, intNumNodes, intNum_DG);
drawEdges(intNumNodes, intTransPower);
}
}
Error: Error 1 The type 'System.Windows.Forms.DataVisualization.Charting.ChartGraphics' has no constructors defined
Used event described in comment in the question.
Related
I'm having an issue where my C# stack will accept a pushed value but then overwrite the previously existing elements in the stack with the new value as well.
Here's a chunk of the constructor for reference:
public class MazNav //handles processing and navigation
{
private Maze m;
private static Stack<int[]> path;
private int[] currCell;
private int visCell;
private Random rng;
//constructor
public MazNav(Maze mz)
{
m = mz; //assigns this object to a maze
path = new Stack<int[]>(); //initialize the stack
currCell = m.getStart(); //starts the pathfinding at start cell
visCell = 1; //initializes the visited cells count
path.Push(currCell); //adds this cell to the stack
rng = new Random(); //initializes the randomizer
The problem occurs in the if block towards the end of this method (sorry it's still ugly; I'm in the middle of debugging and will clean this up once I have something that works :) ):
public void buildMaze()
{
//variables to represent the current cell
int[] currPos; //coordinates
int nextDir = 0; //direction towards next cell
//variables to represent the next cell
int[] nextPos; //coordinates
int backDir = 0; //holds direction towards previous cell
bool deadEnd = false; //flags true when a backtrack is required
bool outOfBounds = false; //flags true when a move would leave the array
while (visCell < m.getTotCells()) //while the number of visited cells is less than the total cells
{
if (path.Count > 0) // if there is something in the stack
{
currPos = path.Peek(); //check the current coordinates
nextPos = currPos; //movement will happen one cell at a time; setting next cell coordinates the same as current allows easy adjustment
nextDir = findNextUnv(currPos); //find the direction of the next cell to check
deadEnd = false;
outOfBounds = false;
switch (nextDir)
{
case 0: //North
if (nextPos[0] - 1 >= 0)
{
nextPos[0]--;
backDir = 2;
}
else
{
outOfBounds = true;
}
break;
case 1: //East
if (nextPos[1] + 1 < m.getCols())
{
nextPos[1]++;
backDir = 3;
}
else
{
outOfBounds = true;
}
break;
case 2: //South
if(nextPos[0] + 1 < m.getRows())
{
nextPos[0]++;
backDir = 0;
}
else
{
outOfBounds = true;
}
break;
case 3: //West
if (nextPos[1] - 1 >= 0)
{
nextPos[1]--;
backDir = 1;
}
else
{
outOfBounds = true;
}
break;
case 99: //dead end
try
{
deadEnd = true;
path.Pop();
currPos = path.Peek();
int diff;
if (currPos[0] == nextPos[0])
{
diff = currPos[1] - nextPos[1];
if (diff == -1)
{
backDir = 3;
}
else if (diff == 1)
{
backDir = 1;
}
}
else if (currPos[1] == nextPos[1])
{
diff = currPos[0] - nextPos[0];
if (diff == -1)
{
backDir = 2;
}
else if (diff == 1)
{
backDir = 0;
}
}
m.getCell(nextPos[0], nextPos[1]).setBck(backDir, true);
}
catch (Exception) { }
break;
}
if (!deadEnd && !outOfBounds)
{
m.getCell(currPos[0], currPos[1]).setWal(nextDir, false);
m.getCell(nextPos[0], nextPos[1]).setWal(backDir, false);
path.Push(nextPos);
visCell++;
}
}
}
}e
The push call is only executed once but when I watch it on the debugger, after that line runs, the count increases by 1 but every element in the stack is now identical. Has anyone come across this behavior before? Where am I going wrong?
There are a lot of bugs in your code because of the way you are treating nextPos and curPos.
In particular, this line:
nextPos = currPos;
Here you are assigning nextPos to be the same array as curPos, so if you modify one, the other one will be modified along with it. And if you push one on to the stack and then modify either one, the array on the stack will be modified along with it.
The solution I would suggest is to use an immutable data type for your positions, rather than an array (which isn't particularly well suited for coordinates anyway):
internal struct Point
{
internal int X { get; private set; }
internal int Y { get; private set; }
internal Point(int x, int y)
{
X = x;
Y = y;
}
internal Point NewX(int deltaX)
{
return new Point(X + deltaX, Y);
}
internal Point NewY(int deltaY)
{
return new Point(X, Y + deltaY);
}
}
This way, when you need to transition to a new point, you can create a new one instead of modifying an existing one:
if (nextPos.X - 1 >= 0)
{
nextPos = nextPos.NewX(-1);
backDir = 2;
}
This should help you to keep a handle on your values and keep them from overwriting each other every which way.
I have a little "project" which involves drawing Symmetric Binary B-Trees, like this one:
But I cant figure out a way to correctly calculate the position (x,y) of each node. The way Im doing it right now, as the tree's height grows some nodes tend to get overlapped by others.
Can anyone give me some light as to how can I calculate the position of a node?
Im using C# and this is the class I have right now that represents a node:
class SBBTreeNode<T> where T : IComparable {
public SBBTreeNode(T item) {
Data = item;
Left = null;
Right = null;
}
public T Data { get; private set; }
public SBBTreeNode<T> Left;
public SBBTreeNode<T> Right;
public bool IsHorizontal { get; set; } //Is this node horizontal?
public bool IsLeaf() {
return Left == null && Right == null;
}
}
Here is a drawing routine:
void drawTree(Graphics G)
{
if (flatTree.Count <= 0) return;
if (maxItemsPerRow <= 0) return;
if (maxLevels <= 0) return;
int width = (int)G.VisibleClipBounds.Width / (maxItemsPerRow + 2);
int height = (int)G.VisibleClipBounds.Height / (maxLevels + 2);
int side = width / 4;
int textOffsetX = 3;
int textOffsetY = 5;
int graphOffsetY = 50;
Size squaresize = new Size(side * 2, side * 2);
foreach (SBBTreeNode<string> node in flatTree)
{
Point P0 = new Point(node.Col * width, node.Row * height + graphOffsetY);
Point textPt = new Point(node.Col * width + textOffsetX,
node.Row * height + textOffsetY + graphOffsetY);
Point midPt = new Point(node.Col * width + side,
node.Row * height + side + graphOffsetY);
if (node.Left != null)
G.DrawLine(Pens.Black, midPt,
new Point(node.Left.Col * width + side,
node.Left.Row * height + side + graphOffsetY));
if (node.Right != null)
G.DrawLine(Pens.Black, midPt,
new Point(node.Right.Col * width + side,
node.Right.Row * height + side + graphOffsetY));
G.FillEllipse(Brushes.Beige, new Rectangle(P0, squaresize));
G.DrawString(node.Data, Font, Brushes.Black, textPt);
G.DrawEllipse(Pens.Black, new Rectangle(P0, squaresize));
}
}
and its result:
Usage:
flatTree = FlatTree();
setRows();
setCols();
panel_tree.Invalidate();
Now for the various pieces that lead up to this:
The drawTree routine is obviously triggered from a Panel's Paint event.
I uses a few class level variables:
This is the Tree I build in my tests; please note that to make things a little simpler I have dumped your generic type T for string:
Dictionary<string, SBBTreeNode<string> > tree
= new Dictionary<string, SBBTreeNode<string>>();
This is a flat traversal copy of the tree, that is, its elements are ordered by level and from left to right:
List<SBBTreeNode<string>> flatTree = new List<SBBTreeNode<string>>() ;
Here are the dimesions of the tree:
int maxItemsPerRow = 0;
int maxLevels = 0;
This is how the flat tree is created, using a Queue:
List<SBBTreeNode<string>> FlatTree()
{
List<SBBTreeNode<string>> flatTree = new List<SBBTreeNode<string>>();
Queue<SBBTreeNode<string>> queue = new Queue<SBBTreeNode<string>>();
queue.Enqueue((SBBTreeNode<string>)(tree[tree.Keys.First()]));
flatNode(queue, flatTree);
return flatTree;
}
This is the recursive call to get the nodes in order:
void flatNode(Queue<SBBTreeNode<string>> queue, List<SBBTreeNode<string>>flatTree)
{
if (queue.Count == 0) return;
SBBTreeNode<string> node = queue.Dequeue();
if (!node.IsHorizontal) flatTree.Add(node);
if (node.Left != null) { queue.Enqueue(node.Left); }
if (node.Left != null && node.Left.Right != null && node.Left.Right.IsHorizontal)
queue.Enqueue(node.Left.Right);
if (node.Right != null)
{
if (node.Right.IsHorizontal) flatTree.Add(node.Right);
else queue.Enqueue(node.Right);
}
flatNode(queue, flatTree);
}
Finally we can set the (virtual) coordinates of each node:
void setCols()
{
List<SBBTreeNode<string>> FT = flatTree;
int levelMax = FT.Last().Row;
int LMaxCount = FT.Count(n => n.Row == levelMax);
int LMaxCount1 = FT.Count(n => n.Row == levelMax-1);
if (LMaxCount1 > LMaxCount)
{ LMaxCount = LMaxCount1; levelMax = levelMax - 1; }
int c = 1;
foreach (SBBTreeNode<string> node in FT) if (node.Row == levelMax)
{
node.Col = ++c;
if (node.Left != null) node.Left.Col = c - 1;
if (node.Right != null) node.Right.Col = c + 1;
}
List<SBBTreeNode<string>> Exceptions = new List<SBBTreeNode<string>>();
for (int n = FT.Count- 1; n >= 0; n--)
{
SBBTreeNode<string> node = FT[n];
if (node.Row < levelMax)
{
if (node.IsHorizontal) node.Col = node.Left.Col + 1;
else if ((node.Left == null) | (node.Right == null)) {Exceptions.Add(node);}
else node.Col = (node.Left.Col + node.Right.Col) / 2;
}
}
// partially filled nodes will need extra attention
foreach (SBBTreeNode<string> node in Exceptions)
textBox1.Text += "\r\n >>>" + node.Data;
maxLevels = levelMax;
maxItemsPerRow = LMaxCount;
}
Note that I have not coded the special case of partially filled nodes but only add them to a list of exceptions; you have to decide what to do with those, ie if they can happen and where they ought to be painted.
OK, this is almost it. We have to do two more things:
I have taken the liberty to add two coordinate fields to your node class:
public int Row { get; set; }
public int Col { get; set; }
And I have writen my AddNode routine in such a way that the level of each node is set right there.
You will certainly want/need to do it differently. A simple SetRows routine is a snap, especially when you use the flatTree for transversal:
void setRows()
{
foreach (SBBTreeNode<string> node in flatTree)
{
if (node.Left != null) node.Left.Row = node.Row + 1;
if (node.Right != null) node.Right.Row =
node.Row + 1 - (node.Right.IsHorizontal ? 1:0);
}
}
Explanation:
Besides the flatTree, I use for drawing, the core of the solution is the SetCols routine.
In a balanced B-Tree the maximum width is reached either at the last or the second-to-last row.
Here I count the number of nodes in that row. This gives me the width of the whole tree, maxItemsPerRow. The routine also sets the height as maxLevels
Now I first set the Col values in that widest row, from left to right (and if present to dangling children in the last row.)
Then I move up level by level and calculate each Col value as the middle between the Left and Right Child, always watching out for horizontal nodes.
Note that I assume that all horizontal nodes are right children! If that is not true you will have to make various adaptions in both the FlatTree and the setCol routines..
I would start by placing the root node at (0,0) (really doesn't matter where you start). Call this point (parent_X, parent_Y). Then pick a starting width (say 2^(number of levels in your tree), if you know how many levels your tree has, otherwise, just pick any width).
The left child goes at position (parent_X-width/2, parent_Y-1) and the right child goes at position (parent_X+width/2, parent_Y-1). Then change the width to width = width/2. If a child happens to be horizontal, you can just forget the parent_Y-1 part and keep the parent_Y. Then just repeat on each of the children of the head node. Each time you move down a level, replace width with width/2 - epsilon.
Hope this helps.
A C++ method returns the correct value when I use a conditional breakpoint, but an incorrect value without a breakpoint.
C# method which calls C++:
bool SetProperty(Element element, Node referencePoint, List<Materializer> materializers, List<ulong> properties)
{
// Loop over STLs
for (int i = 0; i < materializers.Count; i++)
{
Materializer materializer = materializers[i];
if (materializer.IsPointInside(referencePoint.X, referencePoint.Y, referencePoint.Z, pentalTreeDatasets[i].top))
{
element.PropertyId = properties[i];
return true;
};
}
return false;
}
C++ methods in the header file:
int CountIntersects(double x, double y, double z, PentalTreeNode ^root)
{
Math3d::M3d rayPoints[2], intersectionPoint;
rayPoints[0].set(x,y,z);
rayPoints[1].set(x,y,1.0e6);
if(!root)
return 0;
else
{
int special = CountIntersects(x,y,z,root->special);
if (x <= root->xMax && x >= root->xMin && y <= root->yMax && y >= root->yMin)
{
if( _stlMesh->IsRayIntersectsPoly(root->index, rayPoints, intersectionPoint))
{
return (1 + special);
}
else
return special;
}
else
{
if (y>root->yMax)
{
return (CountIntersects(x,y,z,root->top)+special);
}
else if(y<root->yMin)
{
return (CountIntersects(x,y,z,root->bottom)+special);
}
else if(x<root->xMin)
{
return (CountIntersects(x,y,z,root->left)+special);
}
else if(x>root->xMax)
{
return (CountIntersects(x,y,z,root->right)+special);
}
else
return special;
}
}
}
bool IsPointInside(double x, double y, double z, PentalTreeNode ^root)
{
int intersectionCount = 0;
Math3d::M3d rayPoints[2], intersectionPoint;
rayPoints[0].set(x,y,z);
rayPoints[1].set(x,y,1.0e6);
if(_box->IsContainingPoint(x,y,z))
{
intersectionCount=CountIntersects(x,y,z,root);
return (intersectionCount%2!=0);
}
}
C++ methods in other header files:
bool IsRayIntersectsPoly(int nPolygonIndex, Math3d::M3d RayPoints[2], CVector3D& IntersectionPoint)
{
CMeshPolygonBase& Poly = m_PolygonArray[nPolygonIndex];
CArrayResultI Result;
int* pPolygonPoints = GetPolygonPoints(Poly, Result);
Math3d::MPlane TrianglePlane;
double Atmp[3], A;
CVector3D* pPoints[3];
pPoints[0] = &m_PointArray[*pPolygonPoints].m_Position;
for(int i = 1; i < Result.GetSize() - 1; i++)
{
pPoints[1] = &m_PointArray[*(pPolygonPoints+i)].m_Position;
pPoints[2] = &m_PointArray[*(pPolygonPoints+i+1)].m_Position;
TrianglePlane.Init(*pPoints[0], *pPoints[1], *pPoints[2]);
TrianglePlane.IntersectLine(RayPoints[0], RayPoints[1], IntersectionPoint);
A = GetTriangleArea(*pPoints[0], *pPoints[1], *pPoints[2]);
for(int j = 0; j < 3; j++)
{
Atmp[j] = GetTriangleArea(*pPoints[j], *pPoints[(j+1)%3], IntersectionPoint);
}
if( fabs(A - Atmp[0] - Atmp[1] - Atmp[2]) < 1.0e-5 ) return true;
}
return false;
};
double GetTriangleArea(CVector3D& T1, CVector3D& T2, CVector3D& T3)
{
double a, b, c, s;
a = (T1 - T2).length();
b = (T2 - T3).length();
c = (T3 - T1).length();
s = 0.5 * (a + b + c);
return( sqrt(s * (s - a)* (s - b)* (s - c)) );
}
When I start the program which calls SetProperty() within the for-loop, the results for some iterator values are wrong. When I set conditional breakpoints for critical iterator values in the for-loop and step over it, then the result is OK for that item. What may be the problem?
This is method in which I post breakpoint. For example, for critical element.Id==2393.
private void StartButton_Click(object sender, EventArgs e)
{
DateTime startTime = DateTime.Now;
List<Materializer> materializers = new List<Materializer>();
List<ulong> properties = new List<ulong>();
// Load STLs
for (int i = 0; (int)i < (this.dataGridView.RowCount - 1); i++)
{
if (dataGridView.Rows[i].Cells[1].Value != null && (string)dataGridView.Rows[i].Cells[1].Value != "")
{
Materializer materializer = new Materializer();
materializer.LoadSTLMesh(dataGridView.Rows[i].Cells[0].Value.ToString());
materializers.Add(materializer);
properties.Add((ulong)dataGridView.Rows[i].Cells[1].Tag);
}
}
CreatePentalTrees(materializers);
int processedElementCount = 0;
int changedElementCount = 0;
// Loop over elements
foreach (Element element in model.ElementsList.Values)
if ((element.Topology == 7 || element.Topology == 8) && !lockedProperties.ContainsKey(element.PropertyId)) // 3D elements only
{
Node center = this.CenterPoint(element, model.NodesList);
if (element.Id == 2393)
{
//if breakpoints thats ok, else not ok
Console.WriteLine(element.Id);
Console.WriteLine(element.PropertyId);
}
if (SetProperty(element, center, materializers, properties)) // Check for center point
{
//changedElements.Add(element.Id, true);
changedElementCount++;
}
else
{
// Check for all nodes if center point does not belong to any STL
int[] nodeOrder;
switch (element.Topology)
{
case 7:
nodeOrder = wedgeNodeOrder;
break;
case 8:
nodeOrder = brickNodeOrder;
break;
default:
throw new Exception("Unknown topology " + element.Topology.ToString());
}
for (int i = 0; i < nodeOrder.Length; i++)
{
Node node = model.NodesList[element.NodeIds[nodeOrder[i]]];
if (SetProperty(element, node, materializers, properties))
{
//changedElements.Add(element.Id, true);
changedElementCount++;
break;
}
}
}
if (++processedElementCount % 100 == 0)
{
labelTime.Text = "Changed/processed elements: " + changedElementCount.ToString() + "/" + processedElementCount.ToString();
labelTime.Refresh();
Application.DoEvents();
}
}
DateTime endTime = DateTime.Now;
labelTime.Text = "Total time: " + (endTime - startTime).TotalSeconds.ToString() + " s";
MessageBox.Show("Completed.");
SaveFileDialog saveFileDlg = new SaveFileDialog();
saveFileDlg.Title = "Save FEMAP neutral file";
saveFileDlg.Filter = "(*.neu)|*.neu";
if (saveFileDlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
FemapNeutral.ExportNeu(saveFileDlg.FileName, model);
}
}
You seem to be calling a lot of methods you haven't listed, and/or the wall of code made me get lost. Adding that code won't help: reducing your problem to a simpler one that demonstrates the problem might.
However, the most likely cause of your problem, if you have unmanaged code reading managed data, is that you failed to marshal or pin the data prior to using the managed code.
Unpinned data can be moved around by the garbage collector in unexpected ways.
I'm using ListView.GetItemAt(x,y) to retrieve an item from the ListView, but I'm not
able to get the item when I set the FullRowSelect option to false.
I'm not sure of how to do it in .NET 1.1. Does anyone have any pointers?
Here's a code snippet:
public int GetListViewSubItem(ListView listView1, Point pt)
{
const int LVM_FIRST = 0x1000;
const int LVM_GETSUBITEMRECT = (LVM_FIRST + 56);
const int LVIR_BOUNDS = 0;
RECT myrect;
ListViewItem lvitem = listView1.GetItemAt(pt.X, pt.Y);
if(lvitem == null && listView1.SelectedItems.Count > 0)
lvitem = listView1.SelectedItems[0];
int intLVSubItemIndex = -1;
ListViewItem.ListViewSubItem LVSubItem = null;
if(lvitem != null)
{
int intSendMessage;
for ( int i = 1; i <= lvitem.SubItems.Count - 1; i++)
{
LVSubItem = lvitem.SubItems[i];
myrect = new RECT();
myrect.top = i;
myrect.left = LVIR_BOUNDS;
intSendMessage = SendMessage(listView1.Handle,
LVM_GETSUBITEMRECT,
lvitem.Index, ref myrect);
if (pt.X < myrect.left)
{
LVSubItem = lvitem.SubItems[0];
intLVSubItemIndex = 0;
break;
}
else if (pt.X >= myrect.left & pt.X <= myrect.right)
{
intLVSubItemIndex = i;
break;
}
else
LVSubItem = null;
}
}
if (LVSubItem == null || lvitem == null)
{
intLVSubItemIndex = -1;
}
return intLVSubItemIndex;
}
This method is supposed to show me which cell was clicked.
Tt works, but if I change the fullrowselect to false, it returns a null value.
I even tried getitemat(0,e.y), but it didn't work.
whenever I select an item in the ListView, it highlighted with the color blue, so it is not possible to view what is selected. I'm trying to remove that blue highlight.
Any thoughts?
Can you elaborate a bit on what you're trying to do?
Is your ListView's ViewStyle set to Details? Since you mention setting FullRowSelect to false I assume it is.
GetItemAt(x, y) will only work if the mouse was clicked on actual text that belongs to an item. So if you've clicked on the same row as an item but in a column that it doesn't have text for, it'll return null.
A workaround for this is to simply pass 0 in as the x parameter:
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
ListViewItem item = listView1.GetItemAt(0, e.Y);
if (item == null)
{
MessageBox.Show("Null");
}
else
{
MessageBox.Show(item.Text);
}
}
I can confirm that this works fine - it returns the item that lives on that row even if only the first column has text in it.
private int getSelectedSubItemIndex(ListView listView)
{
if (listView.SelectedItems.Count == 1)
{
int x = 0;
ListViewItem item = listView.SelectedItems[0];
Point pt = listView.PointToClient(Control.MousePosition);
for (int idx = 0; idx < item.SubItems.Count; ++idx)
{
x += listView.Columns[idx].Width;
if (pt.X < x)
return idx;
}
}
return -1;
}
private void listView1_MouseClick(object sender, MouseEventArgs e)
{
int column = 0;
ListViewItem item = listView1.GetItemAt(e.X, e.Y);
for (column = 0; column < item.SubItems.Count; column++)
{
if (item.SubItems[column].Bounds.X > e.X) break;
}
if (item != null)
{
textBox1.Text = item.SubItems[column-1].Text;
}
}
How can I compute the minimum bipartite vertex cover in C#? Is there a code snippet to do so?
EDIT: while the problem is NP-complete for general graphs, it is solvable in polynomial time for bipartite graphs. I know that it's somehow related to maximum matching in bipartite graphs (by Konig's theorem) but I can't understand the theorem correctly to be able to convert the result of maximum bipartite matching to vertex cover.
I could figure it out:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class VertexCover
{
static void Main(string[] args)
{
var v = new VertexCover();
v.ParseInput();
v.FindVertexCover();
v.PrintResults();
}
private void PrintResults()
{
Console.WriteLine(String.Join(" ", VertexCoverResult.Select(x => x.ToString()).ToArray()));
}
private void FindVertexCover()
{
FindBipartiteMatching();
var TreeSet = new HashSet<int>();
foreach (var v in LeftVertices)
if (Matching[v] < 0)
DepthFirstSearch(TreeSet, v, false);
VertexCoverResult = new HashSet<int>(LeftVertices.Except(TreeSet).Union(RightVertices.Intersect(TreeSet)));
}
private void DepthFirstSearch(HashSet<int> TreeSet, int v, bool left)
{
if (TreeSet.Contains(v))
return;
TreeSet.Add(v);
if (left) {
foreach (var u in Edges[v])
if (u != Matching[v])
DepthFirstSearch(TreeSet, u, true);
} else if (Matching[v] >= 0)
DepthFirstSearch(TreeSet, Matching[v], false);
}
private void FindBipartiteMatching()
{
Bicolorate();
Matching = Enumerable.Repeat(-1, VertexCount).ToArray();
var cnt = 0;
foreach (var i in LeftVertices) {
var seen = new bool[VertexCount];
if (BipartiteMatchingInternal(seen, i)) cnt++;
}
}
private bool BipartiteMatchingInternal(bool[] seen, int u)
{
foreach (var v in Edges[u]) {
if (seen[v]) continue;
seen[v] = true;
if (Matching[v] < 0 || BipartiteMatchingInternal(seen, Matching[v])) {
Matching[u] = v;
Matching[v] = u;
return true;
}
}
return false;
}
private void Bicolorate()
{
LeftVertices = new HashSet<int>();
RightVertices = new HashSet<int>();
var colors = new int[VertexCount];
for (int i = 0; i < VertexCount; ++i)
if (colors[i] == 0 && !BicolorateInternal(colors, i, 1))
throw new InvalidOperationException("Graph is NOT bipartite.");
}
private bool BicolorateInternal(int[] colors, int i, int color)
{
if (colors[i] == 0) {
if (color == 1) LeftVertices.Add(i);
else RightVertices.Add(i);
colors[i] = color;
} else if (colors[i] != color)
return false;
else
return true;
foreach (var j in Edges[i])
if (!BicolorateInternal(colors, j, 3 - color))
return false;
return true;
}
private int VertexCount;
private HashSet<int>[] Edges;
private HashSet<int> LeftVertices;
private HashSet<int> RightVertices;
private HashSet<int> VertexCoverResult;
private int[] Matching;
private void ReadIntegerPair(out int x, out int y)
{
var input = Console.ReadLine();
var splitted = input.Split(new char[] { ' ' }, 2);
x = int.Parse(splitted[0]);
y = int.Parse(splitted[1]);
}
private void ParseInput()
{
int EdgeCount;
ReadIntegerPair(out VertexCount, out EdgeCount);
Edges = new HashSet<int>[VertexCount];
for (int i = 0; i < Edges.Length; ++i)
Edges[i] = new HashSet<int>();
for (int i = 0; i < EdgeCount; i++) {
int x, y;
ReadIntegerPair(out x, out y);
Edges[x].Add(y);
Edges[y].Add(x);
}
}
}
As you can see, this code solves the problem in polynomial time.
Its probably best to just go ahead and pick a node at random. For each node, either it goes in the vertex cover, or all of its neighbors do (since you need to include that edge). The end-result of the whole thing will be a set of vertex covers, and you pick the smallest one. I"m not going to sit here and code it out, though, since if I remember correctly, its NP complete.
private void DepthFirstSearch(HashSet TreeSet, int v, bool left)
{
if (TreeSet.Contains(v))
return;
TreeSet.Add(v);
if (left) {
foreach (var u in Edges[v])
if (u != Matching[v])
DepthFirstSearch(TreeSet, u, true);
} else if (Matching[v] >= 0)
DepthFirstSearch(TreeSet, Matching[v], false);
}
when the if(left) will execute i am figuring that it's value is always will be false.