# How to find out whether a data point is inside an ellipse (TEllipse)

**URL:** <https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960>\
**Category:** Newbie\
**Created:** [January 25, 2024, 11:00am UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960 "2024-01-25T11:00:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [January 25, 2024, 11:00am UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/1 "2024-01-25T11:00:27Z")

</div>

I use the class TEllipse as 95% contour, generated from a set of points in two dimensions. How can I find out whether a new point is inside the ellipse. I want to use physical coordinates, not pixels on my screen.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 25, 2024, 11:06am UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/2 "2024-01-25T11:06:07Z")

</div>

> [@kiesling](#):
>
> I use the class TEllipse as 95% contour, generated from a set of points

Are you sure you are using TEllipse ? I am asking because TEllipse is not defined by a set of points but by the box enclosing it. Maybe you are using TGraph ?

TGraph has the [IsInside](https://root.cern/doc/master/classTGraph.html#a0a5221e700ae000894667b5a0f4964b1) method which does what you are looking for.

---

<div class="post-metadata">

**Author:** ![Dilicus](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dilicus/32/14338_2.png) [@Dilicus](https://root-forum.cern.ch/u/Dilicus)\
**Post date:** [January 25, 2024, 2:49pm UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/3 "2024-01-25T14:49:59Z")

</div>

Given that you defined an TEllipse like

```auto
TEllipse *el = new TEllipse(x_c, y_c, r_x, r_y);

```

You can verify with just an if statement a new point with coordinates x,y is inside the ellipse.

```auto
if( ( (x - x_c)*(x - x_c)/r_x/r_x + (y - y_c)*(y - y_c)/r_y/r_y)<=1 )
    cout<<"is inside" <<endl;
else
    cout<< "is outside" << endl;

```

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [January 25, 2024, 5:10pm UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/4 "2024-01-25T17:10:08Z")

</div>

Dear Dilicus,

thanks for the formula for the ellipse. I actually hoped that there is a  
method in the TEllipse class to return “Inside” or “Outside”. In  
addition in my case the ellipse is tilted by an angle theta, so that I  
have to use a rotation in addition to the formula you showed. This is  
actually the argument number 7 when calling TEllipse: TEllipse(x\_c,  
y\_c, r\_x, r\_y, phimin, phimax, theta).

So in my case: x → x cos(theta) -y sin(theta) and y-\> x sin(theta) - y  
cos(theta). It would be great if a method (“Inside”) would be integrated  
in the TEllipse class. For the moment I have to do it “the hard way”, as  
you suggest, with my rotations added.

Best

Christian

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 26, 2024, 9:57am UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/5 "2024-01-26T09:57:13Z")

</div>

> [@kiesling](#):
>
> It would be great if a method (“Inside”) would be integrated  
> in the TEllipse class.

Agreed!

Note it is a bit more tricky than just taking into account the tilting, as TEllipse allows to produce this kind of polygon:

![Screenshot 2024-01-26 at 11.00.59](https://root-forum.cern.ch/uploads/default/original/3X/1/a/1a03c0b43c32ffa51c9dea6d8e17431f78bf6d0e.png)

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [January 26, 2024, 10:58am UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/6 "2024-01-26T10:58:04Z")

</div>

Dear Olivier,

indeed, cutting out a sector from the ellipse (arguments 5 = phimin and 6 = phimax) is an additional challenge for the “Inside” method . Nevertheless, I am happy to read that the ROOT team will consider this additional feature for TEllipse.

Best

Christian

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 26, 2024, 11:57am UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/7 "2024-01-26T11:57:08Z")

</div>

I have implemented it:

#### Macro inside.C

```auto
TEllipse *el;

void EPoint(double x, double y) {
   auto p = new TMarker(x,y,20);
   p->Draw();
   if (el->IsInside(x,y)) {
      p->SetMarkerColor(kGreen);
      printf("The point (%g, %g) is inside the ellipse\n",x,y);
   } else {
      p->SetMarkerColor(kRed);
      printf("The point (%g, %g) is outside the ellipse\n",x,y);
   }
}

void inside(){
   el = new TEllipse(0.75, 0.25, .2,.15,45,315,62);
   el->SetLineColor(4);
   el->Draw();
   EPoint(.5,.5);
   EPoint(.7,.3);
   EPoint(.8,.2);
   EPoint(.8,.3);
}

```

 ![Screenshot 2024-01-26 at 12.50.10](https://root-forum.cern.ch/uploads/default/original/3X/f/3/f3ffc9309cdeafe32723dad295b7cbf26b01f23d.jpeg)

But for completeness, I will also implement it for other primitives like TBox etc … I’ll make a PR once I have them.

I made an issue: [Implement IsInside for TEllipse · Issue #14453 · root-project/root · GitHub](https://github.com/root-project/root/issues/14453)

---

<div class="post-metadata">

**Author:** ![kiesling](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/ce7236/32.png) [@kiesling](https://root-forum.cern.ch/u/kiesling)\
**Post date:** [January 26, 2024, 3:34pm UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/8 "2024-01-26T15:34:33Z")

</div>

Dear Olivier,

great, thanks!

I am looking forward to the PR and the new version of ROOT to download via snap!

Cheers

Christian

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 26, 2024, 3:36pm UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/9 "2024-01-26T15:36:56Z")

</div>

The PR is here: [Implement some missing "IsInside" by couet · Pull Request #14456 · root-project/root · GitHub](https://github.com/root-project/root/pull/14456)

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [January 26, 2024, 4:03pm UTC](https://root-forum.cern.ch/t/how-to-find-out-whether-a-data-point-is-inside-an-ellipse-tellipse/57960/10 "2024-01-26T16:03:38Z")

</div>

I added a new tutorial testing IsInside for various classes.

```auto
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);
      }
   }
}

```

 ![Screenshot 2024-01-26 at 17.02.16](https://root-forum.cern.ch/uploads/default/original/3X/d/b/db0d4119797c69e33a276d17161a4212d048a0cb.jpeg)
