
nannou์ Rect ๊ตฌ์กฐ์ฒด๋ 2D ์ฌ๊ฐํ์ ํํํ๋ ํต์ฌ์ ์ธ ํ์
์
๋๋ค.
Rect๋ ์ค์ฌ์ (center)๊ณผ ํฌ๊ธฐ(wh) ๋ก ์ ์๋๋ ์ฌ๊ฐํ์ผ๋ก, ๊ทธ๋ํฝ ํ๋ก๊ทธ๋๋ฐ์์ ํ๋ฉด ์์ญ, ๊ฒฝ๊ณ ์์ ๋ฑ์ ํํํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
use nannou::prelude::*;
// ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก Rect ์์ฑ
let rect1 = Rect::from_wh(vec2(100.0, 50.0)); // ํฌ๊ธฐ๋ก๋ถํฐ (์ค์ฌ์ (0,0))
let rect2 = Rect::from_corners(pt2(-50.0, -25.0), pt2(50.0, 25.0)); // ๋ชจ์๋ฆฌ๋ก๋ถํฐ
let rect3 = Rect::from_x_y_w_h(10.0, 20.0, 100.0, 50.0); // ์์น์ ํฌ๊ธฐ๋ก๋ถํฐ
let rect4 = Rect::from_w_h(200.0, 100.0); // ๋๋น, ๋์ด๋ก๋ถํฐ
let win = app.window_rect();
// ์ค์ฌ์ ๊ณผ ํฌ๊ธฐ
let center: Point2 = win.xy(); // ์ค์ฌ์ (0.0, 0.0)
let size: Vec2 = win.wh(); // ํฌ๊ธฐ (Vec2)
let width: f32 = win.w(); // ๋๋น
let height: f32 = win.h(); // ๋์ด
// ๋ชจ์๋ฆฌ ์ขํ
let left: f32 = win.left(); // ์ผ์ชฝ X
let right: f32 = win.right(); // ์ค๋ฅธ์ชฝ X
let top: f32 = win.top(); // ์์ชฝ Y
let bottom: f32 = win.bottom(); // ์๋์ชฝ Y
// ๋ชจ์๋ฆฌ ์ ๋ค
let top_left: Point2 = win.top_left();
let top_right: Point2 = win.top_right();
let bottom_left: Point2 = win.bottom_left();
let bottom_right: Point2 = win.bottom_right();
// ๊ฒฝ๊ณ ํ์ธ
let point = pt2(50.0, 30.0);
let contains: bool = win.contains(point); // ์ ์ด ๋ด๋ถ์ ์๋์ง ํ์ธ
// ๊ต์ฐจ ํ์ธ
let other_rect = Rect::from_wh(vec2(50.0, 50.0));
let intersection: Option<Rect> = win.intersect(other_rect); // ๊ต์ฐจ ์์ญ
// ํจ๋ฉ ์ ์ฉ
let padded = win.pad(10.0); // ๋ชจ๋ ๋ฐฉํฅ ํจ๋ฉ
let padded_top = win.pad_top(5.0); // ์์ชฝ๋ง ํจ๋ฉ
// ์ด๋
let moved = win.shift(vec2(10.0, -5.0)); // ์ด๋
// ํฌ๊ธฐ ์กฐ์
let scaled = win.scale(1.5); // ๋น์จ๋ก ์ค์ผ์ผ
// ์ ๋ ฌ
let aligned_top = Rect::from_x_y_w_h(
other_rect.x(),
win.top() - other_rect.h() * 0.5,
other_rect.w(),
other_rect.h()
);
// X์ถ์ผ๋ก ๋๋ฆฌ๊ธฐ
let stretched_x = Rect::from_x_y_w_h(
win.x(),
win.y(),
win.w() * 2.0,
win.h()
);
// ๋งคํ ์ ํธ๋ฆฌํฐ
// (ํ์ฌ ์ขํ, ์ ๊ทํ 0.0, ์ ๊ทํ 1.0, ํ๋ฉด ์ผ์ชฝ ์ขํ, ํ๋ฉด ์ค๋ฅธ์ชฝ ์ขํ)
let x = map_range(0.5, 0.0, 1.0, win.left(), win.right());
// (ํ์ฌ ์ขํ, ์ ๊ทํ 0.0, ์ ๊ทํ 1.0, ํ๋ฉด ์๋์ชฝ ์ขํ, ํ๋ฉด ์์ชฝ ์ขํ)
let y = map_range(0.5, 0.0, 1.0, win.bottom(), win.top());
// ๋น์จ ๊ธฐ๋ฐ ๋ถํ (0.0 ~ 1.0)
win.split_left(0.3) // ์ผ์ชฝ 30%
win.split_right(0.3) // ์ค๋ฅธ์ชฝ 30%
win.split_top(0.3) // ์์ชฝ 30%
win.split_bottom(0.3) // ์๋์ชฝ 30%
// ์ ๋ฐ ๋ถํ (ํธ์ ๋ฉ์๋)
win.left_half() // ์ผ์ชฝ ์ ๋ฐ = split_left(0.5)
win.right_half() // ์ค๋ฅธ์ชฝ ์ ๋ฐ = split_right(0.5)
win.top_half() // ์์ชฝ ์ ๋ฐ = split_top(0.5)
win.bottom_half() // ์๋์ชฝ ์ ๋ฐ = split_bottom(0.5)

