Common mathematical functions in Cocos2d-x

Source: Internet
Author: User

Mathematical functions:

 
 
  1. Ccp (x, y); // create a vector ccpFromSize (s) with coordinates x and y; // create a vector with the width of size s as x and height as y

Addition, subtraction, multiplication, division, and operation of CCPoint

 
 
  1. 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 );
  2. 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.

Midpoint:

 
 
  1. CcpMidpoint (v1, v2); // equivalent ccp (v1.x + v2.x)/2, (v1.y + v2.y)/2 );

Dot multiplication, cross multiplication, and projection:

 
 
  1. CcpDot (v1, v2); // equivalent v1.x * v2.x + v1.y * v2.y; ccpCross (v1, v2); // equivalent v1.x * v2.y-v1.y * v2.x;
  2. CcpProject (v1, v2) // returns the projection vector of vector v1 on vector v2.

Evaluate the length, distance, and their square values:

 
 
  1. CcpLength (v) // return vector v length, that is, the square of the length from vertex v to the origin point ccpLengthSQ (v) // return vector v, that is, the square of the distance from vertex v to the origin point
  2. CcpDistance (v1, v2) // returns the square of the distance from v1 to v2 to ccpDistanceSQ (v1, v2) // returns the square of the distance from v1 to v2.
  3. CcpNormalize (v) // returns the standardized vector of v, that is, the length is 1

Rotate, 90 degrees clockwise, 90 degrees clockwise:

 
 
  1. 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)
  2. CcpRPerp (v); // equivalent to ccp (v. y,-v. x); Rotate 90 degrees clockwise

The supported conversion vectors include vectors and radians. There are also some angle-related vectors:

 
 
  1. CcpForAngle (a); // returns a standard vector ccpToAngle (v) with an angle of radians a; // returns the radians of a vector v.
  2. CcpAngle (a, B); // returns the radian value of the difference between the and B vectors indicating the angle ccpRotateByAngle (v, cosine, angle) // returns the vector v with degrees as the rotation axis point, rotate angle radians counterclockwise

Detection of intersection of line segments:

 
 
  1. CcpLineIntersect (p1, p2, p3, p4, & s, & t ); // returns whether the straight line where p1 is the start point p2 is the end line 1 and p3 is the start point p4 is the end line 2. If yes, parameters s and t return the ratio of the intersection online segment 1 and Line Segment 2. // The ratio of s and t can be obtained through p1 + s * (p2-p1) or p3 + t * (p4-p3.
  2. CcpSegmentIntersect (A, B c, D) // returns the intersection of A line A-B and A line C-D ccpIntersectPoint (A, B, C, D) // returns the intersection of A line A-B and A line C-D

Other useful:

 
 
  1. CC_RADIANS_TO_DEGREES (a); // radian conversion angle CC_DEGREES_TO_RADIANS (a); // converts degrees to radians CCRANDOM_0_1 (); // generates random floating point numbers between 0 and 1.
  2. CCRANDOM_MINUS1_1 (); // generates 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.