Skip to content

Commit

Permalink
Better rotation of transition nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewJSchoen committed Jun 29, 2023
1 parent 5d54837 commit 2ebfc03
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ function Petri() {
places[props.target].position.y) /
2,
},
time: 1,
active: false,
};
setTransitions({
Expand Down Expand Up @@ -347,11 +348,12 @@ function Petri() {
id,
name: "New Place",
position,
tokens: [],
tokens: 'finite',
},
});
setAddMode(false);
setSelectedNode(id);
setInitialMarking({...initialMarking, [id]: 0});
} else {
setSelectedNode(null);
}
Expand Down
2 changes: 1 addition & 1 deletion src/PlaceNode.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export default memo(({ isConnectable }) => {
) : place.tokens === "sink" ? (
<IoExitOutline />
) : (
marking[place?.id]
marking[place?.id] || 0
)}
</Avatar>

Expand Down
23 changes: 20 additions & 3 deletions src/atom.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { atom } from "jotai";
import { atomWithReset } from "jotai/utils";
import { mapValues, cloneDeep } from "lodash";
import { mapValues, cloneDeep, range } from "lodash";

export const tokens = atom({});

Expand Down Expand Up @@ -226,12 +226,29 @@ export const edgesAtom = atom((get) => {
return edges;
});

const coerceAngle = (angle) => {
while (angle > 180) {
angle -= 360;
}
while (angle < 0) {
angle += 180;
}
return angle
}

export const transitionArrangementsAtom = atom((get) => {
const transitions = get(transitionsAtom);
const places = get(placesAtom);
return mapValues(transitions, (transition) => {
const inputKeys = Object.keys(transition.input);
const outputKeys = Object.keys(transition.output);
const angles = [
...inputKeys.map(k=>(Math.atan2(transition.position.y - places[k].position.y, transition.position.x - places[k].position.x ) * 180) / Math.PI),
...outputKeys.map(k=>(Math.atan2(places[k].position.y - transition.position.y, places[k].position.x - transition.position.x) * 180) / Math.PI)
]

const angle = angles.reduce((a, b) => a + b, 0) / angles.length;

const sourceX =
inputKeys
.map((placeId) => places[placeId].position.x)
Expand All @@ -248,8 +265,7 @@ export const transitionArrangementsAtom = atom((get) => {
outputKeys
.map((placeId) => places[placeId].position.y)
.reduce((a, b) => a + b, 0) / outputKeys.length;
const angle =
(Math.atan2(targetY - sourceY, targetX - sourceX) * 180) / Math.PI;
//(Math.atan2(targetY - sourceY, targetX - sourceX) * 180) / Math.PI;
return {
sourceX,
sourceY,
Expand All @@ -263,3 +279,4 @@ export const transitionArrangementsAtom = atom((get) => {
export const nodeListAtom = atom((get) => Object.values(get(nodesAtom)));

export const edgeListAtom = atom((get) => Object.values(get(edgesAtom)));

0 comments on commit 2ebfc03

Please sign in to comment.