Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions math/mathcore/inc/TMath.h
Original file line number Diff line number Diff line change
Expand Up @@ -1330,8 +1330,22 @@ template <typename T> Bool_t TMath::IsInside(T xp, T yp, Int_t np, T *x, T *y)

for (i=0; i<np; i++) {
if ((y[i]<yp && y[j]>=yp) || (y[j]<yp && y[i]>=yp)) {
if (x[i]+(yp-y[i])/(y[j]-y[i])*(x[j]-x[i])<xp) {
oddNodes = !oddNodes;
if constexpr (std::numeric_limits<T>::is_integer) {
const Long64_t dx = static_cast<Long64_t>(x[j]) - x[i];
const Long64_t dy = static_cast<Long64_t>(y[j]) - y[i];
const Long64_t py = static_cast<Long64_t>(yp) - y[i];
const Long64_t px = static_cast<Long64_t>(xp) - x[i];

const Long64_t lhs = py * dx;
const Long64_t rhs = px * dy;

if ((dy > 0 && lhs < rhs) || (dy < 0 && lhs > rhs)) {
oddNodes = !oddNodes;
}
} else {
if (x[i]+(yp-y[i])/(y[j]-y[i])*(x[j]-x[i])<xp) {
oddNodes = !oddNodes;
}
}
}
j=i;
Expand Down
18 changes: 18 additions & 0 deletions math/mathcore/test/testTMath.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,23 @@ void testPoints(T x, T y)
cout << "Point(" << x << "," << y << ") IsInside?: " << TMath::IsInside(x, y, n, dx, dy) << endl;
}

void testIsInsideInt()
{
const Int_t n = 4;
Int_t x[n] = {299, 41, 299, 558};
Int_t y[n] = {531, 678, 825, 678};

Double_t xd[n] = {299., 41., 299., 558.};
Double_t yd[n] = {531., 678., 825., 678.};

for (Int_t xp = 0; xp <= 600; ++xp) {
for (Int_t yp = 500; yp <= 850; ++yp) {
R__ASSERT(TMath::IsInside(xp, yp, n, x, y) ==
TMath::IsInside(static_cast<Double_t>(xp), static_cast<Double_t>(yp), n, xd, yd));
}
}
}

template <typename T>
void testPlane()
{
Expand Down Expand Up @@ -299,6 +316,7 @@ int main()
testPoints<Double_t>(1.3, 0.5);
testPoints<Float_t>(-0.2, 1.7);
testPoints<Int_t>(1, 1);
testIsInsideInt();

cout << "\nPLane functions tests: " << endl;

Expand Down
Loading