Skip to content

Commit 918dad5

Browse files
committed
(#68) Moved scaling of Region location into separate function
1 parent 90e8dd6 commit 918dad5

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Region } from "../../region.class";
2+
import { scaleLocation } from "./scale-location.function";
3+
4+
describe("scaleLocation", () => {
5+
it("should scale location of a Region for valid scale factors", () => {
6+
// GIVEN
7+
const scaleFactor = 0.5;
8+
const inputRegion = new Region(100, 100, 10, 10);
9+
const expectedRegion = new Region(200, 200, 10, 10);
10+
11+
// WHEN
12+
const result = scaleLocation(inputRegion, scaleFactor);
13+
14+
// THEN
15+
expect(result).toEqual(expectedRegion);
16+
});
17+
18+
it("should not scale location of a Region for invalid scale factors", () => {
19+
// GIVEN
20+
const scaleFactor = 0.0;
21+
const inputRegion = new Region(100, 100, 10, 10);
22+
const expectedRegion = new Region(100, 100, 10, 10);
23+
24+
// WHEN
25+
const result = scaleLocation(inputRegion, scaleFactor);
26+
27+
// THEN
28+
expect(result).toEqual(expectedRegion);
29+
});
30+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Region } from "../../region.class";
2+
import { lowerBound } from "./bound-value.function";
3+
4+
export const scaleLocation = (
5+
result: Region,
6+
scaleFactor: number,
7+
): Region => {
8+
const boundScaleFactor = lowerBound(scaleFactor, 0.0, 1.0);
9+
return new Region(
10+
result.left / boundScaleFactor,
11+
result.top / boundScaleFactor,
12+
result.width,
13+
result.height,
14+
);
15+
};

0 commit comments

Comments
 (0)