Rhombus sampling (#20082)

# Objective

- Implement `ShapeSample for Rhombus`

## Testing

- The results can be verified visually. 

---

## Showcase
### Boundary
<img width="855" height="545" alt="image"
src="https://github.com/user-attachments/assets/0cdb16a8-f26a-4f13-a5dc-dd27e251a6d0"
/>

### Interior
<img width="736" height="512" alt="Screenshot 2025-07-11 143336"
src="https://github.com/user-attachments/assets/1ba6b2af-4d89-485a-9256-f02b4e2043a7"
/>
This commit is contained in:
Lynn 2025-07-14 23:05:42 +02:00 committed by GitHub
parent 7db64a7b3b
commit c4407b1e03
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -224,6 +224,26 @@ impl ShapeSample for Annulus {
}
}
impl ShapeSample for Rhombus {
type Output = Vec2;
fn sample_interior<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec2 {
let x: f32 = rng.gen_range(0.0..=1.0);
let y: f32 = rng.gen_range(0.0..=1.0);
let unit_p = Vec2::NEG_X + x * Vec2::ONE + Vec2::new(y, -y);
unit_p * self.half_diagonals
}
fn sample_boundary<R: Rng + ?Sized>(&self, rng: &mut R) -> Vec2 {
let x: f32 = rng.gen_range(-1.0..=1.0);
let y_sign = if rng.r#gen() { -1.0 } else { 1.0 };
let y = (1.0 - ops::abs(x)) * y_sign;
Vec2::new(x, y) * self.half_diagonals
}
}
impl ShapeSample for Rectangle {
type Output = Vec2;