e.preventDefault()}
- >
- {Array(nHorLine - 1)
- .fill(0)
- .map((_, i) => {
- return [
- ,
- ,
- ];
- })
- .concat([
- ,
- ])}
-
- )}
- {!readOnly && (
- e.preventDefault()}
+ >
+ {Array(nHorLine - 1)
+ .fill(0)
+ .map((_, i) => {
+ return [
+ ,
+ ,
+ ];
+ })
+ .concat([
+ ,
+ ])}
+
);
};
diff --git a/src/components/Info/index.js b/src/components/Info/index.js
index 64d77ac..b7b91ad 100644
--- a/src/components/Info/index.js
+++ b/src/components/Info/index.js
@@ -4,6 +4,7 @@ import {
NOT_ALLOWED_1_GRID_PROPS,
NOT_ALLOWED_2_GRID_PROPS,
} from "../../constants";
+import {getMatrix} from "../../utils";
import Grid from "../Grid";
import styles from "./styles.module.scss";
@@ -13,8 +14,9 @@ const Info = () => {
return (
<>
- setOpen(true)}>
- i
+
setOpen(true)}>
+
i
+
Info
{open && (
@@ -26,15 +28,36 @@ const Info = () => {
Connect adjacent dots with vertical or horizontal lines, creating
a single loop.
-
+
Crossovers or branches are not allowed (as shown by dotted lines
below).
-
-
+
+
diff --git a/src/components/Info/styles.module.scss b/src/components/Info/styles.module.scss
index fe869bd..4a2a94d 100644
--- a/src/components/Info/styles.module.scss
+++ b/src/components/Info/styles.module.scss
@@ -2,31 +2,46 @@
box-sizing: border-box;
}
-.button {
- position: fixed;
- bottom: 10px;
- right: 10px;
+.container {
display: flex;
align-items: center;
justify-content: center;
- background-color: #263238;
- height: 40px;
- width: 40px;
- padding: 10px;
- font-size: 20px;
- border: 2px solid #455a64;
- font-family: Georgia, 'Times New Roman', Times, serif;
-
- border-radius: 50%;
+ flex-direction: column;
+ position: fixed;
+ bottom: 10px;
+ right: 10px;
+ gap: 2px;
cursor: pointer;
- transition: all 0.2s;
- font-style: italic;
+
+ .button {
+ display: flex;
+ align-items: center;
+ justify-content: center;
+ background-color: #263238;
+ height: 40px;
+ width: 40px;
+ padding: 10px;
+ font-size: 20px;
+ border: 2px solid #455a64;
+ font-family: Georgia, 'Times New Roman', Times, serif;
+ border-radius: 50%;
+ transition: all 0.2s;
+ font-style: italic;
+ }
+
+ span {
+ font-size: 12px;
+ color: #90a4ae;
+ }
&:hover {
- border-color: #78909c;
- font-size: 22px;
transform: scale(1.1);
- color: white;
+
+ .button {
+ border-color: #78909c;
+ font-size: 22px;
+ color: white;
+ }
}
}
diff --git a/src/constants.js b/src/constants.js
index 2b1ae2c..cb74265 100644
--- a/src/constants.js
+++ b/src/constants.js
@@ -5,7 +5,6 @@ export const EDGE_STATE = {
};
export const EXAMPLE_GRID_PROPS = {
- readOnly: true,
dim: [7, 7],
edges: [
{a: [0, 0], b: [0, 1]},
@@ -135,7 +134,6 @@ export const EXAMPLE_GRID_PROPS = {
};
export const NOT_ALLOWED_1_GRID_PROPS = {
- readOnly: true,
dim: [2, 2],
edges: [
{a: [0, 1], b: [1, 1], hovered: true},
@@ -147,7 +145,6 @@ export const NOT_ALLOWED_1_GRID_PROPS = {
};
export const NOT_ALLOWED_2_GRID_PROPS = {
- readOnly: true,
dim: [2, 3],
edges: [
{a: [0, 1], b: [1, 1]},
diff --git a/src/index.css b/src/index.css
index 8684abe..ec2585e 100644
--- a/src/index.css
+++ b/src/index.css
@@ -11,9 +11,3 @@ code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
-
-body, html, #root {
- height: 100%;
- width: 100%;
- box-sizing: border-box;
-}
diff --git a/src/utils.js b/src/utils.js
index d4c44d1..0ea4c16 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,3 +1,4 @@
+import {DEFAULT_NODE} from "./components/Grid";
import {EDGE_STATE} from "./constants";
// to get multiple styles in space-seperated format
@@ -18,6 +19,88 @@ export const isValid = (n, lim) => n >= 0 && n <= lim;
// to check all elements are valid numbers
export const areValid = (n, lim) => n.every((n) => isValid(n, lim));
+export const getMatrix = ([dim1, dim2], edges = [], numbers = []) => {
+ const nRows = dim1;
+ const nCols = dim2;
+
+ let temp = new Array(dim1 + 1)
+ .fill(0)
+ .map(() => new Array(dim2 + 1).fill(DEFAULT_NODE));
+
+ if (numbers.length > 0) {
+ numbers.forEach(({r, c, n}) => {
+ if (r <= nRows && c <= nCols) {
+ temp[r - 1][c - 1] = {...temp[r - 1][c - 1], n};
+ }
+ });
+ }
+
+ if (edges.length > 0) {
+ edges.forEach(({a: [sx, sy], b: [ex, ey], notAllowed, hovered}) => {
+ const edgeState = notAllowed
+ ? EDGE_STATE.NOT_ALLOWED
+ : hovered
+ ? EDGE_STATE.HOVERED
+ : EDGE_STATE.ACTIVE;
+ let startX, startY, endX, endY, hor, ver;
+ hor = sx === ex;
+ ver = sy === ey;
+
+ // excluding cases where both points are same
+ // or where edge is not hor | ver
+ // Assumption: all edges are between two adjacent nodes
+ if ((hor && ver) || (!hor && !ver)) {
+ return;
+ }
+
+ // horizonal line
+ if (hor) {
+ startX = endX = sx;
+ if (sy > ey) {
+ startY = ey;
+ endY = sy;
+ }
+ if (sy < ey) {
+ startY = sy;
+ endY = ey;
+ }
+ }
+ // vertical line
+ if (ver) {
+ startY = endY = sy;
+ if (sx > ex) {
+ startX = ex;
+ endX = sx;
+ }
+ if (sx < ex) {
+ startX = sx;
+ endX = ex;
+ }
+ }
+
+ // check if all nodes are valid nodes
+ if (areValid([startX, endX], nRows) && areValid([startY, endY], nCols)) {
+ temp[startX][startY] = {
+ ...temp[startX][startY],
+ neigh: [
+ ...temp[startX][startY].neigh,
+ {state: edgeState, loc: [endX, endY]},
+ ],
+ };
+ temp[endX][endY] = {
+ ...temp[endX][endY],
+ neigh: [
+ ...temp[endX][endY].neigh,
+ {state: edgeState, loc: [startX, startY]},
+ ],
+ };
+ }
+ });
+ }
+
+ return temp;
+};
+
// to check if a node has an active edge
export const isNodeActive = (n) =>
Array.isArray(n?.neigh) &&