An algorithm for calculating the trajectory of elliptical motion

Source: Internet
Author: User
Tags cos sin

In the middle school or university age, we should all learn the elliptic equation, the hyperbolic equation and so on in the mathematics, of course that time study these knowledge the purpose is for the examination, in order to be able to take a good result, last good university and so on. So beyond that, what convenience does this knowledge bring to our future life or work?

Coincidentally, as programmers, especially those who favor the algorithm, often have this demand, such as the effect of an object to do elliptical motion, or to make a circular trajectory effect, or to do a seemingly irregular curve motion, then this uses the elliptic equation, the circular equation and the Bezier curve equation and so on.

In this paper, the application of the elliptic equation is emphatically introduced.

The formula of the standard elliptic equation:

Second, the center point in (H, k), the spindle parallel to the x-axis formula:

Three, common noun concept and oval shape:

Fig. 1 Fig. 2

    • Long axis: The longest segment that can be obtained by connecting two points on the ellipse, corresponding to major axis in Figure 2.
    • Half-length axis: Half of the long axis, corresponding to a in the ellipse formula.
    • Short axis: A straight line-derived chord perpendicular to the long axis, corresponding to minor axis in Figure 2.
    • Half-short axis: half of the short axis, corresponding to B in the ellipse formula.
    • Focus: Each ellipse has two focal points, respectively, on the F1, F2.
    • Near Arch point: Specifies a focus F1, the closest point from the focus F1 to the nearest arch point, which is a in the figure.
    • Far arch point: As far as the F1 focus is concerned, the farthest point from F1 becomes the distant arch point, which is the B in the figure.
    • Centrifugal rate: Used to describe the shape of the orbit, the eccentricity of the ellipse size interval (0, 1), when the centrifugal rate of 0 indicates a circle.

Four, the calculation formula of centrifugal rate:

    • Calculation according to the distance between the near Arch Point and the far arch point:

Where DP represents the distance near the arch Point, Da represents the distance from the far arch point.

    • Calculation according to half focal length and half-length axis:

Where c is the half focal length, and a represents a half-length axis.

    • Calculation based on half-length and half-short axes:

Where a represents a half-length axis, and B represents a half-short axis.

The formula of this ellipse can be obtained by one of the known ellipses, the near-arch point, the eccentricity, and the tendency vector of the ellipse in three-dimensional space.

1. According to the focus and the near Arch Point, get the distance from the near Arch point to the focus DP.

2. The distance from the far arch point to the focal point can be obtained according to the centrifugal rate and the distance of the near arch.

3. According to the near-arch point, the focus and the distance of the far arch point, the coordinate value of the far arch point can be obtained.

4. According to the near-arch point and the far-arch Point, the center point coordinates of the ellipse can be obtained.

5. According to the formula: half-short axis = near the arch point distance * Far arch point distance, you can get the length of the half-length axis B.

6. According to the normal vector N and the direction of the long axis, which represents the ellipse tendency, the unit vectors in the direction of the short axis can be obtained by cross-multiplication, and the half-short axis vector can be obtained according to the length of the half-short axis.

7. Semi-short axis vector, semi-long axis vector, according to the parameter equation of ellipse, can get the coordinate value of the ellipse.

The specific code is as follows:

