< Summary

Information
Class: FixedMathSharp.Bounds.FixedSegment2d
Assembly: FixedMathSharp
File(s): /home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Primitives/FixedSegment2d.cs
Line coverage
100%
Covered lines: 20
Uncovered lines: 0
Coverable lines: 20
Total lines: 183
Line coverage: 100%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_Delta()100%11100%
get_Length()100%11100%
get_LengthSquared()100%11100%
get_Bounds()100%11100%
ClosestPoint(...)100%11100%
DistanceSquared(...)100%11100%
Deconstruct(...)100%11100%
op_Equality(...)100%11100%
op_Inequality(...)100%11100%
Equals(...)100%22100%
Equals(...)50%22100%
GetHashCode()100%11100%

File(s)

/home/runner/work/FixedMathSharp/FixedMathSharp/src/FixedMathSharp/Geometry/Primitives/FixedSegment2d.cs

#LineLine coverage
 1//=======================================================================
 2// FixedSegment2d.cs
 3//=======================================================================
 4// MIT License, Copyright (c) 2024–present David Oravsky (mrdav30)
 5// See LICENSE file in the project root for full license information.
 6//=======================================================================
 7
 8using MemoryPack;
 9using System;
 10using System.Runtime.CompilerServices;
 11using System.Text.Json.Serialization;
 12
 13namespace FixedMathSharp.Bounds;
 14
 15/// <summary>
 16/// Represents a finite line segment in two-dimensional fixed-point space.
 17/// </summary>
 18[Serializable]
 19[MemoryPackable]
 20public partial struct FixedSegment2d : IEquatable<FixedSegment2d>
 21{
 22    #region Fields
 23
 24    /// <summary>
 25    /// The start point of the segment.
 26    /// </summary>
 27    [JsonInclude]
 28    [MemoryPackOrder(0)]
 29    public Vector2d Start;
 30
 31    /// <summary>
 32    /// The end point of the segment.
 33    /// </summary>
 34    [JsonInclude]
 35    [MemoryPackOrder(1)]
 36    public Vector2d End;
 37
 38    #endregion
 39
 40    #region Constructors
 41
 42    /// <summary>
 43    /// Initializes a new segment from start and end points.
 44    /// </summary>
 45    [JsonConstructor]
 46    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 47    public FixedSegment2d(Vector2d start, Vector2d end)
 48    {
 5749        Start = start;
 5750        End = end;
 5751    }
 52
 53    #endregion
 54
 55    #region Properties
 56
 57    /// <summary>
 58    /// The vector from <see cref="Start"/> to <see cref="End"/>.
 59    /// </summary>
 60    [JsonIgnore]
 61    [MemoryPackIgnore]
 62    public Vector2d Delta
 63    {
 64        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 665        get => End - Start;
 66    }
 67
 68    /// <summary>
 69    /// The segment length.
 70    /// </summary>
 71    [JsonIgnore]
 72    [MemoryPackIgnore]
 73    public Fixed64 Length
 74    {
 75        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 276        get => Delta.Magnitude;
 77    }
 78
 79    /// <summary>
 80    /// The squared segment length.
 81    /// </summary>
 82    [JsonIgnore]
 83    [MemoryPackIgnore]
 84    public Fixed64 LengthSquared
 85    {
 86        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 287        get => Delta.MagnitudeSquared;
 88    }
 89
 90    /// <summary>
 91    /// The normalized axis-aligned area that contains this segment.
 92    /// </summary>
 93    [JsonIgnore]
 94    [MemoryPackIgnore]
 95    public FixedBoundArea Bounds
 96    {
 97        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 398        get => FixedBoundArea.FromMinMax(Start, End);
 99    }
 100
 101    #endregion
 102
 103    #region Spatial Queries
 104
 105    /// <summary>
 106    /// Finds the closest point on this finite segment to the supplied point.
 107    /// </summary>
 108    /// <remarks>
 109    /// Zero-length segments deterministically return <see cref="Start"/>.
 110    /// </remarks>
 111    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 46112    public Vector2d ClosestPoint(Vector2d point) => Vector2d.ClosestPointOnLineSegment(point, Start, End);
 113
 114    /// <summary>
 115    /// Computes the squared distance from the supplied point to this finite segment.
 116    /// </summary>
 117    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 118    public Fixed64 DistanceSquared(Vector2d point)
 119    {
 19120        return Vector2d.DistanceSquared(point, ClosestPoint(point));
 121    }
 122
 123    #endregion
 124
 125    #region Deconstruction
 126
 127    /// <summary>
 128    /// Deconstructs the segment into start and end points.
 129    /// </summary>
 130    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 131    public void Deconstruct(out Vector2d start, out Vector2d end)
 132    {
 1133        start = Start;
 1134        end = End;
 1135    }
 136
 137    #endregion
 138
 139    #region Operators
 140
 141    /// <summary>
 142    /// Determines whether two segments have the same ordered endpoints.
 143    /// </summary>
 144    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 2145    public static bool operator ==(FixedSegment2d left, FixedSegment2d right) => left.Equals(right);
 146
 147    /// <summary>
 148    /// Determines whether two segments have different ordered endpoints.
 149    /// </summary>
 150    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 2151    public static bool operator !=(FixedSegment2d left, FixedSegment2d right) => !left.Equals(right);
 152
 153    #endregion
 154
 155    #region Equality
 156
 157    /// <inheritdoc />
 158    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 159    public bool Equals(FixedSegment2d other)
 160    {
 10161        return Start == other.Start && End == other.End;
 162    }
 163
 164    /// <inheritdoc />
 165    public override bool Equals(object? obj)
 166    {
 1167        return obj is FixedSegment2d other && Equals(other);
 168    }
 169
 170    /// <inheritdoc />
 171    public override int GetHashCode()
 172    {
 173        unchecked
 174        {
 2175            int hash = 17;
 2176            hash = (hash * 31) + Start.StateHash;
 2177            hash = (hash * 31) + End.StateHash;
 2178            return hash;
 179        }
 180    }
 181
 182    #endregion
 183}