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 Operators to Point and Direction #88

Open
Chris3606 opened this issue Oct 18, 2022 · 1 comment
Open

Add Operators to Point and Direction #88

Chris3606 opened this issue Oct 18, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@Chris3606
Copy link
Contributor

Chris3606 commented Oct 18, 2022

Currently, you can do somePoint + someDirection to translate a point in a direction, however there are a few similar operations that either feel like they should work but don't, or would be handy in similar ways to this one:

var p = new Point(1, 2);

// Translate UpLeft using the translate function.
// Feels like it should work since the function accepts a delta,
// but overload does not exist.
p = p.Translate(Direction.UpLeft);

// Either one of these could translate the coordinate up left 3 times;
// Doesn't work because the * operator for Directions doesn't exist.
p.Translate(Direction.UpLeft * 3);
p = p + Direction.UpLeft * 3;

I propose the definition of the following functions to remedy this:

// In Point class
public Point Translate(Direction dir) => return new Point(X + dir.DeltaX, Y + dir.DeltaY);

// In direction class
public static Point operator*(Direction dir, int value) => new Point(dir.DeltaX * value, dir.DeltaY * value);
public static implicit operator Point(Direction dir) => new Point(dir.DeltaX, dir.DeltaY);
@Chris3606 Chris3606 added the enhancement New feature or request label Oct 18, 2022
@Chris3606
Copy link
Contributor Author

Also operator-(Point p) => new(-p.x, -p.y);

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

No branches or pull requests

1 participant