-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e06d4fb
commit 469858c
Showing
3 changed files
with
115 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export type Command = { radius: number, angle: number }; | ||
export type Point = number[]; | ||
export type RobotPosition = { x: number, y: number, angle: number }; | ||
export type ProcrustesResult = { rotation: number, translation: number[] }; | ||
|
||
export function distanceBetweenPoints(p1: Point, p2: Point): number { | ||
return Math.sqrt((p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2); | ||
} | ||
|
||
function findClosestPoint(line: Point[], targetPoint: Point): number { | ||
let closestPointIndex = 0; | ||
let minDistance = Infinity; | ||
|
||
line.forEach((point, index) => { | ||
const distance = distanceBetweenPoints(point, targetPoint); | ||
if (distance < minDistance) { | ||
minDistance = distance; | ||
closestPointIndex = index; | ||
} | ||
}); | ||
|
||
return closestPointIndex; // Return the index of the closest point | ||
} | ||
|
||
export function cutOffLineAtOverlap(line1: Point[], line2: Point[]): { line: Point[], distance: number, overlap: Boolean } { | ||
const line2StartPoint = line2[0]; | ||
let closestPointIndex: number; | ||
const line1End = line1[line1.length - 1]; | ||
const line2End = line2[line2.length - 1]; | ||
|
||
let finalLine: Point[]; | ||
let overlap = false; | ||
|
||
closestPointIndex = findClosestPoint(line1, line2StartPoint); | ||
if (line2End[1] > line1End[1]) { | ||
finalLine = line1.slice(0, closestPointIndex + 1); | ||
|
||
} else { | ||
finalLine = line1; | ||
overlap = true; | ||
} | ||
|
||
const trimmedLine = line1.slice(0, closestPointIndex + 1); | ||
|
||
let totalDistance = 0; | ||
|
||
for (let i = 0; i < trimmedLine.length - 1; i++) { | ||
const point1 = trimmedLine[i]; | ||
const point2 = trimmedLine[i + 1]; | ||
|
||
const distance = Math.sqrt( | ||
Math.pow(point2[0] - point1[0], 2) + Math.pow(point2[1] - point1[1], 2) | ||
); | ||
totalDistance += distance; | ||
} | ||
|
||
return {line: finalLine, distance: totalDistance, overlap}; | ||
} | ||
|
||
export function applyTranslation(line: Point[], translationVector: number[]) { | ||
return line.map(point => [ | ||
point[0] + translationVector[0], | ||
point[1] + translationVector[1] | ||
]); | ||
} |
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