Skip to content

Commit 28633db

Browse files
committed
fix line rectangle intersect bug from 0.6.8
Lines connecting text to points were offset a bit by this change, introduced in version 0.6.8: 0.6.5...0.6.8#diff-0082409048be93e44f7f20c66f882f4dR37 0.6.5 is the official CRAN version as of 2017-09.28 You can see the visual difference between 0.6.5 and 0.6.8 here: (Click "Onion Skin" to get a better view) 0.6.5...0.6.8#diff-6ae21d1d4fd3a41a2aa03de9cb9eed7c I don't like the way it looks. Notice that 0.6.8 introduces a greater number of unneeded lines, cluttering the display.
1 parent fd15d0a commit 28633db

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

Diff for: src/repel_boxes.cpp

+40-19
Original file line numberDiff line numberDiff line change
@@ -34,35 +34,56 @@ NumericVector centroid(NumericVector b) {
3434
NumericVector intersect_line_rectangle(
3535
NumericVector p1, NumericVector p2, NumericVector b
3636
) {
37-
double slope = (p2[1] - p1[1]) / std::max(p2[0] - p1[0], 0.0004);
37+
// Sorry for the ugly code :(
38+
double dy = p2[1] - p1[1];
39+
double dx = p2[0] - p1[0];
40+
41+
double slope = dy / dx;
3842
double intercept = p2[1] - p2[0] * slope;
43+
3944
NumericMatrix retval(4, 2);
4045
std::fill(retval.begin(), retval.end(), -INFINITY);
4146

4247
double x, y;
4348

44-
x = b[0];
45-
y = slope * x + intercept;
46-
if (b[1] <= y && y <= b[3]) {
47-
retval(0, _) = NumericVector::create(x, y);
48-
}
49+
// +----------+ < b[3]
50+
// | |
51+
// | | < y
52+
// | |
53+
// +----------+ < b[1]
54+
// ^ ^ ^
55+
// b[0] x b[2]
56+
57+
if (dx != 0) {
58+
// Left boundary
59+
x = b[0];
60+
y = dy == 0 ? p1[1] : slope * x + intercept;
61+
if (b[1] <= y && y <= b[3]) {
62+
retval(0, _) = NumericVector::create(x, y);
63+
}
4964

50-
x = b[2];
51-
y = slope * x + intercept;
52-
if (b[1] <= y && y <= b[3]) {
53-
retval(1, _) = NumericVector::create(x, y);
65+
// Right boundary
66+
x = b[2];
67+
y = dy == 0 ? p1[1] : slope * x + intercept;
68+
if (b[1] <= y && y <= b[3]) {
69+
retval(1, _) = NumericVector::create(x, y);
70+
}
5471
}
5572

56-
y = b[1];
57-
x = (y - intercept) / slope;
58-
if (b[0] <= x && x <= b[2]) {
59-
retval(2, _) = NumericVector::create(x, y);
60-
}
73+
if (dy != 0) {
74+
// Bottom boundary
75+
y = b[1];
76+
x = dx == 0 ? p1[0] : (y - intercept) / slope;
77+
if (b[0] <= x && x <= b[2]) {
78+
retval(2, _) = NumericVector::create(x, y);
79+
}
6180

62-
y = b[3];
63-
x = (y - intercept) / slope;
64-
if (b[0] <= x && x <= b[2]) {
65-
retval(3, _) = NumericVector::create(x, y);
81+
// Top boundary
82+
y = b[3];
83+
x = dx == 0 ? p1[0] : (y - intercept) / slope;
84+
if (b[0] <= x && x <= b[2]) {
85+
retval(3, _) = NumericVector::create(x, y);
86+
}
6687
}
6788

6889
int i = 0;

0 commit comments

Comments
 (0)