feat:提交
This commit is contained in:
62
tests/dev1/dataHandle/common/ComparePoint.ts
Normal file
62
tests/dev1/dataHandle/common/ComparePoint.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
interface Vec2 { x: number; y: number }
|
||||
|
||||
export type CompareVectorFn = (v1: Vec2, v2: Vec2) => number
|
||||
|
||||
const comparePointCache: Map<string, CompareVectorFn> = new Map()
|
||||
|
||||
const ALLKEY = ['x', 'X', 'y', 'Y', 'z', 'Z']
|
||||
const KEY = ['x', 'y', 'z']
|
||||
|
||||
/**
|
||||
* 构建返回一个用来排序的函数.根据key创建排序规则.
|
||||
*
|
||||
* 当key = "xyz" 时,点集按 x从小到大,y从小到大 z从小到大
|
||||
* key = "X" 时,点集按 x从大到小
|
||||
* 以此类推.
|
||||
*
|
||||
* 例子:
|
||||
* let pts:Vector3[] =...;
|
||||
* pts.sort(comparePoint("x")); //x从小到大排序
|
||||
* pts.sort(comparePoint("zX")); //z从小到大 x从大到小
|
||||
*
|
||||
* @export
|
||||
* @param {string} sortKey
|
||||
*/
|
||||
export function ComparePoint(sortKey: string): CompareVectorFn
|
||||
{
|
||||
if (comparePointCache.has(sortKey))
|
||||
return comparePointCache.get(sortKey)
|
||||
|
||||
let sortIndex = []
|
||||
|
||||
for (let char of sortKey)
|
||||
{
|
||||
let index = ALLKEY.indexOf(char)
|
||||
|
||||
let i2 = index / 2
|
||||
let ci = Math.floor(i2)
|
||||
sortIndex.push([KEY[ci], i2 > ci ? 1 : -1])
|
||||
}
|
||||
|
||||
let compareFunction = (v1: Vec2, v2: Vec2): number =>
|
||||
{
|
||||
if (!v1)
|
||||
return -1
|
||||
if (!v2)
|
||||
return 1
|
||||
for (let s of sortIndex)
|
||||
{
|
||||
let vv1 = v1[s[0]]
|
||||
let vv2 = v2[s[0]]
|
||||
if (vv1 === vv2)
|
||||
continue
|
||||
if (vv2 > vv1)
|
||||
return s[1]
|
||||
else return -s[1]
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
comparePointCache.set(sortKey, compareFunction)
|
||||
return compareFunction
|
||||
}
|
Reference in New Issue
Block a user