Решение на (Floating) Points and Vectors от Андрей

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

Към профила на Андрей

Резултати

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

Код

use std::ops::{Add, Sub, Mul, BitXor};
use std::cmp::{PartialEq};
use std::f64;
#[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 }
}
}
impl PartialEq for Point {
fn eq(&self, other: &Point) -> bool {
(self.x - other.x).abs() < f64::EPSILON &&
(self.y - other.y).abs() < f64::EPSILON &&
(self.z - other.z).abs() < f64::EPSILON
}
}
#[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 to_normal(self) -> Vector {
let len = self.len();
Vector {
x: self.x / len,
y: self.y / len,
z: self.z / len,
}
}
pub fn len(&self) -> f64 {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
}
impl PartialEq for Vector {
fn eq(&self, other: &Vector) -> bool {
(self.x - other.x).abs() < f64::EPSILON &&
(self.y - other.y).abs() < f64::EPSILON &&
(self.z - other.z).abs() < 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 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 Mul<Vector> for f64 {
type Output = Vector;
fn mul(self, vector: Vector) -> Vector {
Vector {
x: vector.x * self,
y: vector.y * self,
z: vector.z * self,
}
}
}
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 {
origin: Point,
direction: Vector
}
impl Line {
pub fn from_pp(p1: Point, p2: Point) -> Option<Self> {
if p1 == p2 { return None; }
Some(Line { origin: p1, direction: (p2 - p1).to_normal() })
}
pub fn from_pv(p: Point, v: Vector) -> Option<Self> {
if v.len() < f64::EPSILON { return None; }
Some(Line { origin: p, direction: v.to_normal() })
}
}
impl Line {
pub fn distance(&self, target: Point) -> f64 {
((target - self.origin) ^ self.direction).len()
}
}
impl PartialEq for Line {
fn eq(&self, other: &Line) -> bool {
let first_distance = self.distance(other.origin);
let second_distance = self.distance(other.origin + other.direction);
first_distance <= f64::EPSILON && second_distance <= f64::EPSILON
}
}

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

Compiling solution v0.1.0 (/tmp/d20190123-22631-11kl5pf/solution)
    Finished dev [unoptimized + debuginfo] target(s) in 4.40s
     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

История (3 версии и 0 коментара)

Андрей качи първо решение на 16.11.2018 22:43 (преди почти 7 години)

Андрей качи решение на 20.11.2018 00:00 (преди почти 7 години)

use std::ops::{Add, Sub, Mul, BitXor};
use std::cmp::{PartialEq};
use std::f64;
#[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 }
}
}
impl PartialEq for Point {
fn eq(&self, other: &Point) -> bool {
(self.x - other.x).abs() < f64::EPSILON &&
(self.y - other.y).abs() < f64::EPSILON &&
(self.z - other.z).abs() < f64::EPSILON
}
}
#[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 to_normal(self) -> Vector {
let len = self.len();
Vector {
x: self.x / len,
y: self.y / len,
z: self.z / len,
}
}
pub fn len(&self) -> f64 {
- self.x * self.x + self.y * self.y + self.z * self.z
+ (self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
}
impl PartialEq for Vector {
fn eq(&self, other: &Vector) -> bool {
(self.x - other.x).abs() < f64::EPSILON &&
(self.y - other.y).abs() < f64::EPSILON &&
(self.z - other.z).abs() < 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 Sub<Vector> for Point {
type Output = Point;
fn sub(self, other: Vector) -> 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 Mul<f64> for Vector {
- type Output = Vector;
-
- fn mul(self, coefficient: f64) -> Vector {
- Vector {
- x: self.x * coefficient,
- y: self.y * coefficient,
- z: self.z * coefficient,
- }
- }
-}
-
impl Mul<Vector> for f64 {
type Output = Vector;
fn mul(self, vector: Vector) -> Vector {
Vector {
x: vector.x * self,
y: vector.y * self,
z: vector.z * self,
}
}
}
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 {
origin: Point,
direction: Vector
}
impl Line {
pub fn from_pp(p1: Point, p2: Point) -> Option<Self> {
if p1 == p2 { return None; }
Some(Line { origin: p1, direction: (p2 - p1).to_normal() })
}
pub fn from_pv(p: Point, v: Vector) -> Option<Self> {
- if v.len() == 0.0 { return None; }
+ if v.len() < f64::EPSILON { return None; }
Some(Line { origin: p, direction: v.to_normal() })
}
}
impl Line {
pub fn distance(&self, target: Point) -> f64 {
((target - self.origin) ^ self.direction).len()
}
}
impl PartialEq for Line {
fn eq(&self, other: &Line) -> bool {
- self.distance(other.origin) < f64::EPSILON &&
- self.distance(other.origin + other.direction) < f64::EPSILON
+ let first_distance = self.distance(other.origin);
+ let second_distance = self.distance(other.origin + other.direction);
+
+ first_distance <= f64::EPSILON && second_distance <= f64::EPSILON
}
}

Андрей качи решение на 24.11.2018 18:43 (преди почти 7 години)

use std::ops::{Add, Sub, Mul, BitXor};
use std::cmp::{PartialEq};
use std::f64;
#[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 }
}
}
impl PartialEq for Point {
fn eq(&self, other: &Point) -> bool {
(self.x - other.x).abs() < f64::EPSILON &&
(self.y - other.y).abs() < f64::EPSILON &&
(self.z - other.z).abs() < f64::EPSILON
}
}
#[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 to_normal(self) -> Vector {
let len = self.len();
Vector {
x: self.x / len,
y: self.y / len,
z: self.z / len,
}
}
pub fn len(&self) -> f64 {
(self.x * self.x + self.y * self.y + self.z * self.z).sqrt()
}
}
impl PartialEq for Vector {
fn eq(&self, other: &Vector) -> bool {
(self.x - other.x).abs() < f64::EPSILON &&
(self.y - other.y).abs() < f64::EPSILON &&
(self.z - other.z).abs() < 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 Sub<Vector> for Point {
- type Output = Point;
-
- fn sub(self, other: Vector) -> 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 Mul<Vector> for f64 {
type Output = Vector;
fn mul(self, vector: Vector) -> Vector {
Vector {
x: vector.x * self,
y: vector.y * self,
z: vector.z * self,
}
}
}
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 {
origin: Point,
direction: Vector
}
impl Line {
pub fn from_pp(p1: Point, p2: Point) -> Option<Self> {
if p1 == p2 { return None; }
Some(Line { origin: p1, direction: (p2 - p1).to_normal() })
}
pub fn from_pv(p: Point, v: Vector) -> Option<Self> {
if v.len() < f64::EPSILON { return None; }
Some(Line { origin: p, direction: v.to_normal() })
}
}
impl Line {
pub fn distance(&self, target: Point) -> f64 {
((target - self.origin) ^ self.direction).len()
}
}
impl PartialEq for Line {
fn eq(&self, other: &Line) -> bool {
let first_distance = self.distance(other.origin);
let second_distance = self.distance(other.origin + other.direction);
first_distance <= f64::EPSILON && second_distance <= f64::EPSILON
}
-}
+}