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

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

Към профила на Антонио Миндов

Резултати

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

Код

use std::f64;
use std::ops::Add;
use std::ops::Sub;
use std::ops::Mul;
use std::ops::BitXor;
fn eqF64(a: f64, b: f64) -> bool {
(a - b).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}
}
}
impl PartialEq for Vector {
fn eq(&self, other: &Vector) -> bool {
eqF64(self.x, other.x) && eqF64(self.y, other.y) && eqF64(self.z, other.z)
}
}
impl PartialEq for Point {
fn eq(&self, other: &Point) -> bool {
eqF64(self.x, other.x) && eqF64(self.y, other.y) && eqF64(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 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<f64> for Vector {
type Output = Vector;
fn mul(self, rhs: f64) -> Vector {
Vector {
x: self.x + rhs,
y: self.y + rhs,
z: self.z + rhs,
}
}
}
impl BitXor for Vector {
type Output = f64;
// rhs is the "right-hand side" of the expression `a ^ b`
fn bitxor(self, rhs: Self) -> f64 {
self.x * rhs.x + self.y * rhs.y + self.z + rhs.z
}

Това трябваше да е векторно произведение, не скаларно. Т.е. v1 * v2 = f64 щеше да е impl Mul for Vector { Output = f64 }, не BitXor. За v1 ^ v2 = v3, искахме векторно произведение.

}
#[derive(Debug)]
pub struct Line {
p: Point,
v: Vector
}
impl Line {
/// Конструиране на линия през две точки, които минават през нея. Две различни точки са
/// достатъчни, за да дефинират еднозначно линия.
///
/// Можете да получите точка и вектор, като извадите едната от другата точка.
///
/// Ако точките са една и съща, очакваме да върнете None.
///
pub fn from_pp(p1: Point, p2: Point) -> Option<Self> {
if p1 == p2 {
return None
}
Some(Line { p: p1, v: p2-p1 })
}
/// Конструиране на линия през точка за начало, и вектор, който определя посоката. Стига
/// вектора да е ненулев, това е достатъчно, за да дефинира еднозначно линия.
///
/// Може да получите две точки, като съберете дадената с вектора.
///
/// Ако вектора е нулев, очакваме да върнете None.
///
pub fn from_pv(p: Point, v: Vector) -> Option<Self> {
if v == Vector::new(0_f64,0_f64, 0_f64) {
return None
}
Some(Line {p, v})
}
pub fn distance(&self, target: Point) -> f64 {
0_f64
}
}
impl PartialEq for Line {
fn eq(&self, other: &Line) -> bool {
self.v == other.v && self.p == other.p
}
}
fn main() {
let p = Point::new(3_f64,3_f64,3_f64);
println!("{}", p.x);
}

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

Compiling solution v0.1.0 (/tmp/d20190123-22631-xgzx05/solution)
warning: unused variable: `target`
   --> src/lib.rs:143:28
    |
143 |     pub fn distance(&self, target: Point) -> f64 {
    |                            ^^^^^^ help: consider using `_target` instead
    |
    = note: #[warn(unused_variables)] on by default

warning: function is never used: `main`
   --> src/lib.rs:155:1
    |
155 | fn main() {
    | ^^^^^^^^^
    |
    = note: #[warn(dead_code)] on by default

warning: function `eqF64` should have a snake case name such as `eq_f64`
  --> src/lib.rs:8:1
   |
8  | / fn eqF64(a: f64, b: f64) -> bool {
9  | |     (a - b).abs() < std::f64::EPSILON
10 | | }
   | |_^
   |
   = note: #[warn(non_snake_case)] on by default

warning: unused variable: `target`
   --> src/lib.rs:143:28
    |
143 |     pub fn distance(&self, target: Point) -> f64 {
    |                            ^^^^^^ help: consider using `_target` instead
    |
    = note: #[warn(unused_variables)] on by default

warning: function `eqF64` should have a snake case name such as `eq_f64`
  --> src/lib.rs:8:1
   |
8  | / fn eqF64(a: f64, b: f64) -> bool {
9  | |     (a - b).abs() < std::f64::EPSILON
10 | | }
   | |_^
   |
   = note: #[warn(non_snake_case)] on by default

error[E0277]: cannot multiply `solution::Vector` to `{float}`
   --> tests/solution_test.rs:110:20
    |
110 |     assert_eq!(2.0 * v!(1.0, 2.0, 3.0), v!(2.0, 4.0, 6.0));
    |                    ^ no implementation for `{float} * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `{float}`

error[E0277]: cannot multiply `solution::Vector` to `{float}`
   --> tests/solution_test.rs:111:24
    |
111 |     assert_eq!(1.0/2.0 * v!(1.0, 2.0, 3.0), v!(0.5, 1.0, 1.5));
    |                        ^ no implementation for `{float} * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `{float}`

error[E0277]: cannot multiply `solution::Vector` to `{float}`
   --> tests/solution_test.rs:112:20
    |
112 |     assert_eq!(0.0 * v!(13.5, 3.333333, PI), v!(0.0, 0.0, 0.0));
    |                    ^ no implementation for `{float} * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `{float}`

error[E0308]: mismatched types
   --> tests/solution_test.rs:11:51
    |
11  | macro_rules! v { ($x:expr, $y:expr, $z:expr) => { Vector::new($x, $y, $z) } }
    |                                                   ^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
...
119 |     assert_eq!(v!(1.0, 1.0, 1.0) * v!(2.0, 2.0, 2.0), 6.0);
    |                                    ----------------- in this macro invocation
    |
    = note: expected type `f64`
               found type `solution::Vector`

error[E0308]: mismatched types
   --> tests/solution_test.rs:119:5
    |
119 |     assert_eq!(v!(1.0, 1.0, 1.0) * v!(2.0, 2.0, 2.0), 6.0);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `solution::Vector`, found floating-point variable
    |
    = note: expected type `solution::Vector`
               found type `{float}`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:11:51
    |
11  | macro_rules! v { ($x:expr, $y:expr, $z:expr) => { Vector::new($x, $y, $z) } }
    |                                                   ^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
...
120 |     assert_eq!(v!(1.0, 2.0, 3.0) * v!(1.0, 2.0, 3.0), 14.0);
    |                                    ----------------- in this macro invocation
    |
    = note: expected type `f64`
               found type `solution::Vector`

error[E0308]: mismatched types
   --> tests/solution_test.rs:120:5
    |
120 |     assert_eq!(v!(1.0, 2.0, 3.0) * v!(1.0, 2.0, 3.0), 14.0);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `solution::Vector`, found floating-point variable
    |
    = note: expected type `solution::Vector`
               found type `{float}`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:11:51
    |
11  | macro_rules! v { ($x:expr, $y:expr, $z:expr) => { Vector::new($x, $y, $z) } }
    |                                                   ^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
...
121 |     assert_eq!(v!(0.0, 0.0, 0.0) * v!(1.0, 2.0, 3.0), 0.0);
    |                                    ----------------- in this macro invocation
    |
    = note: expected type `f64`
               found type `solution::Vector`

error[E0308]: mismatched types
   --> tests/solution_test.rs:121:5
    |
121 |     assert_eq!(v!(0.0, 0.0, 0.0) * v!(1.0, 2.0, 3.0), 0.0);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `solution::Vector`, found floating-point variable
    |
    = note: expected type `solution::Vector`
               found type `{float}`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:11:51
    |
11  | macro_rules! v { ($x:expr, $y:expr, $z:expr) => { Vector::new($x, $y, $z) } }
    |                                                   ^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
...
122 |     assert_eq!(v!(1.0, -1.5, 0.0) * v!(-1.0, 1.5, 0.0), -3.25);
    |                                     ------------------ in this macro invocation
    |
    = note: expected type `f64`
               found type `solution::Vector`

error[E0308]: mismatched types
   --> tests/solution_test.rs:122:5
    |
122 |     assert_eq!(v!(1.0, -1.5, 0.0) * v!(-1.0, 1.5, 0.0), -3.25);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `solution::Vector`, found floating-point variable
    |
    = note: expected type `solution::Vector`
               found type `{float}`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:11:51
    |
11  | macro_rules! v { ($x:expr, $y:expr, $z:expr) => { Vector::new($x, $y, $z) } }
    |                                                   ^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
...
123 |     assert_eq!(v!(1.0, -1.0, 0.0) * v!(-1.0, -1.0, 0.0), 0.0);
    |                                     ------------------- in this macro invocation
    |
    = note: expected type `f64`
               found type `solution::Vector`

error[E0308]: mismatched types
   --> tests/solution_test.rs:123:5
    |
123 |     assert_eq!(v!(1.0, -1.0, 0.0) * v!(-1.0, -1.0, 0.0), 0.0);
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `solution::Vector`, found floating-point variable
    |
    = note: expected type `solution::Vector`
               found type `{float}`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:131:5
    |
131 |     assert_eq!(v!(1.0, -1.0, 0.0) ^ v!(-1.0, 1.0, 0.0), v!(0.0, 0.0, 0.0));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
    |
    = note: expected type `f64`
               found type `solution::Vector`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:133:5
    |
133 |     assert_eq!(v!(1.0, -1.0, 0.0) ^ v!(2.0, -2.0, 0.0), v!(0.0, 0.0, 0.0));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
    |
    = note: expected type `f64`
               found type `solution::Vector`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:136:5
    |
136 |     assert_eq!(v!(1.0, 2.0, 3.0) ^ v!(3.0, 2.0, 1.0), v!(-4.0, 8.0, -4.0));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
    |
    = note: expected type `f64`
               found type `solution::Vector`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:137:5
    |
137 |     assert_eq!(v!(1.0, 1.0, 0.0) ^ v!(1.0, 2.0, 0.0), v!(0.0, 0.0, 1.0));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
    |
    = note: expected type `f64`
               found type `solution::Vector`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0308]: mismatched types
   --> tests/solution_test.rs:138:5
    |
138 |     assert_eq!(v!(-1.0, 1.0, 0.0) ^ v!(-1.0, 2.0, 0.0), v!(0.0, 0.0, -1.0));
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected f64, found struct `solution::Vector`
    |
    = note: expected type `f64`
               found type `solution::Vector`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error[E0277]: cannot multiply `solution::Vector` to `f64`
   --> tests/solution_test.rs:145:29
    |
145 |     assert_eq!((EPS/1000.0) * v!(1.0, 1.0, 1.0), v!(0.0, 0.0, 0.0));
    |                             ^ no implementation for `f64 * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `f64`

error[E0277]: cannot multiply `solution::Vector` to `f64`
   --> tests/solution_test.rs:146:29
    |
146 |     assert_ne!((EPS*1000.0) * v!(1.0, 1.0, 1.0), v!(0.0, 0.0, 0.0));
    |                             ^ no implementation for `f64 * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `f64`

error[E0277]: cannot multiply `solution::Vector` to `f64`
   --> tests/solution_test.rs:148:29
    |
148 |     assert_eq!((EPS/1000.0) * v!(-1.0, -1.0, -1.0), v!(0.0, 0.0, 0.0));
    |                             ^ no implementation for `f64 * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `f64`

error[E0277]: cannot multiply `solution::Vector` to `f64`
   --> tests/solution_test.rs:149:29
    |
149 |     assert_ne!((EPS*1000.0) * v!(-1.0, -1.0, -1.0), v!(0.0, 0.0, 0.0));
    |                             ^ no implementation for `f64 * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `f64`

error[E0277]: cannot multiply `solution::Vector` to `f64`
   --> tests/solution_test.rs:151:30
    |
151 |     assert_eq!((-EPS/1000.0) * v!(1.0, 1.0, 1.0), v!(0.0, 0.0, 0.0));
    |                              ^ no implementation for `f64 * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `f64`

error[E0277]: cannot multiply `solution::Vector` to `f64`
   --> tests/solution_test.rs:152:30
    |
152 |     assert_ne!((-EPS*1000.0) * v!(1.0, 1.0, 1.0), v!(0.0, 0.0, 0.0));
    |                              ^ no implementation for `f64 * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `f64`

error[E0277]: cannot multiply `solution::Vector` to `f64`
   --> tests/solution_test.rs:154:30
    |
154 |     assert_eq!((-EPS/1000.0) * v!(-1.0, -1.0, -1.0), v!(0.0, 0.0, 0.0));
    |                              ^ no implementation for `f64 * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `f64`

error[E0277]: cannot multiply `solution::Vector` to `f64`
   --> tests/solution_test.rs:155:30
    |
155 |     assert_ne!((-EPS*1000.0) * v!(-1.0, -1.0, -1.0), v!(0.0, 0.0, 0.0));
    |                              ^ no implementation for `f64 * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `f64`

error[E0277]: cannot multiply `solution::Vector` to `{float}`
   --> tests/solution_test.rs:226:58
    |
226 |     assert_eq!(Line::from_pv(p, v), Line::from_pv(p, 2.0 * v));
    |                                                          ^ no implementation for `{float} * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `{float}`

error[E0277]: cannot multiply `solution::Vector` to `{float}`
   --> tests/solution_test.rs:227:64
    |
227 |     assert_eq!(Line::from_pv(p, v), Line::from_pv(p, (1.0/3.0) * v));
    |                                                                ^ no implementation for `{float} * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `{float}`

error[E0277]: cannot multiply `solution::Vector` to `{float}`
   --> tests/solution_test.rs:245:61
    |
245 |     assert_eq!(Line::from_pp(p1, p2), Line::from_pv(p1, 2.0 * (p1 - p2)));
    |                                                             ^ no implementation for `{float} * solution::Vector`
    |
    = help: the trait `std::ops::Mul<solution::Vector>` is not implemented for `{float}`

error: aborting due to 29 previous errors

Some errors occurred: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: Could not compile `solution`.

To learn more, run the command again with --verbose.

История (1 версия и 2 коментара)

Антонио качи първо решение на 26.11.2018 16:55 (преди почти 7 години)

Лесните неща като събиранията и равенствата на точки и линии изглеждат като да са щели да работят :). Умножението си го объркал от условието, нямаш умножение с Mul за вектори, а BitXor-а ти го прави.

Отвъд това, нещата за прави не работят, но това ясно. Другия път, почвай домашното от по-рано просто, като има време, по-лесно става :).