C#图解图理论 [英] C# Diagramming Graph Theory

查看:116
本文介绍了C#图解图理论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..
我不确定这是否是解决此问题的合适地点,但我希望是.

首先要说的不是图论作为理论的问题.
我知道如何使用C#画一条线,也知道如何画圆.
问题是我需要知道如何使C#识别在面板上绘制的2个圆通过它们之间绘制的线是连接的.
我用鼠标画出2个圆,然后从circle_1转到circle_2,在它们之间画线.注意:订单很重要.
感谢您的帮助,并提前解决.

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.

推荐答案

我不确定您的最终目标是什么,但是如果您要跟踪不同的节点和允许用户与他们进行交互,那么您将需要为面板中的每个节点(或圆圈)设置一些数据结构.

因此,如果您有类似以下内容:

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

}


这篇关于C#图解图理论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