圆和线相交点

时间:2015-03-28 08:34:44

标签: vb.net vb.net-2010 intersection

我正在尝试找到直线和圆相交的交叉点。但这对我不起作用。请看图片。

The other two small circles where I should get the intersecting points.

图像中的另外两个小圆圈是我应该得到相交点的地方。以下是我计算交集的函数。

Private Function FindLineCircleIntersections(ByVal cx As Single, ByVal cy As Single, ByVal radius As Single, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As Integer
        Dim dx As Single
        Dim dy As Single
        Dim A As Single
        Dim B As Single
        Dim C As Single
        Dim det As Single
        Dim t As Single
        Dim ix1, iy1, ix2, iy2 As Single

        dx = x2 - x1
        dy = y2 - y1

        A = dx * dx + dy * dy
        B = 2 * (dx * (x1 - cx) + dy * (y1 - cy))
        C = (x1 - cx) * (x1 - cx) + (y1 - cy) * (y1 - cy) - _
            radius * radius

        det = B * B - 4 * A * C
        If (A <= 0.0000001) Or (det < 0) Then
            MsgBox("No intersection")
        ElseIf det = 0 Then
            ' One solution.
            t = -B / (2 * A)
            ix1 = x1 + t * dx
            iy1 = y1 + t * dy
            Graphics.FromImage(PictureBox1.Image).FillRectangle(Brushes.Red, ix1 - 2, iy1 - 2, 4, 4)
        Else
            ' Two solutions.
            t = (-B + Sqrt(det)) / (2 * A)
            ix1 = x1 + t * dx
            iy1 = y1 + t * dy
            t = (-B - Sqrt(det)) / (2 * A)
            ix2 = x1 + t * dx
            iy2 = y1 + t * dy
            Graphics.FromImage(PictureBox1.Image).FillRectangle(Brushes.Red, ix1 - 2, iy1 - 2, 4, 4)
            Graphics.FromImage(PictureBox1.Image).FillRectangle(Brushes.Blue, ix2 - 2, iy2 - 2, 4, 4)
        End If
    End Function

0 个答案:

没有答案