Skip to content

Instantly share code, notes, and snippets.

@karno
Created July 20, 2017 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karno/73d2d4543cf2d0a315ba95390489084a to your computer and use it in GitHub Desktop.
Save karno/73d2d4543cf2d0a315ba95390489084a to your computer and use it in GitHub Desktop.
failing compile
#[derive(Debug)]
struct Point {
x: f64,
y: f64,
}
impl std::ops::Add for Point {
type Output = Point;
fn add(self, rhs: Point) -> Point {
Point::new(self.x + rhs.x, self.y + rhs.y)
}
}
impl std::ops::Sub for Point {
type Output = Point;
fn sub(self, rhs: Point) -> Point {
Point::new(self.x - rhs.x, self.y - rhs.y)
}
}
impl Point {
pub fn new(x: f64, y: f64) -> Point {
Point { x: x, y: y }
}
pub fn zero() -> Point {
Point::new(0.0, 0.0)
}
pub fn abs(&self) -> Point {
Point::new(self.x.abs(), self.y.abs())
}
pub fn rotate_by_center(&self, rotate: Angle) -> Point {
self.rotate(Point::zero(), rotate)
}
pub fn rotate(&self, center: Point, rotate: Angle) -> Point {
let rad = match rotate {
Angle::Degree(deg) => 2.0 * deg * std::f64::consts::PI,
Angle::Radian(rad) => rad,
};
let vec = self - center;
let nx = vec.x * rad.cos() - vec.y * rad.sin();
let ny = vec.x * rad.sin() + vec.y * rad.cos();
Point::new(nx, ny)
}
}
enum Angle {
Degree(f64),
Radian(f64),
}
fn main() {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment