Skip to content

Commit

Permalink
clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Mar 21, 2022
1 parent c1a29be commit 0ee882d
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions src/components/Edges/BezierEdge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,60 +21,42 @@ interface GetControlWithCurvatureParams {
y1: number;
x2: number;
y2: number;
cX: number;
cY: number;
c: number;
}

function calculateControlOffset(distance: number, curvature: number): number {
return curvature * 25 * Math.sqrt(distance);
if (distance >= 0) {
return 0.5 * distance;
} else {
return curvature * 25 * Math.sqrt(-distance);
}
}

function getControlWithCurvature({ pos, x1, y1, x2, y2, cX, cY, c }: GetControlWithCurvatureParams): [number, number] {
function getControlWithCurvature({ pos, x1, y1, x2, y2, c }: GetControlWithCurvatureParams): [number, number] {
let ctX: number, ctY: number;
switch (pos) {
case Position.Left:
{
const d = x2 - x1;
ctX = x1 - calculateControlOffset(x1 - x2, c);
ctY = y1;
if (d <= 0) {
ctX = cX;
} else {
ctX = x1 - calculateControlOffset(d, c);
}
}
break;
case Position.Right:
{
const d = x1 - x2;
ctX = x1 + calculateControlOffset(x2 - x1, c);
ctY = y1;
if (d <= 0) {
ctX = cX;
} else {
ctX = x1 + calculateControlOffset(d, c);
}
}
break;
case Position.Top:
{
const d = y2 - y1;
ctX = x1;
if (d <= 0) {
ctY = cY;
} else {
ctY = y1 - calculateControlOffset(d, c);
}
ctY = y1 - calculateControlOffset(y1 - y2, c);
}
break;
case Position.Bottom:
{
const d = y1 - y2;
ctX = x1;
if (d <= 0) {
ctY = cY;
} else {
ctY = y1 + calculateControlOffset(d, c);
}
ctY = y1 + calculateControlOffset(y2 - y1, c);
}
break;
}
Expand All @@ -89,20 +71,13 @@ export function getBezierPath({
targetY,
targetPosition = Position.Top,
curvature = 0.25,
centerX,
centerY,
}: GetBezierPathParams): string {
const [_centerX, _centerY] = getCenter({ sourceX, sourceY, targetX, targetY });
centerX = centerX ?? _centerX;
centerY = centerY ?? _centerY;
const [sourceControlX, sourceControlY] = getControlWithCurvature({
pos: sourcePosition,
x1: sourceX,
y1: sourceY,
x2: targetX,
y2: targetY,
cX: centerX,
cY: centerY,
c: curvature,
});
const [targetControlX, targetControlY] = getControlWithCurvature({
Expand All @@ -111,8 +86,6 @@ export function getBezierPath({
y1: targetY,
x2: sourceX,
y2: sourceY,
cX: centerX,
cY: centerY,
c: curvature,
});
return `M${sourceX},${sourceY} C${sourceControlX},${sourceControlY} ${targetControlX},${targetControlY} ${targetX},${targetY}`;
Expand Down

0 comments on commit 0ee882d

Please sign in to comment.