File size: 1,076 Bytes
079c32c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { config } from "./config";
// 坐标转换函数
export const position2Coordinate = function (position, size) {
  return [Math.floor(position / size), position % size];
};

export const coordinate2Position = function (x, y, size) {
  return x * size + y;
};

// a和b是否在一条直线上,且距离小于maxDistance
export const isLine = function (a, b, size) {
  const maxDistance = config.inLineDistance;
  const [x1, y1] = position2Coordinate(a, size);
  const [x2, y2] = position2Coordinate(b, size);
  return (
    (x1 === x2 && Math.abs(y1 - y1) < maxDistance) ||
    (y1 === y2 && Math.abs(x1 - x2) < maxDistance) ||
    (Math.abs(x1 - x2) === Math.abs(y1 - y2) && Math.abs(x1 - x2) < maxDistance)
  );
}

export const isAllInLine = function (p, arr, size) {
  for (let i = 0; i < arr.length; i++) {
    if (!isLine(p, arr[i], size)) {
      return false;
    }
  }
  return true;
}
export const hasInLine = function (p, arr, size) {
  for (let i = 0; i < arr.length; i++) {
    if (isLine(p, arr[i], size)) {
      return true;
    }
  }
  return false;
}