Click here to Skip to main content
15,917,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
I am not sure if this is the right place for this question but I hope it is.

First to say this is not a question about Graph Theory as a THEORY.
I know how to draw a line using C# and I know how to draw circles as well.
The problem is I need to know how to make C# RECOGNISE that 2 circles drawn on a Panel are CONNECTED though a line drawn between them.
I draw the 2 cirlces then GO from circle_1 to circle_2 with the mouse to draw the line between them. NOTE: ORDER IS IMPORTANT.
Any help is Appreciated and thaks in advance.
Posted

1 solution

I'm not entirely certain what your final goal is, but if you want to keep track of different nodes and allow the user to interact with them then your going to need to set up a few data structures for each of the nodes (or circles) that you have in your panel.

So if you had something like:

class MyNode
{
  //Used for drawing and checking against the mouse
  Point Pos;
  int Radius;

  //Used to uniquely identify this node
  int NodeID;

  //A list of nodes that are linked to this one
  List<mynode> LinkedNodes;
}</mynode>


Then you'd create instanced of this class for each node you have and draw them to your panel. You can then use the mouse events on the panel to check when the mouse is pressed and released and find which node is being selected by checking to see if the distance between the mouse position and the nodes position is less than the radius.

UPDATE: To check specifically if the line intersects two circles then your going to need to know the start and end point of the line, otherwise it's going to be rather tough to check if it intersects your circles or not (not impossible though) and to determine which nodes the circles actually represent.

So if you had a list or array of nodes then you could do something like this:

MyNode startnode = null;
MyNode endnode = null;
Point lineStartPoint;
Point lineEndPoint;

foreach(MyNode testNode in MyBigListOfNodes)
{
  Vector dist_vector = Point.Subtract(lineStartPoint, testNode.Pos);
  if(dist_vector.Length < testNode.Radius)
  {
    startnode = testNode;
    break;
  }
}

foreach(MyNode testNode in MyBigListOfNodes)
{
  Vector dist_vector = Point.Subtract(lineEndPoint, testNode.Pos);
  if(dist_vector.Length < testNode.Radius)
  {
    endnode = testNode;
    break;
  }
}

if(startnode != null && endnode != null)
{
  //The line intersects both of these nodes

}
 
Share this answer
 
v3
Comments
AliNajjar 4-Nov-10 21:50pm    
Thanks so much ,
I am trying to write an Electric Circuit Application, and actually I have a structure pretty similiar to what you presented, BUT I was trying to use Arrays instead of Lists.
The problem is that what I need to know is the way I can tell the program that the line I have just drawn on the panel is intersecting with the first circle and the second circle at the same time, so these two circles are CONNECTED, hope my question is clear engough any way.
AliNajjar 4-Nov-10 21:56pm    
By the way just for clarification, In my structure the line in the line class has a Starting_Node, and an Ending_Node to maintain the order as I have said before.
AGAIN Thanks SOOOO much for your help
AliNajjar 4-Nov-10 23:00pm    
That was really helpful, thanks so much. I believe the problem is solved. Thanks to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900