Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add clockwise and counterclockwise methods to Direction #13

Closed
alice-i-cecile opened this issue Feb 2, 2023 · 3 comments · Fixed by #16
Closed

Add clockwise and counterclockwise methods to Direction #13

alice-i-cecile opened this issue Feb 2, 2023 · 3 comments · Fixed by #16
Labels
enhancement New feature or request good first issue Good for newcomers
Milestone

Comments

@alice-i-cecile
Copy link
Collaborator

This enables easy rotation code for both cameras and object positioning.

@alice-i-cecile alice-i-cecile added enhancement New feature or request good first issue Good for newcomers labels Feb 2, 2023
@alice-i-cecile
Copy link
Collaborator Author

Initial external draft and tests. These should take self, rather than a Direction argument, but otherwise this should be trivial to port over.

/// Rotates a hex [`Direction`] one step clockwise.
#[must_use]
pub fn clockwise(direction: Direction) -> Direction {
    use Direction::*;
    match direction {
        BottomRight => Bottom,
        TopRight => BottomRight,
        Top => TopRight,
        TopLeft => Top,
        BottomLeft => TopLeft,
        Bottom => BottomLeft,
    }
}

/// Rotates a hex [`Direction`] one step counterclockwise.
#[must_use]
pub fn counterclockwise(direction: Direction) -> Direction {
    use Direction::*;
    match direction {
        BottomRight => TopRight,
        TopRight => Top,
        Top => TopLeft,
        TopLeft => BottomLeft,
        BottomLeft => Bottom,
        Bottom => BottomRight,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn rotations_reverse_each_other() {
        for direction in Direction::ALL_DIRECTIONS {
            assert_eq!(direction, counterclockwise(clockwise(direction)));
            assert_eq!(direction, clockwise(counterclockwise(direction)));
        }
    }

    #[test]
    fn six_rotations_comes_home() {
        for direction in Direction::ALL_DIRECTIONS {
            let mut cw_direction = direction;
            let mut ccw_direction = direction;

            for _ in 0..6 {
                cw_direction = clockwise(cw_direction);
                ccw_direction = counterclockwise(ccw_direction);
            }

            assert_eq!(direction, cw_direction);
            assert_eq!(direction, ccw_direction);
        }
    }
}

@ManevilleF
Copy link
Owner

ManevilleF commented Feb 2, 2023

Great suggestion !

Since Hex has :

  • rotate_left for counter clockwise
  • rotate_right for clockwise

I think Direction and Hex rotation methods should follow similar patterns, I prefer right/left but clockwise/counter_clockwise are more explicit

Feel free to open a MR !

@alice-i-cecile
Copy link
Collaborator Author

Yep, I'm fine with making these match :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants