Struct Ellipse
Represents an axis-aligned ellipse defined by a center point and two semi-axis radii, one along the X direction and one along the Y direction. Provides parametric evaluation, derived geometric properties (area, circumference, eccentricity), and point containment tests suitable for 2D gameplay, UI layout, and procedural geometry.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Geom
Assembly: ScyllaCore.dll
Syntax
[Serializable]
public struct Ellipse : IEquatable<Ellipse>
Remarks
The ellipse is stored as three fields: RadiusX, RadiusY, and Center. There is no constraint that the radii must be positive; however, geometric properties such as Area, Eccentricity, and Contains(Vector2) assume non-negative radii and may return incorrect results if negative values are supplied.
A circle is the special case where RadiusX equals RadiusY. Use the single-radius constructors Ellipse(float) or Ellipse(float, Vector2) to construct a circle conveniently. The IsCircle property tests this condition using an approximate floating-point comparison via Approximately(float, float, float).
Equality comparisons between Ellipse values use
Approximately(float, float, float) for all four components,
so two ellipses that differ only by floating-point rounding are considered equal.
This struct is marked [Serializable] and its fields carry
[SerializeField] attributes, making it fully compatible with Unity's
Inspector and serialization systems.
Constructors
Ellipse(float)
Initializes a new circular Ellipse with the same radius along
both axes, centered at the origin (UnityEngine.Vector2.zero).
The resulting ellipse will satisfy IsCircle == true.
Declaration
public Ellipse(float radius)
Parameters
| Type | Name | Description |
|---|---|---|
| float | radius | The uniform radius of the circle. Assigned to both RadiusX and RadiusY. Should be non-negative for geometrically meaningful results. |
Ellipse(float, float)
Initializes a new Ellipse with the specified semi-axis radii, centered at the origin (UnityEngine.Vector2.zero).
Declaration
public Ellipse(float radiusX, float radiusY)
Parameters
| Type | Name | Description |
|---|---|---|
| float | radiusX | The semi-axis radius along the horizontal (X) direction. Should be non-negative for geometrically meaningful results, but no validation is performed. |
| float | radiusY | The semi-axis radius along the vertical (Y) direction. Should be non-negative for geometrically meaningful results, but no validation is performed. |
Ellipse(float, float, Vector2)
Initializes a new Ellipse with the specified semi-axis radii and an explicit center point.
Declaration
public Ellipse(float radiusX, float radiusY, Vector2 center)
Parameters
| Type | Name | Description |
|---|---|---|
| float | radiusX | The semi-axis radius along the horizontal (X) direction. Should be non-negative for geometrically meaningful results, but no validation is performed. |
| float | radiusY | The semi-axis radius along the vertical (Y) direction. Should be non-negative for geometrically meaningful results, but no validation is performed. |
| Vector2 | center | The center point of the ellipse in 2D world space. All geometric operations such as Evaluate(float), GetPointAtAngle(float), and Contains(Vector2) are relative to this position. |
Ellipse(float, Vector2)
Initializes a new circular Ellipse with the same radius along
both axes and an explicit center point.
The resulting ellipse will satisfy IsCircle == true.
Declaration
public Ellipse(float radius, Vector2 center)
Parameters
| Type | Name | Description |
|---|---|---|
| float | radius | The uniform radius of the circle. Assigned to both RadiusX and RadiusY. Should be non-negative for geometrically meaningful results. |
| Vector2 | center | The center point of the circle in 2D world space. All geometric operations such as Evaluate(float), GetPointAtAngle(float), and Contains(Vector2) are relative to this position. |
Fields
Center
The center point of the ellipse in 2D world space. All parametric evaluations and containment tests are performed relative to this position. Defaults to UnityEngine.Vector2.zero when using the two-parameter constructor Ellipse(float, float).
Declaration
[SerializeField]
public Vector2 Center
Field Value
| Type | Description |
|---|---|
| Vector2 |
RadiusX
The semi-axis radius of the ellipse along the horizontal (X) direction. This value corresponds to half the total width of the ellipse's bounding rectangle. When equal to RadiusY, the ellipse is a circle.
Declaration
[SerializeField]
public float RadiusX
Field Value
| Type | Description |
|---|---|
| float |
RadiusY
The semi-axis radius of the ellipse along the vertical (Y) direction. This value corresponds to half the total height of the ellipse's bounding rectangle. When equal to RadiusX, the ellipse is a circle.
Declaration
[SerializeField]
public float RadiusY
Field Value
| Type | Description |
|---|---|
| float |
Properties
Area
Gets the exact area enclosed by the ellipse, computed as
PI * RadiusX * RadiusY. For a circle where both radii are equal to
r, this simplifies to the familiar PI * r².
Declaration
public float Area { get; }
Property Value
| Type | Description |
|---|---|
| float | The area in square units. Returns |
Circumference
Gets the approximate circumference (perimeter) of the ellipse using Ramanujan's second approximation formula. This approximation is highly accurate for most ellipses but falls back to a simpler formula for numerically degenerate cases where the discriminant of the square root term would be negative.
Declaration
public float Circumference { get; }
Property Value
| Type | Description |
|---|---|
| float |
The approximate circumference in world units. Returns
For a circle (IsCircle is |
Remarks
Ramanujan's approximation uses the auxiliary variable
h = (a - b)² / (a + b)² where a and b are the two
semi-axes. The full formula is:
C ≈ π * (a + b) * (1 + 3h / (10 + √(4 - 3h)))
For very highly eccentric ellipses the term (4 - 3h) can become
negative due to floating-point errors, which would produce a NaN result
from Mathf.Sqrt. In those cases the implementation falls back to
the simpler estimate π * (a + b), which is only accurate to a few
percent but avoids producing invalid values.
Eccentricity
Gets the eccentricity of the ellipse, a dimensionless value in the range
[0, 1) that describes how far the shape deviates from a perfect circle.
An eccentricity of 0 means the ellipse is a circle; values approaching
1 indicate increasingly elongated (flattened) shapes.
Declaration
public float Eccentricity { get; }
Property Value
| Type | Description |
|---|---|
| float |
The eccentricity computed as
Returns |
IsCircle
Gets a value indicating whether this ellipse is a circle, meaning
RadiusX and RadiusY are approximately equal.
The comparison uses Approximately(float, float, float)
to account for floating-point rounding, so ellipses constructed with equal
radii will always report true even after serialization round-trips.
Declaration
public bool IsCircle { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
SemiMajorAxis
Gets the semi-major axis of the ellipse, which is the larger of the two radii. For a circle, both radii are equal so this returns the common radius value. The semi-major axis corresponds to the longest radius from center to edge.
Declaration
public float SemiMajorAxis { get; }
Property Value
| Type | Description |
|---|---|
| float |
See Also
SemiMinorAxis
Gets the semi-minor axis of the ellipse, which is the smaller of the two radii. For a circle, both radii are equal so this returns the common radius value. The semi-minor axis corresponds to the shortest radius from center to edge.
Declaration
public float SemiMinorAxis { get; }
Property Value
| Type | Description |
|---|---|
| float |
See Also
Methods
Contains(Vector2)
Determines whether the specified point lies inside or exactly on the boundary
of this ellipse. The test uses the standard ellipse inequality:
(dx / RadiusX)² + (dy / RadiusY)² ≤ 1, where dx and dy
are the signed distances from Center to the test point.
Declaration
public bool Contains(Vector2 point)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | point | The 2D point to test, in the same coordinate space as Center. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
The boundary itself (where the expression equals exactly 1) is considered
"inside" due to the non-strict ≤ comparison. Because floating-point
arithmetic is used, points very close to the boundary may be classified
inconsistently depending on precision.
Equals(Ellipse)
Determines whether this Ellipse is approximately equal to
other by comparing all four components - RadiusX,
RadiusY, Center.x, and Center.y -
using Approximately(float, float, float) to tolerate
floating-point rounding differences.
Declaration
public bool Equals(Ellipse other)
Parameters
| Type | Name | Description |
|---|---|---|
| Ellipse | other | The Ellipse to compare with this instance. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Determines whether this Ellipse is equal to an arbitrary object.
Returns false if obj is not an Ellipse;
otherwise delegates to Equals(Ellipse) for approximate comparison.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object to compare with this instance. May be |
Returns
| Type | Description |
|---|---|
| bool |
|
Overrides
Evaluate(float)
Evaluates a point on the ellipse's perimeter at a normalized parameter
t that maps the full angular range [0°, 360°] to
the interval [0, 1]. A value of t = 0 or t = 1 returns
the rightmost point (positive X axis direction from the center), while
t = 0.25 returns the topmost point (positive Y axis direction), and so on,
following the standard counter-clockwise mathematical convention.
Declaration
public Vector2 Evaluate(float t)
Parameters
| Type | Name | Description |
|---|---|---|
| float | t | The normalized parameter in the range |
Returns
| Type | Description |
|---|---|
| Vector2 | A UnityEngine.Vector2 lying on the ellipse's perimeter at the angle
corresponding to |
Remarks
Internally converts t to a full angle in degrees
(360 * t), then to radians, and applies the standard parametric ellipse
equations: x = cos(angle) * RadiusX, y = sin(angle) * RadiusY.
Use GetPointAtAngle(float) if you already have the angle in radians.
See Also
GetHashCode()
Computes a hash code for this Ellipse derived from RadiusX, RadiusY, and Center. The hash is computed using a prime-multiplier chain (397) to distribute values across the integer range and minimize collisions.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | A 32-bit signed integer hash code. Note that because Equals(Ellipse) uses approximate floating-point comparison, two ellipses that compare as equal may produce different hash codes if their raw float bits differ. Use this struct as a dictionary key only when the exact float values are stable. |
Overrides
GetPointAtAngle(float)
Returns a point on the ellipse's perimeter at the specified angle measured in radians from the positive X axis, following the standard mathematical counter-clockwise convention.
Declaration
public Vector2 GetPointAtAngle(float radians)
Parameters
| Type | Name | Description |
|---|---|---|
| float | radians | The angle in radians measured counter-clockwise from the positive X axis.
|
Returns
| Type | Description |
|---|---|
| Vector2 | A UnityEngine.Vector2 lying on the ellipse's perimeter at the given angle,
offset from Center by the parametric displacement
|
See Also
ToString()
Returns a human-readable string representation of this ellipse that includes the values of RadiusX, RadiusY, and Center. Useful for debugging and logging purposes.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string in the format |
Overrides
Operators
operator ==(Ellipse, Ellipse)
Returns true if left and right
are approximately equal, delegating to Equals(Ellipse).
Declaration
public static bool operator ==(Ellipse left, Ellipse right)
Parameters
| Type | Name | Description |
|---|---|---|
| Ellipse | left | The left-hand Ellipse operand. |
| Ellipse | right | The right-hand Ellipse operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator !=(Ellipse, Ellipse)
Returns true if left and right
are not approximately equal, delegating to Equals(Ellipse).
Declaration
public static bool operator !=(Ellipse left, Ellipse right)
Parameters
| Type | Name | Description |
|---|---|---|
| Ellipse | left | The left-hand Ellipse operand. |
| Ellipse | right | The right-hand Ellipse operand. |
Returns
| Type | Description |
|---|---|
| bool |
|