如何确定的GDI +两条线的交叉点? [英] How do I determine the intersection point of two lines in GDI+?

查看:362
本文介绍了如何确定的GDI +两条线的交叉点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET做一个绘图表面,类似于Visio中的应用程序。用户界面与Graphics.DrawLine在屏幕上连接两个物体。这个简单的实现工作得很好,但随着表面变得更加复杂,我需要一个更可靠的方法来表示的对象。其中的一个强劲的需求是确定两条线的交叉点,所以我可以指示通过某种图形的分离。

I'm using .NET to make an application with a drawing surface, similar to Visio. The UI connects two objects on the screen with Graphics.DrawLine. This simple implementation works fine, but as the surface gets more complex, I need a more robust way to represent the objects. One of these robust requirements is determining the intersection point for two lines so I can indicate separation via some kind of graphic.

所以我的问题是,任何人都可以提出一个方法来做这个?也许不同的技术(也许GraphViz的)或者算法?

So my question is, can anyone suggest a way to do this? Perhaps with a different technique (maybe GraphViz) or an algorithm?

推荐答案

由y行的代表性= MX + C是有问题对于计算机图形中,因为垂直线需要微米至是无限的。

The representation of lines by y = mx + c is problematic for computer graphics, because vertical lines require m to be infinite.

此外,在计算机图形线具有开始和结束点,不同于数学线这在某种程度上是无限的。人们通常只在线条的交叉兴趣,如果交叉点位于所涉两个线段。

Furthermore, lines in computer graphics have a start and end point, unlike mathematical lines which are infinite in extent. One is usually only interested in a crossing of lines if the crossing point lies on both the line segments in question.

如果你有两个线段,一是载体X1到X1 + V1,和一个载体X2到X2 + V2,然后定义:

If you have two line segments, one from vectors x1 to x1+v1, and one from vectors x2 to x2+v2, then define:

a = (v2.v2 v1.(x2-x1) - v1.v2 v2.(x2-x1)) / ((v1.v1)(v2.v2) - (v1.v2)^2)
b = (v1.v2 v1.(x2-x1) - v1.v1 v2.(x2-x1)) / ((v1.v1)(v2.v2) - (v1.v2)^2)

其中的矢量p =(PX,PY),q =(QX,QY),PQ是点积(PX * QX + PY * QY)。首先检查是否(v1.v1)(v2.v2)=(v1.v2)^ 2。 - 如果是,则线平行和不交叉

where for the vectors p=(px,py), q=(qx,qy), p.q is the dot product (px * qx + py * qy). First check if (v1.v1)(v2.v2) = (v1.v2)^2 - if so, the lines are parallel and do not cross.

如果它们不是平行的,则如果0℃; = A&下; = 1和0℃; = b将; = 1,上述交点位于两个线段,并且由点定

If they are not parallel, then if 0<=a<=1 and 0<=b<=1, the intersection point lies on both of the line segments, and is given by the point

x1 + a * v1

修改方程为a和b的推导如下。交点满足向量方程

Edit The derivation of the equations for a and b is as follows. The intersection point satisfies the vector equation

x1 + a*v1 = x2 + b*v2

通过利用这个方程的点积与 V1 ,并用 V2 ,我们得到两个等式:

By taking the dot product of this equation with v1, and with v2, we get two equations:

v1.v1*a - v2.v1*b = v1.(x2-x1)
v1.v2*a - v2.v2*b = v2.(x2-x1)

其中形式为a和b两个线性方程。解决这一系统(由v2.v2第一方程式及第二通过v1.v1和减法,或以其他方式相乘)给出为a和b的方程式。

which form two linear equations for a and b. Solving this system (by multiplying the first equation by v2.v2 and the second by v1.v1 and subtracting, or otherwise) gives the equations for a and b.

这篇关于如何确定的GDI +两条线的交叉点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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