Решение на (Floating) Points and Vectors от Димитър Николов

Обратно към всички решения

Към профила на Димитър Николов

Резултати

  • 20 точки от тестове
  • 0 бонус точки
  • 20 точки общо
  • 15 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::ops::Add;
use std::ops::Sub;
use std::ops::Mul;
use std::ops::BitXor;
#[derive(Debug, Clone, Copy)]
pub struct Point {
x: f64,
y: f64,
z: f64,
}
#[derive(Debug, Clone, Copy)]
pub struct Vector {
x: f64,
y: f64,
z: f64,
}
impl Point {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Point { x, y, z }
}
}
impl PartialEq for Point {
fn eq(&self, other: &Point) -> bool {
(self.x - other.x).abs() < std::f64::EPSILON &&
(self.y - other.y).abs() < std::f64::EPSILON &&
(self.z - other.z).abs() < std::f64::EPSILON
}
}
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 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 Vector {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Vector { x, y, z }
}
pub fn norm(self) -> f64 {
(self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
}
}
impl PartialEq for Vector {
fn eq(&self, other: &Vector) -> bool {
(self.x - other.x).abs() < std::f64::EPSILON &&
(self.y - other.y).abs() < std::f64::EPSILON &&
(self.z - other.z).abs() < std::f64::EPSILON
}
}
impl Add<Point> for Vector {
type Output = Point;
fn add(self, other: Point) -> Point {
Point {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}
impl Add 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 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 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,
}
}
}
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,
}
}
}
#[derive(Debug)]
pub struct Line {
point1: Point,
point2: Point,
}
impl Line {
pub fn from_pp(p1: Point, p2: Point) -> Option<Self> {
if p1 == p2 {
None
} else {
Some(Line {
point1: p1,
point2: p2,
})
}
}
pub fn from_pv(p: Point, v: Vector) -> Option<Self> {
if v == Vector::new(0.0, 0.0, 0.0) {
None
} else {
Some(Line {
point1: p,
point2: p + v,
})
}
}
}
impl Line {
pub fn distance(&self, target: Point) -> f64 {
let vec1 = target - self.point1;
let vec2 = self.point2 - self.point1;
(vec1 ^ vec2).norm() / vec2.norm()
}
}
impl PartialEq for Line {
fn eq(&self, other: &Line) -> bool {
other.distance(self.point1).abs() < std::f64::EPSILON &&
other.distance(self.point2).abs() < std::f64::EPSILON
}
}
fn main() {}

Лог от изпълнението

Compiling solution v0.1.0 (/tmp/d20190123-22631-11wfd9/solution)
warning: function is never used: `main`
   --> src/lib.rs:177:1
    |
177 | fn main() {}
    | ^^^^^^^^^
    |
    = note: #[warn(dead_code)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 4.54s
     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 16:00 (преди почти 5 години)

Димитър качи решение на 26.11.2018 16:06 (преди почти 5 години)

use std::ops::Add;
use std::ops::Sub;
use std::ops::Mul;
use std::ops::BitXor;
#[derive(Debug, Clone, Copy)]
pub struct Point {
x: f64,
y: f64,
z: f64,
}
#[derive(Debug, Clone, Copy)]
pub struct Vector {
x: f64,
y: f64,
z: f64,
}
impl Point {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Point { x, y, z }
}
}
impl PartialEq for Point {
fn eq(&self, other: &Point) -> bool {
- (self.x - other.x).abs() < std::f64::EPSILON && (self.y - other.y).abs() < std::f64::EPSILON
- && (self.z - other.z).abs() < std::f64::EPSILON
+ (self.x - other.x).abs() < std::f64::EPSILON &&
+ (self.y - other.y).abs() < std::f64::EPSILON &&
+ (self.z - other.z).abs() < std::f64::EPSILON
}
}
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 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 Vector {
pub fn new(x: f64, y: f64, z: f64) -> Self {
Vector { x, y, z }
}
pub fn norm(self) -> f64 {
(self.x.powi(2) + self.y.powi(2) + self.z.powi(2)).sqrt()
}
}
impl PartialEq for Vector {
fn eq(&self, other: &Vector) -> bool {
- (self.x - other.x).abs() < std::f64::EPSILON && (self.y - other.y).abs() < std::f64::EPSILON
- && (self.z - other.z).abs() < std::f64::EPSILON
+ (self.x - other.x).abs() < std::f64::EPSILON &&
+ (self.y - other.y).abs() < std::f64::EPSILON &&
+ (self.z - other.z).abs() < std::f64::EPSILON
}
}
impl Add<Point> for Vector {
type Output = Point;
fn add(self, other: Point) -> Point {
Point {
x: self.x + other.x,
y: self.y + other.y,
z: self.z + other.z,
}
}
}
impl Add 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 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 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,
}
}
}
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,
}
}
}
#[derive(Debug)]
pub struct Line {
point1: Point,
point2: Point,
}
impl Line {
pub fn from_pp(p1: Point, p2: Point) -> Option<Self> {
if p1 == p2 {
None
} else {
Some(Line {
point1: p1,
point2: p2,
})
}
}
pub fn from_pv(p: Point, v: Vector) -> Option<Self> {
if v == Vector::new(0.0, 0.0, 0.0) {
None
} else {
- Some(Line{
+ Some(Line {
point1: p,
point2: p + v,
})
}
}
}
impl Line {
pub fn distance(&self, target: Point) -> f64 {
let vec1 = target - self.point1;
let vec2 = self.point2 - self.point1;
(vec1 ^ vec2).norm() / vec2.norm()
}
}
impl PartialEq for Line {
fn eq(&self, other: &Line) -> bool {
other.distance(self.point1).abs() < std::f64::EPSILON &&
other.distance(self.point2).abs() < std::f64::EPSILON
}
}
-fn main() {
-}
+fn main() {}