instructions

The distance between a point and a line segment is calculated in a similar way to that of a ray. The nearest point is directly taken to calculate the distance.

The geometric

As before, for performance purposes, we will also give a distance squared calculation method.

code

/// The distance from the point to the segment
static func distanceBetween(point:simd_float3, segment:Segment) -> Float{
    let position = nearestPointOnSegment(from: point, to: segment)
    return distance(position, point)
}
/// The square of the distance from the point to the segment
static func distanceSquaredBetween(point:simd_float3, segment:Segment) -> Float {
    let position = nearestPointOnSegment(from: point, to: segment)
    return distance_squared(position, point)
}

Copy the code