Cocos2d-x mathematical functions, common macro sorting (1)

Source: Internet
Author: User

Mathematical cocos2d-x in the use of the most mathematical type is CCPoint, a point, essentially a vector, there are a lot of mathematical operations between the vector and the vector to do, oh I know what to do, maybe I know how to calculate a value, but I don't know how to calculate efficiency (or I don't know). How can I be lazy? Of course. This is actually not a lazy standard, because there are some ways to write too much and it may be a little bit of trouble, so naturally cocos2d provides a set of ccp series to help us do a lot of work, it also shows that library Programmers take care of the laziness spirit of development programmers (of course they use it themselves, and they are also very lazy ). First, we will create the vector [cpp] ccp (x, y); // we all know that we use coordinates x and y to create a vector. CcpFromSize (s); // use the width of size s as x and height as y to create a basic addition, subtraction, negative removal, and multiplication of vectors [cpp] ccpAdd (v1, v2); // equivalent ccp (v1.x + v2.x, v1.y + v2.y); ccpSub (v1, v2); // equivalent ccp (v1.x-v2.x, v1.y-v2.y ); ccpNeg (v) // equivalent ccp (-v. x,-v. y); ccpMult (v, s); // equivalent ccp (v. x * s, v. y * s); s is a floating point number. Take the midpoint: Originally a ccpMult (ccpAdd (v1, v2), 0.5f), the developer said no, I just want to play a few words, okay, the library programmer gave a method [cpp] ccpMidpoint (v1, v2); // equivalent ccp (v1.x + v2.x)/2, (v1.y + v2.y)/2); point Multiplication, Cross multiplication, projection [cpp] ccpDot (v1, v2); // equivalent v1.x * v2.x + v1.y * v2.y; ccpCross (v1, v2 ); // equivalent v1.x * v2.y-v1.y * v2.x; ccpProject (v1, v2) // return the length, distance, and Square Value of the projection vector v1 on vector v2, because the efficiency is much higher than the developer's step, it is not just a programmer's laziness, too lazy to be efficient) [cpp] ccpLength (v) // return vector v length, that is, the distance from vertex v to the origin ccpLengthSQ (v) // returns the square of the length of vector v, that is, the square of the distance from vertex v to the origin ccpDistance (v1, v2) // returns the square ccpNormali from point v1 to point v2 to ccpDistanceSQ (v1, v2) // returns the square ccpNormali from point v1 to point v2. Ze (v) // returns the standardized vector of v, that is, the length is 1 rotation, 90 degrees clockwise, 90 degrees clockwise (90 degrees efficiency of course is faster... Similarly, it is too inefficient.) [cpp] ccpRotate (v1, v2); // vector v1 rotates the angle of vector v2 and multiplied by the length of vector v2. When v2 is a standard vector with a length of 1, it is normal rotation. It can be used together with ccpForAngle ccpPerp (v); // equivalent to ccp (-v. y, v. x); (because the opengl coordinate system is the origin in the lower left corner, the vector v is rotated 90 degrees counterclockwise.) ccpRPerp (v); // equivalent to ccp (v. y,-v. x); clockwise rotation of 90 degrees above ccpRotate, supporting vector and radian conversion vector, and some angle-related [cpp] ccpForAngle (); // return a standard vector ccpToAngle (v) with an angle of radians a; // return the radians ccpAngle (a, B) of the vector v; // return, radian value of B vector indicating the difference of angle ccpRotateByAngle (v, degrees, angle) // return vector v uses degrees as the rotation axis point, and rotates angle radians in a counter-clockwise direction without column-wide mathematical methods, for more information, see the header file CCPointExte. Nsion. h. Basically, all of them are available. Of course, mathematics is not just a vector, but there are some other ...... These are also frequently used. A little lazy. [Cpp] CC_RADIANS_TO_DEGREES (a); // radian conversion angle CC_DEGREES_TO_RADIANS (a); // converts degrees to radians CCRANDOM_0_1 (); // generate a random floating point number between 0 and 1: CCRANDOM_MINUS1_1 (); // generate a random floating point number between-1 and 1.

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.