1 //mdirection represents whether the direction of the point on the ellipse is counterclockwise or clockwise, anglethe angle used to calculate the elliptic parametric equation2 DoubleAngle = (Mdirection = = orbitdirection.clockwise? -1:1) * MAngle * time *mathutil.pre_pi_div_180;3 4 //calculate the distance from the nearest arch point to the focus5 DoublePeriapsisradius =Mperiapsis.distanceto (mfocalpoint);6 //The distance from the far arch Point (the distance from the near Arch point to the far arch point is the long axis of the ellipse) according to the formula of the eccentricity, the distance from the near Arch point to the focus, and the far arch point to the focus .7 DoubleApoapsisradius = Periapsisradius * (1 + meccentricity)/(1-meccentricity);8 9 //calculate the unit vector near the arch point to the focus, noting that the purpose of multiplying by 1e8 and dividing by 1e8 is to remove the least significant number after the 8th decimal point to reduce the computational error. Ten DoubleUAx = (Math.Round (mfocalpoint.x * 1e8)-Math.Round (mperiapsis.x * 1e8))/1e8; One DoubleUay = (Math.Round (MFOCALPOINT.Y * 1e8)-Math.Round (MPERIAPSIS.Y * 1e8))/1e8; A DoubleUAz = (Math.Round (MFOCALPOINT.Z * 1e8)-Math.Round (MPERIAPSIS.Z * 1e8))/1e8; - DoubleMoD = math.sqrt (uAx * uAx + Uay * Uay + uAz * uAz);//calculate the distance from the nearest arch point to the focus - if(mod! = 0 && MoD! = 1) {//Unit of theMoD = 1/MoD; -UAx *=MoD; -Uay *=MoD; -UAz *=MoD; + } -  + Doubleapoapsisdir_x = Math.Round (uAx * Apoapsisradius * 1e8)/1e8; A Doubleapoapsisdir_y = Math.Round (Uay * Apoapsisradius * 1e8)/1e8; at DoubleApoapsisdir_z = Math.Round (uAz * Apoapsisradius * 1e8)/1e8; - //calculate the coordinates of the far arch point - DoubleApoapsispos_x = Math.Round ((apoapsisdir_x + mfocalpoint.x) * 1e8)/1e8; - DoubleApoapsispos_y = Math.Round ((apoapsisdir_y + mfocalpoint.y) * 1e8)/1e8; - DoubleApoapsispos_z = Math.Round ((apoapsisdir_z + mfocalpoint.z) * 1e8)/1e8; -  in //The center of the near Arch Point and the far arch Point is the center of the ellipse - Doublecenter_x = Math.Round (((mperiapsis.x + apoapsispos_x)/2) * 1e8)/1e8; to DoubleCenter_y = Math.Round (((Mperiapsis.y + apoapsispos_y)/2) * 1e8)/1e8; + DoubleCenter_z = Math.Round (((mperiapsis.z + apoapsispos_z)/2) * 1e8)/1e8; -  the //calculate the length of a half-short axis * Doubleb = Math.sqrt (Periapsisradius *Apoapsisradius); $ Panax Notoginseng //Vector from center point to near Arch Point - DoubleSemimajoraxis_x = Math.Round ((mperiapsis.x-center_x) * 1e8)/1e8; the DoubleSemimajoraxis_y = Math.Round ((mperiapsis.y-center_y) * 1e8)/1e8; + DoubleSemimajoraxis_z = Math.Round ((mperiapsis.z-center_z) * 1e8)/1e8; A  the //Vector Semi-long axis of unit + DoubleUnitsemimajoraxis_x =semimajoraxis_x; - DoubleUnitsemimajoraxis_y =semimajoraxis_y; $ DoubleUnitsemimajoraxis_z =semimajoraxis_z; $MoD = math.sqrt (semimajoraxis_x * semimajoraxis_x + semimajoraxis_y * semimajoraxis_y +semimajoraxis_z -*semimajoraxis_z); - if(mod! = 0 && MoD! = 1) { theMoD = 1/MoD; -Unitsemimajoraxis_x *=MoD;WuyiUnitsemimajoraxis_y *=MoD; theUnitsemimajoraxis_z *=MoD; - } Wu  - //Pan The center point along the normal vector direction AboutVector3 Unitnormal =Mnormal.clone (); $ unitnormal.normalize (); - DoubleUNx = Math.Round (unitnormal.x * 1e8)/1e8; - DoubleUNy = Math.Round (UNITNORMAL.Y * 1e8)/1e8; - DoubleUNz = Math.Round (UNITNORMAL.Z * 1e8)/1e8; A Doublenormalcenter_x = center_x +uNx; + DoubleNormalcenter_y = center_y +uNy; the DoubleNormalcenter_z = Center_z +uNz; -MoD = math.sqrt (normalcenter_x * normalcenter_x + normalcenter_y * normalcenter_y +normalcenter_z $*normalcenter_z); the if(mod! = 0 && MoD! = 1) { theMoD = 1/MoD; theNormalcenter_x *=MoD; theNormalcenter_y *=MoD; -Normalcenter_z *=MoD; in } the  the Mscratch1.setall (unitsemimajoraxis_x, unitsemimajoraxis_y, unitsemimajoraxis_z); About Mscratch2.setall (normalcenter_x, normalcenter_y, normalcenter_z); the //a unit vector for calculating half-short axes by cross-multiplication theVector3 Semiminoraxis =Mscratch3.crossandset (MScratch1, MSCRATCH2); the //get the half-short axis vector + semiminoraxis.multiply (b); -  the //parametric equation of 3D space ellipseBayi Doublex = center_x + (math.cos (angle) * semimajoraxis_x) + (Math.sin (angle) *semiminoraxis.x); the Doubley = center_y + (math.cos (angle) * semimajoraxis_y) + (Math.sin (angle) *semiminoraxis.y); the Doublez = center_z + (math.cos (angle) * semimajoraxis_z) + (Math.sin (angle) * semiminoraxis.z);

An algorithm for calculating the trajectory of elliptical motion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.