forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement missing IsInside() (root-project#15074)
* implement missing IsInside() * Add missing `<CR>`s * clang format * More clang format * Final adjustment
- Loading branch information
Showing
7 changed files
with
169 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ | |
#include "TVirtualX.h" | ||
|
||
|
||
const Double_t kPI = 3.14159265358979323846; | ||
constexpr Double_t kPI = TMath::Pi(); | ||
|
||
ClassImp(TEllipse); | ||
|
||
|
@@ -507,6 +507,38 @@ void TEllipse::ExecuteEvent(Int_t event, Int_t px, Int_t py) | |
} | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
/// Return 1 if the point (x,y) is inside the polygon defined by | ||
/// the ellipse 0 otherwise. | ||
/// Author: Ole Hansen ([email protected]) | ||
Int_t TEllipse::IsInside(Double_t x, Double_t y) const | ||
{ | ||
x -= fX1; | ||
y -= fY1; | ||
Double_t th = fTheta * TMath::DegToRad(); | ||
Double_t st = TMath::Sin(th); | ||
Double_t ct = TMath::Cos(th); | ||
Double_t xx = ct * x + st * y; | ||
Double_t yy = -st * x + ct * y; | ||
|
||
if (TMath::Abs(xx) > fR1 || TMath::Abs(yy) > fR2) | ||
return 0; | ||
Double_t xn = xx / fR1; | ||
Double_t yn = yy / fR2; | ||
if (xn * xn + yn * yn > 1.) | ||
return 0; | ||
if (fPhimax - fPhimin >= 360.) | ||
return 1; | ||
Double_t phimin = std::fmod(fPhimin, 360.); | ||
Double_t phimax = std::fmod(fPhimax, 360.); | ||
Double_t phi = TMath::RadToDeg()*(TMath::Pi() + TMath::ATan2(-yy * fR1 / fR2, -xx)); | ||
if (phi < phimin || phi > phimax) | ||
return 0; | ||
|
||
return 1; | ||
} | ||
|
||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
/// List this ellipse with its attributes. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/// \file | ||
/// \ingroup tutorial_graphics | ||
/// \notebook -js | ||
/// Test the IsInside methods of various graphics primitives. | ||
/// | ||
/// \macro_image | ||
/// \macro_code | ||
/// | ||
/// \author Olivier Couet | ||
|
||
void inside() { | ||
auto el = new TEllipse(0.75, 0.25, .2,.15,45,315,62); | ||
el->Draw(); | ||
|
||
auto gr = new TGraph(); | ||
double gr_x1[5] = {0.1, 0.3388252, 0.03796561, 0.4176218, 0.1}; | ||
double gr_y1[5] = {0.5, 0.9644737, 0.7776316, 0.6960526, 0.5}; | ||
gr = new TGraph(5, gr_x1, gr_y1); | ||
gr->Draw("L"); | ||
|
||
auto bx = new TBox(.7, .8, .9, .95); | ||
bx->Draw(); | ||
|
||
auto pv = new TPave(.05, .1, .3, .2); | ||
pv->Draw(); | ||
|
||
auto di = new TDiamond(.05, .25, .3, .4); | ||
di->Draw(); | ||
|
||
auto cr = new TCrown(.5, .5, .1, .15); | ||
cr->SetFillColor(19); | ||
cr->Draw(); | ||
|
||
for (int i = 0; i < 10000; i++) { | ||
double x = gRandom->Rndm(); | ||
double y = gRandom->Rndm(); | ||
auto p = new TMarker(x,y,7); | ||
p->Draw(); | ||
if (el->IsInside(x,y) || bx->IsInside(x,y) || pv->IsInside(x,y) || di->IsInside(x,y) || cr->IsInside(x,y) || | ||
gr->IsInside(x,y)) { | ||
p->SetMarkerColor(kGreen); | ||
} else { | ||
p->SetMarkerColor(kRed); | ||
} | ||
} | ||
} |