Add more tests.
rodzic
428a21d6df
commit
cbf57f7fef
|
@ -0,0 +1,21 @@
|
||||||
|
import { normalizePoint } from "./point";
|
||||||
|
|
||||||
|
describe("normalizePoint()", () => {
|
||||||
|
it("Does nothing to points w/ length 1", () => {
|
||||||
|
expect(normalizePoint({ x: 1, y: 0 })).toEqual({ x: 1, y: 0 });
|
||||||
|
expect(normalizePoint({ x: 0, y: 1 })).toEqual({ x: 0, y: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Raises an exception on points w/ length 0", () => {
|
||||||
|
expect(() => normalizePoint({ x: 0, y: 0 })).toThrow(
|
||||||
|
"Unable to normalize point with length 0"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Normalizes points", () => {
|
||||||
|
expect(normalizePoint({ x: 1, y: 1 })).toEqual({
|
||||||
|
x: 1 / Math.sqrt(2),
|
||||||
|
y: 1 / Math.sqrt(2),
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -9,6 +9,9 @@ export function subtractPoints(p1: Point, p2: Point): Point {
|
||||||
|
|
||||||
export function normalizePoint(p: Point): Point {
|
export function normalizePoint(p: Point): Point {
|
||||||
const len = Math.sqrt(Math.pow(p.x, 2) + Math.pow(p.y, 2));
|
const len = Math.sqrt(Math.pow(p.x, 2) + Math.pow(p.y, 2));
|
||||||
|
if (len === 0) {
|
||||||
|
throw new Error(`Unable to normalize point with length 0`);
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
x: p.x / len,
|
x: p.x / len,
|
||||||
y: p.y / len,
|
y: p.y / len,
|
||||||
|
|
Ładowanie…
Reference in New Issue