抱歉,如果這有點令人困惑,但我已經堅持了很長時間。我正在開發一個帶有 lat/lng 坐標的地圖應用程式。為了簡化,我將使用整數。
假設我有一條 n 點線,其中 n > 0(在本例中為 n = 3)。

這條線在代碼中表示為 x,y 點的陣列
[
[1, 1] // E
[2, 2] // F
[3, 3] // G
]
我需要找到一種方法將其轉換為線段陣列,其中下一段從最后一段開始。
例如,從最后一個轉換的新陣列如下所示:
[
[ //Line segment 1
[1, 1] // Point E
[2, 2] // Point F
],
[ //Line segment 2
[2, 2] // Point F
[3, 3] // Point G
]
]
我希望這是有道理的。
如果 n 是1、5、200等,這應該可以作業。我只需要演算法,所以偽代碼可以正常作業。如果你必須有一種語言而不是 typescript 或 c# 是我最好的。
提前感謝您的任何幫助/提示。我很感激。
uj5u.com熱心網友回復:
var pts = new[] { (1, 1), (2, 2), (3, 3) };
var prevPt = pts[0];
var segments = pts.Skip(1).Select(pt =>
{
var seg = new { a = prevPt, b = pt };
prevPt = pt;
return seg;
});
foreach (var s in segments) Console.WriteLine(s.a "-" s.b);
輸出:

uj5u.com熱心網友回復:
打字稿版本 FWIW
type Pair<T> = [T, T];
const points: Pair<number>[] = [
[1, 1], // E
[2, 2], // F
[3, 3], // G
];
function buildSegments(points: Pair<number>[]): Pair<number>[][] {
const collection = [];
for (let i = 1; i < points.length; i ) {
const segment = [
[
points[i - 1][0],
points[i - 1][1]
],
[
points[i][0],
points[i][1]
]
];
collection.push(segment);
}
return collection as Pair<number>[][];
}
console.log(buildSegments(points));
操場
uj5u.com熱心網友回復:
語言:打字稿
type Point = [number, number];
type LineSegment = [Point, Point];
type LineSegments = LineSegment[];
function calculate_line_segmenmts(points: Point[]) {
let lineSegments: LineSegments = [];
if (points && points.length) {
const pointPairs: LineSegments = points
.map((currentPoint: Point, currIndex: number) => [currentPoint, points[currIndex 1] || []] as LineSegment);
lineSegments = pointPairs.slice(0, pointPairs.length - 1);
}
return lineSegments;
}
插圖
function calculate_line_segmenmts(points) {
let lineSegments = [];
if (points && points.length) {
const pointPairs = points
.map((currentPoint, currIndex) => [currentPoint, points[currIndex 1] || []]);
lineSegments = pointPairs.slice(0, pointPairs.length - 1);
}
return lineSegments;
}
const points = [
[1, 1],
[2, 2],
[3, 3],
[4, 4]
];
console.log('[Empty Points]', calculate_line_segmenmts([]));
console.log('[1 Point]', calculate_line_segmenmts([
[1, 2]
])); // <-- Return empty as there is only 1 point, where as a segment requires at least 2
console.log('[2 Points]', '\n', JSON.stringify(calculate_line_segmenmts([
[1, 2],
[2, 3]
])));
console.log('[Multiple Points]', '\n', JSON.stringify(calculate_line_segmenmts(points)));
uj5u.com熱心網友回復:
這是一個經典的Fence Post 問題。
該演算法是輸出陣列中索引i處的每個專案都是由輸入陣列中位置i處的元素和位置i 1處的元素組成的對。輸出陣列包含的元素比輸入陣列少一個。如果輸入陣列中有N個元素,則輸出陣列中有N-1 個元素。
在 JavaScript 中,它可以像這樣巧妙地完成:
const input = [
[1, 1],
[2, 2],
[3, 3]
];
const output = input.map((_, i, a) => [a[i], a[i 1]]).slice(0, -1);
console.log(output);
這里map用于為原始陣列中的每個專案準備一對。該對由專案和其后的專案組成。我們可以接受這樣一個事實,即 JavaScript 語言將允許通過回傳來訪問超出陣列末尾的項undefined。結果中的最后一項將包含該對的未定義的第二個元素,因為它試圖訪問剛剛超過陣列末尾的項。這是合法的,但不是我們想要的,所以我們可以slice把最后一個元素去掉(洗掉它)。
可能是最清晰的方法:
大多數強型別語言不允許您訪問包含 N 個專案的陣列的索引 N 處的專案,因此,實際上,使用簡單的回圈或映射構造輸出陣列i是0最好N-2的,并且只需選擇索引處的專案[i]并[i 1]為您的一對。
const input = [
[1, 1],
[2, 2],
[3, 3]
];
const output = new Array(input.length - 1);
for (let i = 0; i < input.length - 1; i)
output[i] = [input[i], input[i 1]];
console.log(output);
我將上述內容撰寫為output[i] = [input[i], input[i 1]]并預先宣告了輸出陣列的長度,因為這會產生與演算法表達方式最匹配的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/445677.html
標籤:javascript C# 数组 打字稿 算法
