react-native-rectangle-doc-scanner 1.1.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,10 +66,27 @@ const orderQuad = (points) => {
66
66
  if (points.length !== 4) {
67
67
  return points;
68
68
  }
69
- const sorted = [...points].sort((a, b) => (a.y === b.y ? a.x - b.x : a.y - b.y));
70
- const top = sorted.slice(0, 2).sort((a, b) => a.x - b.x);
71
- const bottom = sorted.slice(2, 4).sort((a, b) => a.x - b.x);
72
- return [top[0], top[1], bottom[1], bottom[0]];
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
+ }
83
+ }
84
+ return [
85
+ sorted[startIndex % 4],
86
+ sorted[(startIndex + 1) % 4],
87
+ sorted[(startIndex + 2) % 4],
88
+ sorted[(startIndex + 3) % 4],
89
+ ];
73
90
  };
74
91
  const Overlay = ({ quad, color = '#e7a649', frameSize, showGrid = true, gridColor = 'rgba(231, 166, 73, 0.35)', gridLineWidth = 2, }) => {
75
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.1.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,11 +52,33 @@ const orderQuad = (points: Point[]): Point[] => {
52
52
  return points;
53
53
  }
54
54
 
55
- const sorted = [...points].sort((a, b) => (a.y === b.y ? a.x - b.x : a.y - b.y));
56
- const top = sorted.slice(0, 2).sort((a, b) => a.x - b.x);
57
- const bottom = sorted.slice(2, 4).sort((a, b) => a.x - b.x);
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
+ );
59
+
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
+ }
74
+ }
58
75
 
59
- return [top[0], top[1], bottom[1], bottom[0]];
76
+ return [
77
+ sorted[startIndex % 4],
78
+ sorted[(startIndex + 1) % 4],
79
+ sorted[(startIndex + 2) % 4],
80
+ sorted[(startIndex + 3) % 4],
81
+ ];
60
82
  };
61
83
 
62
84
  export const Overlay: React.FC<OverlayProps> = ({