#[derive(Debug, Clone, Copy)]
pub struct Circle {
pub center: [f64; 2],
pub radius: f64,
}
impl Circle {
pub fn new(center: [f64; 2], radius: f64) -> Self {
Circle { center, radius }
}
pub fn area(&self) -> f64 {
std::f64::consts::PI * self.radius * self.radius
}
pub fn contains_point(&self, point: &[f64; 2]) -> bool {
super::narrowphase::point_circle_collision(point, self)
}
}
#[derive(Debug, Clone, Copy)]
pub struct LineSegment2D {
pub start: [f64; 2],
pub end: [f64; 2],
}
impl LineSegment2D {
pub fn new(start: [f64; 2], end: [f64; 2]) -> Self {
LineSegment2D { start, end }
}
pub fn length(&self) -> f64 {
let dx = self.end[0] - self.start[0];
let dy = self.end[1] - self.start[1];
(dx * dx + dy * dy).sqrt()
}
}
#[derive(Debug, Clone, Copy)]
pub struct Triangle2D {
pub v1: [f64; 2],
pub v2: [f64; 2],
pub v3: [f64; 2],
}
impl Triangle2D {
pub fn new(a: [f64; 2], b: [f64; 2], c: [f64; 2]) -> Self {
Triangle2D {
v1: a,
v2: b,
v3: c,
}
}
pub fn area(&self) -> f64 {
0.5 * ((self.v2[0] - self.v1[0]) * (self.v3[1] - self.v1[1])
- (self.v3[0] - self.v1[0]) * (self.v2[1] - self.v1[1]))
.abs()
}
pub fn contains_point(&self, point: &[f64; 2]) -> bool {
super::narrowphase::point_triangle2d_collision(point, self)
}
pub fn a(&self) -> &[f64; 2] {
&self.v1
}
pub fn b(&self) -> &[f64; 2] {
&self.v2
}
pub fn c(&self) -> &[f64; 2] {
&self.v3
}
}
#[derive(Debug, Clone, Copy)]
pub struct Box2D {
pub min: [f64; 2],
pub max: [f64; 2],
}
impl Box2D {
pub fn new(min: [f64; 2], max: [f64; 2]) -> Self {
Box2D { min, max }
}
pub fn width(&self) -> f64 {
self.max[0] - self.min[0]
}
pub fn height(&self) -> f64 {
self.max[1] - self.min[1]
}
pub fn area(&self) -> f64 {
self.width() * self.height()
}
pub fn center(&self) -> [f64; 2] {
[
(self.min[0] + self.max[0]) * 0.5,
(self.min[1] + self.max[1]) * 0.5,
]
}
pub fn contains_point(&self, point: &[f64; 2]) -> bool {
super::narrowphase::point_box2d_collision(point, self)
}
}
#[derive(Debug, Clone, Copy)]
pub struct Sphere {
pub center: [f64; 3],
pub radius: f64,
}
impl Sphere {
pub fn new(center: [f64; 3], radius: f64) -> Self {
Sphere { center, radius }
}
pub fn volume(&self) -> f64 {
(4.0 / 3.0) * std::f64::consts::PI * self.radius * self.radius * self.radius
}
pub fn contains_point(&self, point: &[f64; 3]) -> bool {
super::narrowphase::point_sphere_collision(point, self)
}
}
#[derive(Debug, Clone, Copy)]
pub struct LineSegment3D {
pub start: [f64; 3],
pub end: [f64; 3],
}
impl LineSegment3D {
pub fn new(start: [f64; 3], end: [f64; 3]) -> Self {
LineSegment3D { start, end }
}
pub fn length(&self) -> f64 {
let dx = self.end[0] - self.start[0];
let dy = self.end[1] - self.start[1];
let dz = self.end[2] - self.start[2];
(dx * dx + dy * dy + dz * dz).sqrt()
}
}
#[derive(Debug, Clone, Copy)]
pub struct Triangle3D {
pub v1: [f64; 3],
pub v2: [f64; 3],
pub v3: [f64; 3],
}
impl Triangle3D {
pub fn new(a: [f64; 3], b: [f64; 3], c: [f64; 3]) -> Self {
Triangle3D {
v1: a,
v2: b,
v3: c,
}
}
pub fn area(&self) -> f64 {
let edge1 = [
self.v2[0] - self.v1[0],
self.v2[1] - self.v1[1],
self.v2[2] - self.v1[2],
];
let edge2 = [
self.v3[0] - self.v1[0],
self.v3[1] - self.v1[1],
self.v3[2] - self.v1[2],
];
let cross = [
edge1[1] * edge2[2] - edge1[2] * edge2[1],
edge1[2] * edge2[0] - edge1[0] * edge2[2],
edge1[0] * edge2[1] - edge1[1] * edge2[0],
];
0.5 * (cross[0] * cross[0] + cross[1] * cross[1] + cross[2] * cross[2]).sqrt()
}
pub fn normal(&self) -> [f64; 3] {
let edge1 = [
self.v2[0] - self.v1[0],
self.v2[1] - self.v1[1],
self.v2[2] - self.v1[2],
];
let edge2 = [
self.v3[0] - self.v1[0],
self.v3[1] - self.v1[1],
self.v3[2] - self.v1[2],
];
let normal = [
edge1[1] * edge2[2] - edge1[2] * edge2[1],
edge1[2] * edge2[0] - edge1[0] * edge2[2],
edge1[0] * edge2[1] - edge1[1] * edge2[0],
];
let normal_length =
(normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]).sqrt();
if normal_length == 0.0 {
[0.0, 0.0, 0.0] } else {
[
normal[0] / normal_length,
normal[1] / normal_length,
normal[2] / normal_length,
]
}
}
pub fn a(&self) -> &[f64; 3] {
&self.v1
}
pub fn b(&self) -> &[f64; 3] {
&self.v2
}
pub fn c(&self) -> &[f64; 3] {
&self.v3
}
}
#[derive(Debug, Clone, Copy)]
pub struct Box3D {
pub min: [f64; 3],
pub max: [f64; 3],
}
impl Box3D {
pub fn new(min: [f64; 3], max: [f64; 3]) -> Self {
Box3D { min, max }
}
pub fn width(&self) -> f64 {
self.max[0] - self.min[0]
}
pub fn height(&self) -> f64 {
self.max[1] - self.min[1]
}
pub fn depth(&self) -> f64 {
self.max[2] - self.min[2]
}
pub fn volume(&self) -> f64 {
self.width() * self.height() * self.depth()
}
pub fn center(&self) -> [f64; 3] {
[
(self.min[0] + self.max[0]) * 0.5,
(self.min[1] + self.max[1]) * 0.5,
(self.min[2] + self.max[2]) * 0.5,
]
}
pub fn contains_point(&self, point: &[f64; 3]) -> bool {
super::narrowphase::point_box3d_collision(point, self)
}
}