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 Rect::scale_from_center #4673

Merged
merged 4 commits into from
Jul 15, 2024
Merged

Add Rect::scale_from_center #4673

merged 4 commits into from
Jul 15, 2024

Conversation

zkrising
Copy link
Contributor

@zkrising zkrising commented Jun 18, 2024

I find myself wanting this API quite a lot, and I imagine it'll probably be useful for others.

What?

This PR adds Rect::scale and Rect::scale2 functions, which work a lot like expand, but instead multiply by a scale.

i.e.

rect.scale_from_center(2.0); // rect is 2x as big, still in same center
rect.scale_from_center2(vec2(1.5, 2.0)); // rect is 1.5x as big on x axis, 2.0x as big on y axis. still in same center

Why?

Before this you either had to write this yourself or use a expand in a cumbersome way:

rect.expand2(vec2(rect.width() * scale.x / 2.0, rect.height() * scale.y / 2.0));

I find myself wanting to scale things up by a factor frequently enough, and it seems like a useful addition to have a multiply-based variant of expand.

I realise this is pretty minor, but it seems useful enough to me!

Comment on lines 194 to 202
/// Scale up by this factor in each direction, keeping the center
#[must_use]
pub fn scale(self, amnt: f32) -> Self {
self.scale2(Vec2::splat(amnt))
}

/// Scale up by this factor in each direction, keeping the center
#[must_use]
pub fn scale2(self, scale: Vec2) -> Self {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these should be called scale_from_center[2], or take an Align2 to specify which corner is the fixpoint

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to scale_from_center. I don't think taking an Align2 would be particularly useful (I can't think of a use case for wanting to scale up around non-center?), but I might be wrong on that.

crates/emath/src/rect.rs Outdated Show resolved Hide resolved
crates/emath/src/rect.rs Outdated Show resolved Hide resolved
@emilk emilk added the emath Relates to the emath crate label Jul 15, 2024
@emilk emilk changed the title feat(emath): Add scale and scale2 to Rects Add Rect:: scale_from_center Jul 15, 2024
@emilk emilk changed the title Add Rect:: scale_from_center Add Rect::scale_from_center Jul 15, 2024
@emilk emilk merged commit 27e648a into emilk:master Jul 15, 2024
18 of 20 checks passed
@zkrising
Copy link
Contributor Author

Sweet, thanks man!

@zkrising zkrising deleted the zk/rect_scale branch July 15, 2024 18:14
lucasmerlin pushed a commit to lucasmerlin/egui that referenced this pull request Jul 30, 2024
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

I find myself wanting this API quite a lot, and I imagine it'll probably
be useful for others.

# What?

This PR adds `Rect::scale` and `Rect::scale2` functions, which work a
lot like `expand`, but instead multiply by a scale.

i.e.
```rs
rect.scale(2.0); // rect is 2x as big, still in same center
rect.scale2(vec2(1.5, 2.0)); // rect is 1.5x as big on x axis, 2.0x as big on y axis. still in same center
```

# Why?

Before this you either had to write this yourself or use a `expand` in a
cumbersome way:
```rs
rect.expand2(vec2(rect.width() * scale.x / 2.0, rect.height() * scale.y / 2.0));
```

I find myself wanting to scale things up by a factor frequently enough,
and it seems like a useful addition to have a multiply-based variant of
`expand`.

I realise this is pretty minor, but it seems useful enough to me!

---------

Co-authored-by: zkldi <[email protected]>
Co-authored-by: Emil Ernerfeldt <[email protected]>
486c pushed a commit to 486c/egui that referenced this pull request Oct 9, 2024
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

I find myself wanting this API quite a lot, and I imagine it'll probably
be useful for others.

# What?

This PR adds `Rect::scale` and `Rect::scale2` functions, which work a
lot like `expand`, but instead multiply by a scale.

i.e.
```rs
rect.scale(2.0); // rect is 2x as big, still in same center
rect.scale2(vec2(1.5, 2.0)); // rect is 1.5x as big on x axis, 2.0x as big on y axis. still in same center
```

# Why?

Before this you either had to write this yourself or use a `expand` in a
cumbersome way:
```rs
rect.expand2(vec2(rect.width() * scale.x / 2.0, rect.height() * scale.y / 2.0));
```

I find myself wanting to scale things up by a factor frequently enough,
and it seems like a useful addition to have a multiply-based variant of
`expand`.

I realise this is pretty minor, but it seems useful enough to me!

---------

Co-authored-by: zkldi <[email protected]>
Co-authored-by: Emil Ernerfeldt <[email protected]>
hacknus pushed a commit to hacknus/egui that referenced this pull request Oct 30, 2024
<!--
Please read the "Making a PR" section of
[`CONTRIBUTING.md`](https://github.com/emilk/egui/blob/master/CONTRIBUTING.md)
before opening a Pull Request!

* Keep your PR:s small and focused.
* The PR title is what ends up in the changelog, so make it descriptive!
* If applicable, add a screenshot or gif.
* If it is a non-trivial addition, consider adding a demo for it to
`egui_demo_lib`, or a new example.
* Do NOT open PR:s from your `master` branch, as that makes it hard for
maintainers to add commits to your PR.
* Remember to run `cargo fmt` and `cargo clippy`.
* Open the PR as a draft until you have self-reviewed it and run
`./scripts/check.sh`.
* When you have addressed a PR comment, mark it as resolved.

Please be patient! I will review your PR, but my time is limited!
-->

I find myself wanting this API quite a lot, and I imagine it'll probably
be useful for others.

# What?

This PR adds `Rect::scale` and `Rect::scale2` functions, which work a
lot like `expand`, but instead multiply by a scale.

i.e.
```rs
rect.scale(2.0); // rect is 2x as big, still in same center
rect.scale2(vec2(1.5, 2.0)); // rect is 1.5x as big on x axis, 2.0x as big on y axis. still in same center
```

# Why?

Before this you either had to write this yourself or use a `expand` in a
cumbersome way:
```rs
rect.expand2(vec2(rect.width() * scale.x / 2.0, rect.height() * scale.y / 2.0));
```

I find myself wanting to scale things up by a factor frequently enough,
and it seems like a useful addition to have a multiply-based variant of
`expand`.

I realise this is pretty minor, but it seems useful enough to me!

---------

Co-authored-by: zkldi <[email protected]>
Co-authored-by: Emil Ernerfeldt <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emath Relates to the emath crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants