Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'source/blender/blenlib/intern/rct.c')
-rw-r--r--source/blender/blenlib/intern/rct.c57
1 files changed, 52 insertions, 5 deletions
diff --git a/source/blender/blenlib/intern/rct.c b/source/blender/blenlib/intern/rct.c
index cab383b60f3..ee073d5d309 100644
--- a/source/blender/blenlib/intern/rct.c
+++ b/source/blender/blenlib/intern/rct.c
@@ -102,16 +102,28 @@ int BLI_rctf_isect_pt_v(const rctf *rect, const float xy[2])
}
/* based closely on 'isect_line_line_v2_int', but in modified so corner cases are treated as intersections */
-static int isect_segments(const int v1[2], const int v2[2], const int v3[2], const int v4[2])
+static int isect_segments_i(const int v1[2], const int v2[2], const int v3[2], const int v4[2])
{
const double div = (double)((v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]));
- if (div == 0.0f) {
+ if (div == 0.0) {
return 1; /* co-linear */
}
else {
const double labda = (double)((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
const double mu = (double)((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
- return (labda >= 0.0f && labda <= 1.0f && mu >= 0.0f && mu <= 1.0f);
+ return (labda >= 0.0 && labda <= 1.0 && mu >= 0.0 && mu <= 1.0);
+ }
+}
+static int isect_segments_fl(const float v1[2], const float v2[2], const float v3[2], const float v4[2])
+{
+ const double div = (double)((v2[0] - v1[0]) * (v4[1] - v3[1]) - (v2[1] - v1[1]) * (v4[0] - v3[0]));
+ if (div == 0.0) {
+ return 1; /* co-linear */
+ }
+ else {
+ const double labda = (double)((v1[1] - v3[1]) * (v4[0] - v3[0]) - (v1[0] - v3[0]) * (v4[1] - v3[1])) / div;
+ const double mu = (double)((v1[1] - v3[1]) * (v2[0] - v1[0]) - (v1[0] - v3[0]) * (v2[1] - v1[1])) / div;
+ return (labda >= 0.0 && labda <= 1.0 && mu >= 0.0 && mu <= 1.0);
}
}
@@ -134,14 +146,49 @@ int BLI_rcti_isect_segment(const rcti *rect, const int s1[2], const int s2[2])
/* diagonal: [/] */
tvec1[0] = rect->xmin; tvec1[1] = rect->ymin;
tvec2[0] = rect->xmin; tvec2[1] = rect->ymax;
- if (isect_segments(s1, s2, tvec1, tvec2)) {
+ if (isect_segments_i(s1, s2, tvec1, tvec2)) {
+ return 1;
+ }
+
+ /* diagonal: [\] */
+ tvec1[0] = rect->xmin; tvec1[1] = rect->ymax;
+ tvec2[0] = rect->xmax; tvec2[1] = rect->ymin;
+ if (isect_segments_i(s1, s2, tvec1, tvec2)) {
+ return 1;
+ }
+
+ /* no intersection */
+ return 0;
+ }
+}
+
+int BLI_rctf_isect_segment(const rctf *rect, const float s1[2], const float s2[2])
+{
+ /* first do outside-bounds check for both points of the segment */
+ if (s1[0] < rect->xmin && s2[0] < rect->xmin) return 0;
+ if (s1[0] > rect->xmax && s2[0] > rect->xmax) return 0;
+ if (s1[1] < rect->ymin && s2[1] < rect->ymin) return 0;
+ if (s1[1] > rect->ymax && s2[1] > rect->ymax) return 0;
+
+ /* if either points intersect then we definetly intersect */
+ if (BLI_rctf_isect_pt_v(rect, s1) || BLI_rctf_isect_pt_v(rect, s2)) {
+ return 1;
+ }
+ else {
+ /* both points are outside but may insersect the rect */
+ float tvec1[2];
+ float tvec2[2];
+ /* diagonal: [/] */
+ tvec1[0] = rect->xmin; tvec1[1] = rect->ymin;
+ tvec2[0] = rect->xmin; tvec2[1] = rect->ymax;
+ if (isect_segments_fl(s1, s2, tvec1, tvec2)) {
return 1;
}
/* diagonal: [\] */
tvec1[0] = rect->xmin; tvec1[1] = rect->ymax;
tvec2[0] = rect->xmax; tvec2[1] = rect->ymin;
- if (isect_segments(s1, s2, tvec1, tvec2)) {
+ if (isect_segments_fl(s1, s2, tvec1, tvec2)) {
return 1;
}