첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170
using System.Drawing;

#region 직선과 직선 교차점 찾기 - FindLineLineIntersectionPoint(line1StartPoint, line1EndPoint, line2StartPoint,
    line2EndPoint, lineIntersect, segmentIntersect, intersectionPoint, closePoint1, closePoint2, t1, t2)

/// <summary>
/// 직선과 직선 교차점 찾기
/// </summary>
/// <param name="line1StartPoint">선 1 시작점</param>
/// <param name="line1EndPoint">선 1 종료점</param>
/// <param name="line2StartPoint">선 2 시작점</param>
/// <param name="line2EndPoint">선 2 종료점</param>
/// <param name="lineIntersect">선 교차 여부</param>
/// <param name="segmentIntersect">세그먼트 교차 여부</param>
/// <param name="intersectionPoint">교차점</param>
/// <param name="closePoint1">근접점 1</param>
/// <param name="closePoint2">근접점 2</param>
/// <param name="t1">T1</param>
/// <param name="t2">T2</param>
public void FindLineLineIntersectionPoint
(
    PointF     line1StartPoint,
    PointF     line1EndPoint,
    PointF     line2StartPoint,
    PointF     line2EndPoint,
    out bool   lineIntersect,
    out bool   segmentIntersect,
    out PointF intersectionPoint,
    out PointF closePoint1,
    out PointF closePoint2,
    out float  t1,
    out float  t2
)
{
    float deltaX12 = line1EndPoint.X - line1StartPoint.X;
    float deltaY12 = line1EndPoint.Y - line1StartPoint.Y;
    float deltaX34 = line2EndPoint.X - line2StartPoint.X;
    float deltaY34 = line2EndPoint.Y - line2StartPoint.Y;

    float denominator = (deltaY12 * deltaX34 - deltaX12 * deltaY34);

    t1 = ((line1StartPoint.X - line2StartPoint.X) * deltaY34 +
          (line2StartPoint.Y - line1StartPoint.Y) * deltaX34) / denominator;

    if(float.IsInfinity(t1))
    {
        lineIntersect    = false;
        segmentIntersect = false;

        intersectionPoint = new PointF(float.NaN, float.NaN);

        closePoint1 = new PointF(float.NaN, float.NaN);
        closePoint2 = new PointF(float.NaN, float.NaN);

        t2 = float.PositiveInfinity;

        return;
    }

    lineIntersect = true;

    t2 = ((line2StartPoint.X - line1StartPoint.X) * deltaY12 +
          (line1StartPoint.Y - line2StartPoint.Y) * deltaX12) / -denominator;

    intersectionPoint = new PointF(line1StartPoint.X + deltaX12 * t1, line1StartPoint.Y + deltaY12 * t1);

    segmentIntersect = ((t1 >= 0) && (t1 <= 1) && (t2 >= 0) && (t2 <= 1));

    if(t1 < 0)
    {
        t1 = 0;
    }
    else if(t1 > 1)
    {
        t1 = 1;
    }

    if(t2 < 0)
    {
        t2 = 0;
    }
    else if(t2 > 1)
    {
        t2 = 1;
    }

    closePoint1 = new PointF(line1StartPoint.X + deltaX12 * t1, line1StartPoint.Y + deltaY12 * t1);
    closePoint2 = new PointF(line2StartPoint.X + deltaX34 * t2, line2StartPoint.Y + deltaY34 * t2);
}

#endregion
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요