Discuss and collaborate on GML scripts

You are not logged in.

- Topics: Active | Unanswered

Pages: **1**

```
//point_in_ellipse(point_x, point_y, x1, y1, x2, y2);
var x1, y1, x2, y2, cenx, ceny, major, minor, foc1, foc2, R1, R2;
x1 = min(argument2, argument4); y1 = min(argument3, argument5);
x2 = max(argument2, argument4); y2 = max(argument3, argument5);
cenx = x1 +(x2-x1)/2;
ceny = y1 +(y2-y1)/2;
major = max(x2-x1, y2-y1);
minor = min(x2-x1, y2-y1);
if(major==(x2-x1)) {
foc1 = cenx -sqrt(sqr(major/2) - sqr(minor/2));
foc2 = cenx +sqrt(sqr(major/2) - sqr(minor/2));
R1 = point_distance(argument0, argument1, foc1, ceny);
R2 = point_distance(argument0, argument1, foc2, ceny);
} else {
foc1 = ceny -sqrt(sqr(major/2) - sqr(minor/2));
foc2 = ceny +sqrt(sqr(major/2) - sqr(minor/2));
R1 = point_distance(argument0, argument1, cenx, foc1);
R2 = point_distance(argument0, argument1, cenx, foc2);
}
return(R1+R2 <= major);
```

I'm sure there are plenty of things that can be made more efficient, but it still works, so there.

*Last edited by brod (2010-01-09 15:51:39)*

Offline

**xot****Administrator**- Registered: 2007-08-18
- Posts: 1,240

This is a great idea for a script. It could be done a lot simpler, though, if the ellipse is axis-aligned (which it is).

`return sqr((2*argument0-(argument4+argument2))/(argument4-argument2)) + sqr((2*argument1-(argument5+argument3))/(argument5-argument3)) < 1;`

The problem with that, besides being hard to read, is that it will fail if (x1 == x2) or (y1 == y2) because of a divide by zero. Even with checks it's still far fewer operations.

```
// point_in_ellipse(point_x, point_y, x1, y1, x2, y2);
{
if ((x1==x2) || (y1==y2)) return false;
return sqr((2*argument0-(argument4+argument2))/(argument4-argument2)) + sqr((2*argument1-(argument5+argument3))/(argument5-argument3)) < 1;
}
```

I think that's something like 17 operations compared to your 48. I couldn't find a way to simplify it more but I'm not a math whiz.

**edit:** Hey, I just noticed you are from NY. I lived on Long Island for 10 years of my yoot. I miss it but not right now. It's snowing here in Florida, I'm not sure I could handle NY anymore. Winters suck.

*Last edited by xot (2010-01-09 17:26:54)*

*Abusing forum power since 1986.*

Offline

Wow impressive stuff, although what the script is doing is way over my head >_<. Thanks a lot, though!

Yeah I'm from long island too, I'll probably be moving out to college next year, but then again I'm not sure which college I plan on attending. All "iffy" stuff.

*Last edited by brod (2010-01-09 17:35:40)*

Offline

**xot****Administrator**- Registered: 2007-08-18
- Posts: 1,240

Well, it's over my head too. I read this, wrote a script based on it, and then tried to optimize it as much as possible ... which completely destroyed its normal elegance.

[tex]\frac{x^2}{a^2}+\frac{y^2}{b^2} < 1[/tex]

What's good about yours is that it could be adapted for ellipses of arbitrary position and orientation.

*Abusing forum power since 1986.*

Offline

**xot****Administrator**- Registered: 2007-08-18
- Posts: 1,240

There is some interesting discussion about ellipses in this blog post by Yourself.

http://blog.ultimatepronoun.com/?p=11#more-11

Way, way, way, way over my head.

*Abusing forum power since 1986.*

Offline

**Marchal_Mig12****Member**- Registered: 2009-05-21
- Posts: 75

xot wrote:

edit:Hey, I just noticed you are from NY. I lived on Long Island for 10 years of my yoot. I miss it but not right now. It's snowing here in Florida, I'm not sure I could handle NY anymore. Winters suck.

Yeah New York's winter suck. I went there for 2-3 days during winter and it really sucked because it was enough cold to snow and enough hot to... suck heh. I think if you really want to enjoy winter you should come to Quebec someday .

Offline

**paul23****Member**- Registered: 2007-10-17
- Posts: 110

Well xot: one could also add the inverse translation to the point first (asume the circle is the origin, and it's axis-aligned to the x' and y' axis: then calculate the x' and y' coordinate of the point and use the simple formula above. - I used a similar method for describing how much "percent" of a cell is filled by an ellipse...

Offline

**xot****Administrator**- Registered: 2007-08-18
- Posts: 1,240

It already does that, unless you meant inverse *transform* so it could handle rotations as well. I would agree that that is probably best way to handle arbitrary ellipses.

*Abusing forum power since 1986.*

Offline

Pages: **1**