Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[956] Implement bordered snap to parent container
Bug: #956 Signed-off-by: Laurent Fasani <laurent.fasani@obeo.fr>
- Loading branch information
Showing
5 changed files
with
215 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 THALES GLOBAL SERVICES. | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Obeo - initial API and implementation | ||
*******************************************************************************/ | ||
import { Bounds, Point } from 'sprotty'; | ||
|
||
export enum RectangleSide { | ||
'north', | ||
'south', | ||
'west', | ||
'east', | ||
} | ||
/** | ||
* Returns the distance between the point p and the segment made of p1 and p2 | ||
* @param p the point | ||
* @param p1 the first point of the segment | ||
* @param p2 the second point of the segment | ||
*/ | ||
export function closestPointOnSegment(p: Point, p1: Point, p2: Point): Point { | ||
var A = p.x - p1.x; | ||
var B = p.y - p1.y; | ||
var C = p2.x - p1.x; | ||
var D = p2.y - p1.y; | ||
|
||
var dot = A * C + B * D; | ||
var lengthSquare = C * C + D * D; | ||
// param is the projection distance from p1 on the segment. This is the fraction of the segment that p is closest to. | ||
var param = -1; | ||
if (lengthSquare != 0) | ||
//if the line has 0 length | ||
param = dot / lengthSquare; | ||
|
||
var nearestPoint: Point = p1; | ||
|
||
if (param < 0) { | ||
// The point is not "in front of" the segment. It is "away" on the p1 side | ||
nearestPoint = { x: p1.x, y: p1.y }; | ||
} else if (param > 1) { | ||
// The point is not "in front of" the segment. It is "away" on the p1 side | ||
nearestPoint = { x: p2.x, y: p2.y }; | ||
} else { | ||
// The point is "in front of" the segment | ||
// get the projection of p on the segment | ||
nearestPoint = { x: p1.x + param * C, y: p1.y + param * D }; | ||
} | ||
|
||
return nearestPoint; | ||
} | ||
|
||
export interface SnapToRectangleInfo { | ||
pointOnRectangleSide: Point; | ||
side: RectangleSide; | ||
} | ||
|
||
/** | ||
* Returns a point on the rectangle that is the closest from the given p point. | ||
*/ | ||
export const snapToRectangle = (point: Point, rectangle: Bounds): SnapToRectangleInfo => { | ||
const upperLeftCorner: Point = { x: rectangle.x, y: rectangle.y }; | ||
const upperRightCorner: Point = { x: rectangle.x + rectangle.width, y: rectangle.y }; | ||
const bottomLeftCorner: Point = { x: rectangle.x, y: rectangle.y + rectangle.height }; | ||
const bottomRightCorner: Point = { x: rectangle.x + rectangle.width, y: rectangle.y + rectangle.height }; | ||
|
||
let closestPointInfo = computeClosestPoint(null, point, upperLeftCorner, upperRightCorner, RectangleSide.north); | ||
closestPointInfo = computeClosestPoint( | ||
closestPointInfo, | ||
point, | ||
upperRightCorner, | ||
bottomRightCorner, | ||
RectangleSide.east | ||
); | ||
closestPointInfo = computeClosestPoint( | ||
closestPointInfo, | ||
point, | ||
bottomRightCorner, | ||
bottomLeftCorner, | ||
RectangleSide.south | ||
); | ||
closestPointInfo = computeClosestPoint( | ||
closestPointInfo, | ||
point, | ||
bottomLeftCorner, | ||
upperLeftCorner, | ||
RectangleSide.west | ||
); | ||
|
||
return { pointOnRectangleSide: closestPointInfo.pointOnSegment, side: closestPointInfo.side }; | ||
}; | ||
|
||
function computeClosestPoint(current, p: Point, p1: Point, p2: Point, side: RectangleSide) { | ||
const pointOnSegment: Point = closestPointOnSegment(p, p1, p2); | ||
let distanceSquare = getDistanceSquare(p, pointOnSegment); | ||
if (current != null && distanceSquare > current.distanceSquare) { | ||
return { pointOnSegment: current.pointOnSegment, distanceSquare: current.distanceSquare, side: current.side }; | ||
} | ||
return { pointOnSegment, distanceSquare, side }; | ||
} | ||
|
||
function getDistanceSquare(p1: Point, p2: Point): number { | ||
var dx = p1.x - p2.x; | ||
var dy = p1.y - p2.y; | ||
return dx * dx + dy * dy; | ||
} |