Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slowkow committed Sep 28, 2017
1 parent fd15d0a commit 28633db
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/repel_boxes.cpp
Expand Up @@ -34,35 +34,56 @@ NumericVector centroid(NumericVector b) {
NumericVector intersect_line_rectangle(
NumericVector p1, NumericVector p2, NumericVector b
) {
double slope = (p2[1] - p1[1]) / std::max(p2[0] - p1[0], 0.0004);
// Sorry for the ugly code :(
double dy = p2[1] - p1[1];
double dx = p2[0] - p1[0];

double slope = dy / dx;
double intercept = p2[1] - p2[0] * slope;

NumericMatrix retval(4, 2);
std::fill(retval.begin(), retval.end(), -INFINITY);

double x, y;

x = b[0];
y = slope * x + intercept;
if (b[1] <= y && y <= b[3]) {
retval(0, _) = NumericVector::create(x, y);
}
// +----------+ < b[3]
// | |
// | | < y
// | |
// +----------+ < b[1]
// ^ ^ ^
// b[0] x b[2]

if (dx != 0) {
// Left boundary
x = b[0];
y = dy == 0 ? p1[1] : slope * x + intercept;
if (b[1] <= y && y <= b[3]) {
retval(0, _) = NumericVector::create(x, y);
}

x = b[2];
y = slope * x + intercept;
if (b[1] <= y && y <= b[3]) {
retval(1, _) = NumericVector::create(x, y);
// Right boundary
x = b[2];
y = dy == 0 ? p1[1] : slope * x + intercept;
if (b[1] <= y && y <= b[3]) {
retval(1, _) = NumericVector::create(x, y);
}
}

y = b[1];
x = (y - intercept) / slope;
if (b[0] <= x && x <= b[2]) {
retval(2, _) = NumericVector::create(x, y);
}
if (dy != 0) {
// Bottom boundary
y = b[1];
x = dx == 0 ? p1[0] : (y - intercept) / slope;
if (b[0] <= x && x <= b[2]) {
retval(2, _) = NumericVector::create(x, y);
}

y = b[3];
x = (y - intercept) / slope;
if (b[0] <= x && x <= b[2]) {
retval(3, _) = NumericVector::create(x, y);
// Top boundary
y = b[3];
x = dx == 0 ? p1[0] : (y - intercept) / slope;
if (b[0] <= x && x <= b[2]) {
retval(3, _) = NumericVector::create(x, y);
}
}

int i = 0;
Expand Down

0 comments on commit 28633db

Please sign in to comment.