use nannou::prelude::*;
fn main() {
nannou::sketch(view).size(800, 800).run();
}
fn view(app: &App, frame: Frame) {
let draw = app.draw();
let win = app.window_rect();
draw.background().color(BLACK);
let quadrants = [
Rect::from_corners(win.top_left(), win.xy()),
Rect::from_corners(win.xy(), win.top_right()),
Rect::from_corners(win.bottom_left(), win.xy()),
Rect::from_corners(win.xy(), win.bottom_right()),
];
// ์์ ํ๋ ํธ
let colors = [
hsla(220.0 / 360.0, 0.5, 0.1, 1.0),
hsla(330.0 / 360.0, 0.5, 0.5, 1.0),
hsla(160.0 / 360.0, 0.5, 0.5, 1.0),
hsla(270.0 / 360.0, 0.5, 0.5, 1.0),
];
// ์ฌ๋ถ๋ฉด ๊ทธ๋ฆฌ๊ธฐ
for (i, quadrant) in quadrants.iter().enumerate() {
draw.rect()
.xy(quadrant.xy())
.wh(quadrant.wh())
.color(colors[i]);
}
// ํ๋ฉด ์ค์์ ์ ์ฌ๊ฐํ
let square_size = win.w().min(win.h()) * 0.2;
let square_rect = Rect::from_w_h(square_size, square_size);
draw.rect()
.xy(square_rect.xy())
.wh(square_rect.wh())
.color(WHITE);
// === ๋ง๋ ๊ทธ๋ํ ๋ก์ง ์์ ===
let mouse_pos = app.mouse.position();
// ๋ง๋๊ฐ ์์๋ ์ผ์ชฝ x์ขํ๋ฅผ ๊ณ ์ ํฉ๋๋ค. (์ฌ๋ฐฑ 0%)
let start_x = win.left();
let bar_h = 20.0;
// ๋ง๋์ ๋๋น = (๋ง์ฐ์ค x์ขํ - ์์์ x์ขํ). ๋๋น๊ฐ ์์๊ฐ ๋์ง ์๋๋ก clampํฉ๋๋ค.
let bar_w = (mouse_pos.x - start_x).max(0.0);
// ๋ง๋์ ์ค์ฌ x์ขํ = ์์์ + (๊ณ์ฐ๋ ๋๋น / 2)
let bar_x = start_x + bar_w * 0.5;
let bar_y = win.bottom() + bar_h * 0.5 + 10.0; // ํ๋ฉด ํ๋จ์ ์ฝ๊ฐ์ ์ฌ์ ๊ณต๊ฐ ์ถ๊ฐ
draw.rect()
.x_y(bar_x, bar_y)
.w_h(bar_w, bar_h)
.color(PURPLE);
draw.to_frame(app, &frame).unwrap();
}
Rect์ ์ขํ๊ณ๋ ์ค์ฌ์ ์์ (0,0) ์ผ๋ก ์ฌ์ฉํฉ๋๋ค
left(), right(), top(), bottom()์ ํด๋น ๋ชจ์๋ฆฌ์ ์ขํ ๊ฐ์ ๋ฐํํฉ๋๋ค
wh()๋ Vec2 ํ์
์ผ๋ก ๋๋น์ ๋์ด๋ฅผ ๋ฐํํฉ๋๋ค
์๋์ฐ Rect๋ ์ผ๋ฐ์ ์ผ๋ก app.window_rect()๋ก ์ป์ ์ ์์ต๋๋ค