# Finding roads inside of a rotated ellipse

## Recommended Posts

Hello,

I am trying to find all the roads inside of rotated ellipse. The ellipse comes from a map marker that is created by the user.

I have tried implementing the answer from here to determine if a point is in an ellipse.

```
_u = cos(_angle) * (_ellipseX - _pointX) + sin(_angle) * (_ellipseY - _pointY);
_v = -sin(_angle) * (_ellipseX - _pointX) + cos(_angle) * (_ellipseY - _pointY);

_dSqr = (_u/_sizeA)*(_u/_sizeA) + (_v/_sizeB)*(_v/_sizeB);
_dSqr <= 1;
```

However, when I try this, it only works when the angle is 0, 90, 180, or 270.

Any other angle produces what I can only describe as a scissor effect. Here is a series of pictures from 0 to 90 degrees, increasing the angle by 15 degrees.

The green dots are the roads that the code is returning as "in the ellipse".

0 Degrees

15 Degrees

30 Degrees

45 Degrees

60 Degrees

75 Degrees

90 Degrees

I also tried implementing the answer from here, and got the same results

```_left = (cos(_angle)*(_pointX - _ellipseX) + sin(_angle) * (_pointY - _ellipseY))/_sizeA;
_right = (sin(_angle)*(_pointX - _ellipseX) - cos(_angle) * (_pointY - _ellipseY))/_sizeB;

_left*_left + _right*_right <= 1;
```

Does anybody know what is wrong with the math? The angle and ellipse size are taken from the marker using markerDir and getMarkerSize

##### Share on other sites

Hi,

it seems that your ellipse is mirrored. What happens if you put negative value to the angle? Eg.

_angle = _angle * (-1)

regards.VÃ¤inÃ¤mÃ¶inen

##### Share on other sites

That fixes the problem!

I guess it is because as you increase your angle when using North/South/East/West, you rotate clockwise. However, when working with a unit circle, you rotate counter clockwise.