Skip to content

Instantly share code, notes, and snippets.

@AnthonyMikh
Last active February 3, 2018 21:14
Show Gist options
  • Save AnthonyMikh/d83f44790a75042446cad35d6851e996 to your computer and use it in GitHub Desktop.
Save AnthonyMikh/d83f44790a75042446cad35d6851e996 to your computer and use it in GitHub Desktop.
Решение задачи №67 от UniLecs
type BaseCoord = i32;
type DeltaCoord = u32;
type InputPoint = (BaseCoord, BaseCoord);
//gcd - greatest common divisor
//Считает НОД аргументов по алгоритму Евклида
//В случае, если один из аргументов равен нулю,
//возвращает второй
fn gcd(a: DeltaCoord, b: DeltaCoord) -> DeltaCoord {
if a < b { gcd(b, a) }
else {
if b == 0 { a }
else { gcd(b, a % b) }
}
}
fn integer_points((x1, y1): InputPoint, (x2, y2): InputPoint) -> DeltaCoord {
let delta_x = (x2 - x1).abs() as DeltaCoord; //длина проекции отрезка на ось абсцисс
let delta_y = (y2 - y1).abs() as DeltaCoord; //длина проекции отрезка на ось ординат
gcd(delta_x, delta_y) + 1
}
#[test]
fn do_tests() {
assert_eq!(integer_points((0, 5), (5, 0)), 6);
assert_eq!(integer_points((0, 0), (0, 6)), 7);
assert_eq!(integer_points((345, 1), (-345, -1)), 3);
assert_eq!(integer_points((0, 0), (341, 235)), 2);
assert_eq!(integer_points((2, 8), (10, 0)), 9);
assert_eq!(integer_points((-3, 5), (6, 17)), 4);
assert_eq!(integer_points((-10_000, 10_000), (10_000, -10_000)), 20_001);
}
@AnthonyMikh
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment