relund / gMOIP

2D and 3D plots of linear/integer programming models in R
https://relund.github.io/gMOIP/
5 stars 2 forks source link

Fix inHull() for horizontal and vertical lines #3

Closed scottkosty closed 3 years ago

scottkosty commented 3 years ago

First, thank you for your package, for developing it on GitHub, and for having a nice suite of tests for it!

This PR fixes handling of horizontal and vertical lines. The previous code would return NA in some cases because of a division by zero. I'm not confident at all that my proposed fix is the most simple or most efficient, but perhaps it will give a clue for a better fix. For example, maybe there's a simple way to handle all cases together, rather than handling vertical and horizontal lines separately from other types of lines (as I do in the commit).

By the way, cool style for entering matrices in the tests. I haven't seen that style before.

Here are a couple of examples for what this PR fixes:

library("gMOIP")

vertices <- matrix(c(1,1, 1,2, 1,3), ncol = 2, byrow = TRUE)
pt <- matrix(c(1,2, 1,3, 1,4), ncol = 2, byrow = TRUE)

res <- inHull(pt, vertices)
res_expected <- c(0,0,-1)

vertices <- matrix(c(1,1, 2,1, 3,1), ncol = 2, byrow = TRUE)
pt <- matrix(c(2,1, 3,1, 4,1), ncol = 2, byrow = TRUE)

res <- inHull(pt, vertices)
res_expected <- c(0,0,-1)
relund commented 3 years ago

Thanks for the fix! Much appreciated.