detect_collision.dart
4.28 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//Some magic created by chatGPT
import 'package:flutter/material.dart';
bool isOffsetInsideShape(Offset point, List<Offset> shape) {
return _isPointInPolygon(shape, point);
}
bool _isPointInPolygon(List<Offset> polygon, Offset point) {
// Use the ray-casting algorithm for checking if a point is inside a polygon
bool inside = false;
for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
if ((polygon[i].dy > point.dy) != (polygon[j].dy > point.dy) &&
(point.dx <
(polygon[j].dx - polygon[i].dx) *
(point.dy - polygon[i].dy) /
(polygon[j].dy - polygon[i].dy) +
polygon[i].dx)) {
inside = !inside;
}
}
return inside;
}
// import 'package:flutter/material.dart';
//
// bool crosshairFullyFitsIntoShape(Rect rect, List<Offset> shape) {
// final List<Offset> rectCorners = [
// Offset(rect.left, rect.top),
// Offset(rect.right, rect.top),
// Offset(rect.right, rect.bottom),
// Offset(rect.left, rect.bottom),
// ];
//
// // Check if all rect corners are inside the shape
// for (final Offset corner in rectCorners) {
// if (!_isPointInPolygon(shape, corner)) {
// return false; // If any corner is outside, the rectangle doesn't fit fully
// }
// }
//
// return true; // All corners are inside the shape
// }
//
// bool _isPointInPolygon(List<Offset> polygon, Offset point) {
// // Use the ray-casting algorithm for checking if a point is inside a polygon
// bool inside = false;
// for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
// if ((polygon[i].dy > point.dy) != (polygon[j].dy > point.dy) &&
// (point.dx <
// (polygon[j].dx - polygon[i].dx) *
// (point.dy - polygon[i].dy) /
// (polygon[j].dy - polygon[i].dy) +
// polygon[i].dx)) {
// inside = !inside;
// }
// }
// return inside;
// }
// // import 'package:flutter/material.dart';
// //
// // bool crosshairTouchesBarcode(Rect rect, List<Offset> shape) {
// // final List<Offset> rectCorners = [
// // Offset(rect.left, rect.top),
// // Offset(rect.right, rect.top),
// // Offset(rect.right, rect.bottom),
// // Offset(rect.left, rect.bottom),
// // ];
// // final List<Offset> edges = [shape[0], shape[1], shape[2], shape[3], shape[0]];
// //
// // // Check edge intersection
// // for (int i = 0; i < edges.length - 1; i++) {
// // for (int j = 0; j < rectCorners.length; j++) {
// // final int next = (j + 1) % rectCorners.length;
// // if (_checkIntersection(
// // edges[i],
// // edges[i + 1],
// // rectCorners[j],
// // rectCorners[next],
// // )) {
// // return true;
// // }
// // }
// // }
// //
// // // Check if any rect corner is inside the shape
// // for (final Offset corner in rectCorners) {
// // if (_isPointInPolygon(shape, corner)) {
// // return true;
// // }
// // }
// //
// // return false;
// // }
// //
// // bool _checkIntersection(Offset p1, Offset p2, Offset p3, Offset p4) {
// // // Calculate the intersection of two line segments
// // double s1X;
// // double s1Y;
// // double s2X;
// // double s2Y;
// // s1X = p2.dx - p1.dx;
// // s1Y = p2.dy - p1.dy;
// // s2X = p4.dx - p3.dx;
// // s2Y = p4.dy - p3.dy;
// //
// // double s;
// // double t;
// // s = (-s1Y * (p1.dx - p3.dx) + s1X * (p1.dy - p3.dy)) /
// // (-s2X * s1Y + s1X * s2Y);
// // t = (s2X * (p1.dy - p3.dy) - s2Y * (p1.dx - p3.dx)) /
// // (-s2X * s1Y + s1X * s2Y);
// //
// // return s >= 0 && s <= 1 && t >= 0 && t <= 1;
// // }
// //
// // bool _isPointInPolygon(List<Offset> polygon, Offset point) {
// // // Ray-casting algorithm for checking if a point is inside a polygon
// // bool inside = false;
// // for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
// // if ((polygon[i].dy > point.dy) != (polygon[j].dy > point.dy) &&
// // (point.dx <
// // (polygon[j].dx - polygon[i].dx) *
// // (point.dy - polygon[i].dy) /
// // (polygon[j].dy - polygon[i].dy) +
// // polygon[i].dx)) {
// // inside = !inside;
// // }
// // }
// // return inside;
// // }