DiscreteRotation


value public final class DiscreteRotation


A value class representing a rotation constrained to 90-degree increments (0, 90, 180, or 270 degrees).

This class provides utility functions for performing modulo-360 arithmetic on rotations (addition and subtraction), as well as rounding arbitrary rotation values to the nearest 90-degree step.

Summary

Nested types

public static class DiscreteRotation.Companion

Public methods

static final @NonNull DiscreteRotation
from(int degrees)

Creates a DiscreteRotation from the given integer degrees.

static final @NonNull DiscreteRotation
fromSurfaceRotation(int surfaceRotation)

Creates a DiscreteRotation from a Surface rotation constant.

final int

The rotation value in degrees.

final @NonNull DiscreteRotation
minus(int degrees)

Subtracts the specified number of degrees from this rotation.

final @NonNull DiscreteRotation

Subtracts another DiscreteRotation from this rotation.

final @NonNull DiscreteRotation
plus(int degrees)

Adds the specified number of degrees to this rotation.

final @NonNull DiscreteRotation

Adds another DiscreteRotation to this rotation.

static final @NonNull DiscreteRotation
round(float degrees)

Rounds degrees to the nearest DiscreteRotation (0, 90, 180, or 270).

static final @NonNull DiscreteRotation
round(int degrees)

Rounds degrees to the nearest DiscreteRotation (0, 90, 180, or 270).

@NonNull String

Public methods

from

Added in 1.7.0-alpha03
public static final @NonNull DiscreteRotation from(int degrees)

Creates a DiscreteRotation from the given integer degrees.

Parameters
int degrees

The rotation in degrees. Must be one of 0, 90, 180, or 270.

Returns
@NonNull DiscreteRotation

A DiscreteRotation representing the specified degrees.

Throws
IllegalArgumentException

If degrees is not a valid discrete rotation (0, 90, 180, or 270).

fromSurfaceRotation

Added in 1.7.0-alpha03
public static final @NonNull DiscreteRotation fromSurfaceRotation(int surfaceRotation)

Creates a DiscreteRotation from a Surface rotation constant.

Rotation values are relative to the device's natural orientation, Surface.ROTATION_0.

Parameters
int surfaceRotation

A Surface rotation value. Must be one of the SurfaceRotation constants (e.g., Surface.ROTATION_90).

Returns
@NonNull DiscreteRotation

The corresponding DiscreteRotation.

Throws
IllegalArgumentException

If the provided surfaceRotation is not valid.

getDegrees

Added in 1.7.0-alpha03
public final int getDegrees()

The rotation value in degrees. Guaranteed to be one of 0, 90, 180, or 270.

minus

Added in 1.7.0-alpha03
public final @NonNull DiscreteRotation minus(int degrees)

Subtracts the specified number of degrees from this rotation.

The result is normalized to the range [0, 360) degrees.

Parameters
int degrees

The degrees to subtract. Must be a valid discrete rotation (0, 90, 180, or 270).

Returns
@NonNull DiscreteRotation

The resulting DiscreteRotation.

Throws
IllegalArgumentException

If degrees is not a valid discrete rotation (0, 90, 180, or 270).

minus

Added in 1.7.0-alpha03
public final @NonNull DiscreteRotation minus(@NonNull DiscreteRotation other)

Subtracts another DiscreteRotation from this rotation.

The result is normalized to the range [0, 360) degrees.

Parameters
@NonNull DiscreteRotation other

The DiscreteRotation to subtract.

Returns
@NonNull DiscreteRotation

The resulting DiscreteRotation.

plus

Added in 1.7.0-alpha03
public final @NonNull DiscreteRotation plus(int degrees)

Adds the specified number of degrees to this rotation.

The result is normalized to the range [0, 360) degrees.

Parameters
int degrees

The degrees to add. Must be a valid discrete rotation (0, 90, 180, or 270).

Returns
@NonNull DiscreteRotation

The resulting DiscreteRotation.

Throws
IllegalArgumentException

If degrees is not a valid discrete rotation (0, 90, 180, or 270).

plus

Added in 1.7.0-alpha03
public final @NonNull DiscreteRotation plus(@NonNull DiscreteRotation other)

Adds another DiscreteRotation to this rotation.

The result is normalized to the range [0, 360) degrees.

Parameters
@NonNull DiscreteRotation other

The DiscreteRotation to add.

Returns
@NonNull DiscreteRotation

The resulting DiscreteRotation.

round

Added in 1.7.0-alpha03
public static final @NonNull DiscreteRotation round(float degrees)

Rounds degrees to the nearest DiscreteRotation (0, 90, 180, or 270).

Negative values are normalized and rounded to the nearest positive DiscreteRotation (e.g., -90.0f rounds to 270, and -45.0f rounds to 0). Boundary values (e.g., 45.0f) are rounded up to the nearest 90-degree increment (e.g., 45.0f rounds to 90).

Parameters
float degrees

The rotation in degrees to round.

Returns
@NonNull DiscreteRotation

The rounded DiscreteRotation.

round

Added in 1.7.0-alpha03
public static final @NonNull DiscreteRotation round(int degrees)

Rounds degrees to the nearest DiscreteRotation (0, 90, 180, or 270).

Negative values are normalized and rounded to the nearest positive DiscreteRotation (e.g., -90 rounds to 270, and -45 rounds to 0). Boundary values (e.g., 45) are rounded up to the nearest 90-degree increment (e.g., 45 rounds to 90).

Parameters
int degrees

The rotation in degrees to round.

Returns
@NonNull DiscreteRotation

The rounded DiscreteRotation.

toString

public @NonNull String toString()