Enjoy an ad free experience by logging in. Not a member yet? Register.

Results 1 to 6 of 6
Thread: Ellipse calculation

07072002, 06:10 AM #1
 Join Date
 Jul 2002
 Posts
 31
 Thanks
 0
 Thanked 0 Times in 0 Posts
Ellipse calculation
I need a function (it needn't be in JavaScript) that calculates the distance of a point from the center of an ellipse given:
 The coordinates of the bounding rectangle of the ellipse, and
 The position of a point relative to the topleft corner of the ellipse's bounding rectangle.
The distance must be in units.
07072002, 06:41 AM
#2
 Join Date
 Jun 2002
 Location
 Between DC and Baltimore In a Cave
 Posts
 2,717
 Thanks
 1
 Thanked 94 Times in 88 Posts
LOL....do a search online,,,,,,,,
sounds like you want us to do an assignment......
07072002, 03:26 PM
#3
 Join Date
 Jun 2002
 Posts
 338
 Thanks
 0
 Thanked 0 Times in 0 Posts
Maybe the getBoundingClientRect() method will help?
Hope that helps!
Happy coding!
07072002, 07:32 PM
#4
Well, given you know the rectangle touching the 2 vertices and the 2 covertices:
var v1 = [leftTopX, (leftTopY + leftBottomY) / 2];
var v2 = [rightTopX, (rightTopY + rightBottomY) / 2];
var cv1 = [(leftTopX + rightTopX) / 2, rightTopY];
var cv2 = [(leftBottomX + rightBottomX) / 2, rightBottomY];
Now, this is assuming that a^2 is under x in the equation, otherwise the cv's would be the v's, and vice versa. You can find this out via:
rightTopX  leftTopX > leftTopY  leftBottomY;
If true, the general equation is (xh)^2/a^2 + (yk)^2/b^2, if false b^2 goes under x, and a^2 under y.
From the vertices and covertices, you can generate the equation:
var h = (leftTopX + rightTopX) / 2;
var k = (leftTopY + leftBottomY) / 2;
var a = [v1[0]  h, v2[0]  h];
a = (a[0] > a[1]) ? a[0] : a[1];
var b = [cv1[1]  k, cv2[1]  k];
b = (b[0] > b[1]) ? b[0] : b[1];
var eqn = function(x,y) {
return Math.pow((xh), 2) / Math.pow(a, 2) + Math.pow((yk), 2) / Math.pow(b, 2);
}
Remember to take into account orientation, in which case if the check above proves false, you need to switch the denominators.
Also, this does not take into account possible rotation....
And some of the above might be *slightly* inaccurate, due to fast typing or it being a little while, but I'm pretty sure it seems correct.
07072002, 09:36 PM
#5
 Join Date
 Jul 2002
 Posts
 31
 Thanks
 0
 Thanked 0 Times in 0 Posts
Thanks there.
I have another question: I need a function that calculates the radius of an ellipse at an arbitrary angle (in degrees) given the ellipse's bounding rectangle.
07072002, 09:50 PM
#6
 Join Date
 Jun 2002
 Posts
 185
 Thanks
 0
 Thanked 0 Times in 0 Posts
Actually, you don't need to worry about the ellipse. You've got the bounding rectangle so you just need to (1) find the center of that rectangle (which will be the same as the center of the ellipse) then (2) find the distance from that center point to the given point.
Since the coordinates of the point are given relative to the top, left corner of the bounding rectangle, you can simplify it even more by just using the rectangle width and height.
So the equation looks like:
d = sqrt((x  w/2)^2 + (y  h/2)^2)
where (x,y) is the point and w and h are the width and height of the bounding rectangle.
In JavaScript syntax:
d = Math.sqrt(Math.pow(xw/2, 2) + Math.pow(yh/2, 2));