react-native-rectangle-doc-scanner 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -66,18 +66,27 @@ const orderQuad = (points) => {
66
66
  if (points.length !== 4) {
67
67
  return points;
68
68
  }
69
- const sum = (p) => p.x + p.y;
70
- const diff = (p) => p.x - p.y;
71
- const topLeft = points.reduce((prev, curr) => (sum(curr) < sum(prev) ? curr : prev));
72
- const bottomRight = points.reduce((prev, curr) => (sum(curr) > sum(prev) ? curr : prev));
73
- const remaining = points.filter((p) => p !== topLeft && p !== bottomRight);
74
- if (remaining.length !== 2) {
75
- return [topLeft, bottomRight, ...remaining];
69
+ const center = points.reduce((acc, point) => ({ x: acc.x + point.x / 4, y: acc.y + point.y / 4 }), { x: 0, y: 0 });
70
+ const sorted = [...points].sort((a, b) => {
71
+ const angleA = Math.atan2(a.y - center.y, a.x - center.x);
72
+ const angleB = Math.atan2(b.y - center.y, b.x - center.x);
73
+ return angleA - angleB;
74
+ });
75
+ // Ensure the first point is the top-left (smallest y, then smallest x)
76
+ let startIndex = 0;
77
+ for (let i = 1; i < sorted.length; i += 1) {
78
+ const current = sorted[i];
79
+ const candidate = sorted[startIndex];
80
+ if (current.y < candidate.y || (current.y === candidate.y && current.x < candidate.x)) {
81
+ startIndex = i;
82
+ }
76
83
  }
77
- const [candidate1, candidate2] = remaining;
78
- const topRight = diff(candidate1) > diff(candidate2) ? candidate1 : candidate2;
79
- const bottomLeft = topRight === candidate1 ? candidate2 : candidate1;
80
- return [topLeft, topRight, bottomRight, bottomLeft];
84
+ return [
85
+ sorted[startIndex % 4],
86
+ sorted[(startIndex + 1) % 4],
87
+ sorted[(startIndex + 2) % 4],
88
+ sorted[(startIndex + 3) % 4],
89
+ ];
81
90
  };
82
91
  const Overlay = ({ quad, color = '#e7a649', frameSize, showGrid = true, gridColor = 'rgba(231, 166, 73, 0.35)', gridLineWidth = 2, }) => {
83
92
  const { width: screenWidth, height: screenHeight } = (0, react_native_1.useWindowDimensions)();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-rectangle-doc-scanner",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Native-backed document scanner for React Native with customizable overlays.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
@@ -52,22 +52,33 @@ const orderQuad = (points: Point[]): Point[] => {
52
52
  return points;
53
53
  }
54
54
 
55
- const sum = (p: Point) => p.x + p.y;
56
- const diff = (p: Point) => p.x - p.y;
57
-
58
- const topLeft = points.reduce((prev, curr) => (sum(curr) < sum(prev) ? curr : prev));
59
- const bottomRight = points.reduce((prev, curr) => (sum(curr) > sum(prev) ? curr : prev));
55
+ const center = points.reduce(
56
+ (acc, point) => ({ x: acc.x + point.x / 4, y: acc.y + point.y / 4 }),
57
+ { x: 0, y: 0 },
58
+ );
60
59
 
61
- const remaining = points.filter((p) => p !== topLeft && p !== bottomRight);
62
- if (remaining.length !== 2) {
63
- return [topLeft, bottomRight, ...remaining];
60
+ const sorted = [...points].sort((a, b) => {
61
+ const angleA = Math.atan2(a.y - center.y, a.x - center.x);
62
+ const angleB = Math.atan2(b.y - center.y, b.x - center.x);
63
+ return angleA - angleB;
64
+ });
65
+
66
+ // Ensure the first point is the top-left (smallest y, then smallest x)
67
+ let startIndex = 0;
68
+ for (let i = 1; i < sorted.length; i += 1) {
69
+ const current = sorted[i];
70
+ const candidate = sorted[startIndex];
71
+ if (current.y < candidate.y || (current.y === candidate.y && current.x < candidate.x)) {
72
+ startIndex = i;
73
+ }
64
74
  }
65
75
 
66
- const [candidate1, candidate2] = remaining;
67
- const topRight = diff(candidate1) > diff(candidate2) ? candidate1 : candidate2;
68
- const bottomLeft = topRight === candidate1 ? candidate2 : candidate1;
69
-
70
- return [topLeft, topRight, bottomRight, bottomLeft];
76
+ return [
77
+ sorted[startIndex % 4],
78
+ sorted[(startIndex + 1) % 4],
79
+ sorted[(startIndex + 2) % 4],
80
+ sorted[(startIndex + 3) % 4],
81
+ ];
71
82
  };
72
83
 
73
84
  export const Overlay: React.FC<OverlayProps> = ({