Решение на (Floating) Points and Vectors от Гергана Грудева
Към профила на Гергана Грудева
Резултати
- 20 точки от тестове
- 0 бонус точки
- 20 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
Лог от изпълнението
Compiling solution v0.1.0 (/tmp/d20190123-22631-dh7y76/solution) Finished dev [unoptimized + debuginfo] target(s) in 4.95s Running target/debug/deps/solution-2e785d603b538f71 running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Running target/debug/deps/solution_test-29808948fb50ed3a running 15 tests test solution_test::test_equailty_symmetry ... ok test solution_test::test_equality_basic ... ok test solution_test::test_equality_floating ... ok test solution_test::test_line_constructors ... ok test solution_test::test_line_equality_by_points ... ok test solution_test::test_line_equality_by_points_and_vectors ... ok test solution_test::test_line_equality_by_vectors ... ok test solution_test::test_line_validity ... ok test solution_test::test_number_by_vector ... ok test solution_test::test_number_vector_multiplication_with_precision ... ok test solution_test::test_point_distance ... ok test solution_test::test_points_minus_points ... ok test solution_test::test_points_plus_vectors ... ok test solution_test::test_vector_by_vector ... ok test solution_test::test_vector_by_vector_cross ... ok test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Doc-tests solution running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
История (2 версии и 1 коментар)
Гергана качи решение на 26.11.2018 07:55 (преди почти 7 години)
-
fn eq_float(first: f64, second: f64) -> bool {
(first - second).abs() < std::f64::EPSILON
}
#[derive(Debug, Clone, Copy )]
pub struct Point {
x : f64,
y : f64,
z : f64
}
impl Point {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Point {x, y, z}
}
}
#[derive(Debug, Clone, Copy)]
pub struct Vector {
x : f64,
y : f64,
z : f64
}
impl Vector {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Vector {x, y, z}
}
pub fn is_null(&self) -> bool {
- self.x == 0_f64 && self.y == 0_f64 && self.z == 0_f64
+ eq_float(self.x, 0_f64) && eq_float(self.y, 0_f64) && eq_float(self.z, 0_f64)
}
pub fn norm(&self) -> f64 {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
}
use std::ops::{Add, Sub, Mul, BitXor};
impl PartialEq for Point {
fn eq(&self, other: &Point) -> bool {
eq_float(self.x, other.x) && eq_float(self.y, other.y) && eq_float(self.z, other.z)
}
}
impl PartialEq for Vector {
fn eq(&self, other: &Vector) -> bool {
eq_float(self.x, other.x) &&eq_float(self.y, other.y) && eq_float(self.z, other.z)
}
}
impl Add<Vector> for Point {
type Output = Point;
fn add(self, other: Vector) -> Point {
Point {x: self.x + other.x, y: self.y + other.y, z: self.z + other.z}
}
}
impl Add<Point> for Vector {
type Output = Point;
fn add(self, other: Point) -> Point {
other + self
}
}
impl Add<Vector> for Vector {
type Output = Vector;
fn add(self, other: Vector) -> Vector {
Vector {x: self.x + other.x, y: self.y + other.y, z: self.z + other.z}
}
}
impl Sub for Point {
type Output = Vector;
fn sub(self, other: Point) -> Vector {
Vector {x: self.x - other.x, y: self.y - other.y, z: self.z - other.z}
}
}
impl Mul for Vector {
type Output = f64;
fn mul(self, other: Vector) -> f64 {
self.x * other.x + self.y * other.y + self.z * other.z
}
}
impl Mul<Vector> for f64 {
type Output = Vector;
fn mul(self, other: Vector) -> Vector {
Vector {x: self * other.x, y: self * other.y, z: self * other.z}
}
}
impl BitXor for Vector {
type Output = Vector;
fn bitxor(self, other: Vector) -> Vector {
Vector {x: self.y * other.z - self.z * other.y,
y: self.z * other.x - self.x * other.z,
z: self.x * other.y - self.y * other.x}
}
}
#[derive(Debug)]
pub struct Line {
point: Point,
vec: Vector
Тук не бих използвал vec
, понеже съкращението се използва често за вектора от стандартната библиотека. В контекста, не мисля, че е голям проблем, но предвид, че point
е изпизано изцяло, може би и vector
си заслужаваше.
Не е нещо драматично, просто бих търсил някаква консистентна конвенция, удобно е когато имената следват някакъв шаблон.
}
impl Line {
pub fn from_pp(p1: Point, p2: Point) -> Option<Self> {
if p1 == p2 {
None
} else {
Some(Line{point: p1, vec: p2-p1})
}
}
pub fn from_pv(point: Point, vec: Vector) -> Option<Self> {
if vec.is_null() {
None
} else {
Some(Line{point, vec})
}
}
pub fn distance(&self, target: Point) -> f64 {
(self.vec ^ (self.point - target)).norm() / self.vec.norm()
}
}
impl PartialEq for Line {
fn eq(&self, other: &Line) -> bool {
- other.distance(self.point) == 0_f64 &&
- other.distance(self.vec + self.point) == 0_f64
+ eq_float(other.distance(self.point), 0_f64) &&
+ eq_float(other.distance(self.vec + self.point), 0_f64)
}
}
Тук не бих използвал
vec
, понеже съкращението се използва често за вектора от стандартната библиотека. В контекста, не мисля, че е голям проблем, но предвид, чеpoint
е изпизано изцяло, може би иvector
си заслужаваше.Не е нещо драматично, просто бих търсил някаква консистентна конвенция, удобно е когато имената следват някакъв шаблон.