Skip to content

Commit 5b3f18e

Browse files
committed
TwoDSplit
1 parent 4fa8d69 commit 5b3f18e

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

rectangle-area-ii/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,17 @@ function change(
7575
}
7676
export function TwoDSplit(
7777
current: Interval,
78-
midx?: number,
79-
midy?: number,
78+
midx: number,
79+
midy: number,
8080
): Interval[] {
8181
const { left, right, up, down } = current;
8282

8383
if (left > right || down > up) return [];
8484

8585
if ((right - left) * (up - down) <= 1) return [];
86-
const mx = typeof midx !== "undefined"
87-
? midx
88-
: Math.floor((left + right) / 2);
89-
const my = typeof midy !== "undefined" ? midy : Math.floor((down + up) / 2);
86+
const mx = midx;
87+
88+
const my = midy;
9089

9190
const lr = [
9291
{ left: left, right: mx },

rectangle-area-ii/test.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import rectangleArea, { TwoDSplit } from "./index.ts";
22
import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
33
Deno.test("TwoDSplit-1", () => {
44
const current = { left: 0, right: 1000000000, down: 0, up: 1000000000 };
5-
const result = TwoDSplit(current);
5+
const { left, right, up, down } = current;
6+
const result = TwoDSplit(
7+
current,
8+
Math.floor((left + right) / 2),
9+
Math.floor((down + up) / 2),
10+
);
611
// console.log(current, result);
712
assertEquals(result, [
813
{ left: 0, right: 500000000, down: 0, up: 500000000 },
@@ -13,7 +18,12 @@ Deno.test("TwoDSplit-1", () => {
1318
});
1419
Deno.test("TwoDSplit-2", () => {
1520
const current = { left: 0, right: 3, down: 0, up: 3 };
16-
const result = TwoDSplit(current);
21+
const { left, right, up, down } = current;
22+
const result = TwoDSplit(
23+
current,
24+
Math.floor((left + right) / 2),
25+
Math.floor((down + up) / 2),
26+
);
1727
// console.log(current, result);
1828
assertEquals(result, [
1929
{ left: 0, right: 1, down: 0, up: 1 },
@@ -24,19 +34,34 @@ Deno.test("TwoDSplit-2", () => {
2434
});
2535
Deno.test("TwoDSplit-3", () => {
2636
const current = { left: 0, right: 1, down: 0, up: 1 };
27-
const result = TwoDSplit(current);
37+
const { left, right, up, down } = current;
38+
const result = TwoDSplit(
39+
current,
40+
Math.floor((left + right) / 2),
41+
Math.floor((down + up) / 2),
42+
);
2843
// console.log(current, result);
2944
assertEquals(result, []);
3045
});
3146
Deno.test("TwoDSplit-4", () => {
3247
const current = { left: 0, right: 0, down: 0, up: 0 };
33-
const result = TwoDSplit(current);
48+
const { left, right, up, down } = current;
49+
const result = TwoDSplit(
50+
current,
51+
Math.floor((left + right) / 2),
52+
Math.floor((down + up) / 2),
53+
);
3454
// console.log(current, result);
3555
assertEquals(result, []);
3656
});
3757
Deno.test("TwoDSplit-5", () => {
3858
const current = { left: 0, right: 6, down: 0, up: 1 };
39-
const result = TwoDSplit(current);
59+
const { left, right, up, down } = current;
60+
const result = TwoDSplit(
61+
current,
62+
Math.floor((left + right) / 2),
63+
Math.floor((down + up) / 2),
64+
);
4065
// console.log(current, result);
4166
assertEquals(result, [{ left: 0, right: 3, down: 0, up: 1 }, {
4267
left: 3,

0 commit comments

Comments
 (0)