This week, I heard other project team colleagues discuss a small demand:
Given three points (in fact, the flight station that passes through the plane, for example, from Pudong-Xi'an-Beijing), a curve is generated on the UI, the road map used to show the plane (in fact, I think it can be done with a straight line. It is just a gesture, but everyone thinks the line is too straight out and not beautiful). I tried it at night:
There are two solutions:
1. the elliptic (soon rejected by myself, the standard equation of the elliptic (X-m) ^ 2/(a ^ 2) + (Y-N) ^ 2/(B ^ 2) = 1, which has four unknown numbers: m, n, A, and B. The three points cannot be uniquely identified. If the center of the center is set to the center of the page, it can be solved theoretically, but square learning is also complicated)
2. besell Curve
Explanation:
The quadratic standard equation is:
I have studied flash before, so I decided to use it. It solves the problem of curve generation and aircraft orientation. The head of the plane has a direction and must conform to the direction of the curve. This can be done by using the "geometric meaning of the curve derivative: the derivative of a certain point in the curve is the slope of the tangent of the point (for another angle, it is actually the Rotation Angle of the plane icon)
After a while, the Code came out:
First define an aircraft entity class (for convenience, temporarily use a small triangle instead)
Package {import flash. display. shape;/*** aircraft entity class * @ author Jimmy. yang */public class plane extends shape {public function plane () {// use a small triangle to simulate the plane graphics. linestyle (1, 0xff0000, 1); graphics. beginfill (0xff0000, 1); graphics. moveTo (-50,-25); graphics. lineto (50, 0); graphics. lineto (-50, 25); graphics. lineto (-50,-25); graphics. endfill ();} public function setangle (Y: Number, X: Number) {This. rotation = math. atan2 (Y, x) * 180/math. pi ;}}}
The following code generates a curve and adjusts the head orientation:
Package {import flash. display. sprite; import flash. events. event; import flash. events. mouseevent; import flash. text. textfield;/*** second Beitz curve to generate aircraft roadmap * @ author Jimmy. yang (Yang Guo yjmyzz@126.com under the http://yjmyzz.cnblogs.com bodhi tree/) */[frame (factoryclass = "preloader")] public class main extends sprite {public function main (): void {If (stage) Init (); else addeventlistener (event. added_to_stage, init);} private function Init (E: event = NULL): void {removeeventlistener (event. added_to_stage, init); testbzcurve ();} private function testbzcurve (): void {var txtp0: textfield = new textfield (); var txtp1: textfield = new textfield (); vaR txtp3: textfield = new textfield (); addchild (txtp0); addchild (txtp1); addchild (txtp3); var p0x: Int = 100; var p0y: Int = 300; txtp0.x = p0X-10; txtp0.y = p0y + 10; txtp0.text = "Pudong (PVG)"; var p1x: Int = 300; var p1y: Int = 250; txtp1.x = p1x; txtp1.y = p1y + 20; txtp1.text = "Xi'an (xiy)"; var P2x: Int = 500; var p2y: Int = 50; txtp3.x = P2x + 5; txtp3.y = p2y; txtp3.text = "Beijing (pek)"; // artificially raise the control point so that the curve passes through the control point p1x = p1x * 2-(p0x + P2x)/2; p1y = p1y * 2-(p1y + p2y)/2; // generate 10 sample points for (var t: Number = 0; t <= 1; t + = 0.1) {// second BZ curve formula var X: Number = (1-t) * (1-t) * p0x + 2 * T * (1-T) * p1x + T * P2x; var y: Number = (1-t) * (1-t) * p0y + 2 * T * (1-T) * p1y + T * p2y; // derivative coordinate of the BZ curve at the t point var FX: Number = 2 * (t-1) * p0x + 2*(1-2 * t) * p1x + 2 * T * P2x; var FY: Number = 2 * (t-1) * p0y + 2*(1-2 * t) * p1y + 2 * T * p2y; // fit the variable var p = new plane (); addchild (P ); p. X = x; p. y = y; p. scalex = 0.2; p. scale= 0.2; p. setangle (FY, FX); // geometric meaning of the derivative} // draw the BZ curve (when the background is used) graphics. linestyle (1, 0x000000, 0.5); graphics. moveTo (p0x, p0y); graphics. curveto (p1x, p1y, P2x, p2y );}}}
No picture, no truth:
Emotion: mathematics is really useful!