Skip to content

Commit ffcb492

Browse files
committed
improved algorithm for generating x and y coordiantes for making new state
1 parent abb6271 commit ffcb492

File tree

3 files changed

+51
-9
lines changed

3 files changed

+51
-9
lines changed

src/components/Editor.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
StateMinSize,
4646
} from "../consts/StateSizes";
4747
import { ErrorSnackbar } from "../common/ErrorSnackbar";
48+
import { GenerateXYCoordinatesForNewState } from "../utils/GenerateXYCoordinatesForNewState";
4849

4950
export const DataContext = createContext<AutomataData>({} as AutomataData);
5051

@@ -173,11 +174,8 @@ export const Editor = () => {
173174

174175
setRowId((prev) => prev + 1);
175176

176-
const newState = new DraggableStateModel(
177-
row.state,
178-
Math.floor(Math.random() * playgroundSize.width),
179-
Math.floor(Math.random() * playgroundSize.height)
180-
);
177+
const { x, y } = GenerateXYCoordinatesForNewState(states, playgroundSize);
178+
const newState = new DraggableStateModel(row.state, x, y);
181179

182180
setStates((prev) => [...prev, newState]);
183181
};
@@ -535,8 +533,9 @@ export const Editor = () => {
535533
let stateX: number, stateY: number;
536534
// check if event has touch data (mobile) or mouse data (desktop)
537535
if (e.touches) {
538-
stateX = Math.floor(Math.random() * playgroundSize.width);
539-
stateY = Math.floor(Math.random() * playgroundSize.height);
536+
const { x, y } = GenerateXYCoordinatesForNewState(states, playgroundSize);
537+
stateX = x;
538+
stateY = y;
540539
} else {
541540
stateX = e.clientX - rect.x;
542541
stateY = e.clientY - rect.y;

src/features/components/nfaToDfa/ResultantDfa.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import { CustomAppBar } from "../../../common/CustomAppBar";
4343
import { CustomDrawer } from "../../../common/CustomDrawer";
4444
import { CustomAppBarProps } from "../../../common/props/CustomAppBarProps";
4545
import { CustomDrawerProps } from "../../../common/props/CustomDrawerProps";
46+
import { GenerateXYCoordinatesForNewState } from "../../../utils/GenerateXYCoordinatesForNewState";
4647

4748
const numberOfColumns = 3; // one for state, one for a and one for b
4849
let index = numberOfColumns;
@@ -189,12 +190,16 @@ export const ResultantDfa = (props: ResultantDfaProps) => {
189190
]);
190191

191192
// add new state to resultantDfaStates
193+
const { x, y } = GenerateXYCoordinatesForNewState(
194+
resultantDfaStates,
195+
props.playgroundSize
196+
);
192197
setResultantDfaStates((resultantDfaStates) => [
193198
...resultantDfaStates,
194199
{
195200
id: stateToProcess,
196-
x: Math.floor(Math.random() * props.playgroundSize.width),
197-
y: Math.floor(Math.random() * props.playgroundSize.height),
201+
x: x,
202+
y: y,
198203
},
199204
]);
200205

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { PlaygroundSize } from "../components/types/PlaygroundSize";
2+
import { StateDefaultSize } from "../consts/StateSizes";
3+
import { DraggableStateModel } from "../models";
4+
5+
export const GenerateXYCoordinatesForNewState = (
6+
states: DraggableStateModel[],
7+
playgroundSize: PlaygroundSize
8+
) => {
9+
return GenerateCYCoordinatesForNewStateHelper(states, playgroundSize, 0);
10+
};
11+
12+
function GenerateCYCoordinatesForNewStateHelper(
13+
states: DraggableStateModel[],
14+
playgroundSize: PlaygroundSize,
15+
index: number
16+
) {
17+
let x: number, y: number;
18+
19+
x = Math.floor(Math.random() * playgroundSize.width);
20+
y = Math.floor(Math.random() * playgroundSize.height);
21+
22+
const isOverlapping =
23+
index < 1000 &&
24+
states.some(
25+
(state) =>
26+
Math.abs(state.x - x) < StateDefaultSize &&
27+
Math.abs(state.y - y) < StateDefaultSize
28+
);
29+
30+
if (isOverlapping) {
31+
return GenerateCYCoordinatesForNewStateHelper(
32+
states,
33+
playgroundSize,
34+
++index
35+
);
36+
}
37+
return { x, y };
38+
}

0 commit comments

Comments
 (0)