Three.js Source Note (11) math/box2.js

Source: Internet
Author: User
Tags scalar

Commercial Territory (http://blog.csdn.net/omni360/)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Business Domain-this blog focuses on Agile development and mobile and IoT device research: Data visualization, Golang, HTML5, WEBGL, three. JS, Otherwise, from this blog article refused to reprint or reprint, thank you for your cooperation.


I was just beginning to learn, a lot of things are definitely wrong and please forgive me.

The following code is a comment for the math/box2.js file in the three.js source file.

More updates in: https://github.com/omni360/three.js.sourcecode/blob/master/Three.js


file:src/math/box2.js/** * @author bhouston/http://exocortex.com *//*/// The constructor for the Box2 object. Used to create a two-dimensional rectangular boundary boundary object within a two-dimensional space. The function function of the Box2 object is implemented using a function prototype object defined by///. Note: If there are no parameters min,max the two-dimensional rectangle boundary is initialized to infinity, infinity Usage: var min = new Vector2 (0,0), max = new Vector2 (n); var box = new Box2 (Min,max);///////Create a two-dimensional rectangular boundary object with two Vector2 (two-dimensional vector) Min,max. *////<summary>box2</summary>/// <param name = "min" type= "Vector2" > The minimum coordinate value of the boundary </param>///<param name = "Max" type= "Vector2" > The maximum coordinate value of the boundary </param>three. Box2 = function (min, max) {this.min = (min!== undefined)? min:new three. Vector2 (Infinity, Infinity);//infinity positive Infinity This.max = (max!== undefined)? Max:new three. Vector2 (-Infinity,-Infinity);//-Infinity negative Infinity};/******************************************** The following are the function functions provided by the Box2 object. * * /three. Box2.prototype = {Constructor:three. The box2,//constructor, which returns a reference to the BOX2 function that created this object/*///set method is used to set the starting point, end point, and Min,max coordinate value of the two-dimensional rectangle boundary from the new setting. and returns a two-dimensional rectangular boundary of the newly-coordinate value. *////<summary>set </summary>///&lT;param name = "min" type= "Vector2" > The minimum coordinate value of the boundary </param>///<param name = "Max" type= "Vector2" > The maximum coordinate value of the boundary </ Param>///<returns type= "Box2" > returns the two-dimensional rectangle boundary of the new coordinate value </returns>set:function (min, max) {this.min.copy (min); This.max.copy (max); return this;//returns the two-dimensional rectangle boundary of the new coordinate value},/*///setfrompoints method re-sets the minimum value, maximum value, of the two-dimensional rectangle boundary by a points array of Vector2 objects, The Min,max coordinate value. And returns the two-dimensional rectangle boundary of the new coordinate value. *////<summary>setfrompoints</summary>///<param name = "Points" type= " Vector2array ">vector2 object points array </param>///<returns type=" Box2 "> Returns a two-dimensional rectangular boundary of new coordinate values </returns> Setfrompoints:function (points) {this.makeempty ();//Call the. Makeempty () method to initialize the x, y coordinates of the two-dimensional rectangle boundary Min,max to infinity, infinity for (var i = 0, il = points.length; i < il; i + +) {This.expandbypoint (points[i])//Call the. Expandbypoint () method, get the minimum, maximum coordinates in the points array, and then extend the bounds.} Return this;//returns the two-dimensional rectangle boundary of the new coordinate value},/*///setfromcenterandsize method re-sets the minimum value, Maximum, Min, and size of the two-dimensional rectangle boundary by the center point, boundary Dimension mode. The max coordinate value. And returns a two-dimensional rectangular boundary of the new coordinate value. *////<summary>setfromcenterandsize</summary>///<param name ="Center" type= "Vector2" >vector2 object, center point coordinate </param>///<param name = "Size" type= "number" > Boundary dimension </param >///<returns type= "Box2" > returns the two-dimensional rectangle boundary of the new coordinate value </returns>setfromcenterandsize:function () {var v1 = new three. Vector2 (); Return function (center, size) {var halfsize = v1.copy (size). Multiplyscalar (0.5); This.min.copy (center). S UB (halfsize); this.max.copy (center). Add (halfsize); return this;//returns the two-dimensional rectangle boundary of the new coordinate value}; (), the/*///copy method is used to copy the minimum, maximum, and min,max coordinate values of a two-dimensional rectangular boundary. And returns a two-dimensional rectangular boundary of the new coordinate value .*////<summary>copy</summary>///< param name = "box" type= "Box2" > two-dimensional rectangular boundary </param>///<returns type= "Box2" > Returns a two-dimensional rectangular boundary of new coordinate values </returns> Copy:function (Box) {this.min.copy (box.min); this.max.copy (Box.max); return this;//returns the two-dimensional rectangle boundary of the new coordinate value},/*/// The Makeempty method is used to initialize a two-dimensional rectangular boundary to infinity, Infinity *////<summary>makeempty</summary>///<returns type= "Box2" > Returns the two-dimensional rectangle boundary of the new coordinate value </returns>makeempty:function () {this.min.x = THIS.MIN.Y = infinity;//initializes the two-dimensional rectangle boundary to Infinity, Infinitely large this.max.x = this.max.y =-infinity;//initializes the two-dimensional rectangular boundary to Infinity, and the Infinity return this;//returns the new coordinate value of the two-dimensional rectangular boundary},/*///empty method used to determine the maximum value of the two-dimensional rectangular boundary x, Y is less than the minimum value of x,y.///Note: If this box includes 0 points, return true at its bounds. Note: A box has the largest, smallest boundary, and the maximum minimum boundary is represented by a single point, which is shared by two boundaries.///The Todo:empty method is not understood. When to use. *////<summary>empty</ Summary>///<returns type= "Boolean" > Returns TRUE or False</returns>empty:function () {//This was a more robust CH Eck for empty than (volume <= 0) because volume can get positive with both negative axesreturn (this.max.x < this. min.x) | | (This.max.y < THIS.MIN.Y);},/*///center method is used to return the midpoint of a two-dimensional rectangular boundary *////<summary>makeempty</summary>///< param name = "Optionaltarget" type= "Vector2" > Optional parameters, receive return result, midpoint of boundary </param>///<returns type= "Vector2" > Returns the midpoint of a two-dimensional rectangular boundary </returns>center:function (optionaltarget) {var result = Optionaltarget | | new three. Vector2 (); return result.addvectors (This.min, This.max). Multiplyscalar (0.5);//Returns the midpoint of the two-dimensional rectangular boundary},/*/// The center method is used to return a vector of two-dimensional rectangular boundary dimensions *////<summary>makeempty</summarY>///<param name = "Optionaltarget" type= "Vector2" > Optional parameters, receive return results, vector </param>///<returns type= "Boundary dimensions" Vector2 "> returns the vector </returns>size:function of the two-dimensional rectangular boundary dimension (optionaltarget) {var result = Optionaltarget | | new three. Vector2 (); return result.subvectors (This.max, this.min);//Returns the vector},/*///expandbypoint method of a two-dimensional rectangular boundary dimension via the Vector2 object (point parameter) Expands the minimum, maximum, and min,max coordinate values of a two-dimensional rectangular boundary. And returns a two-dimensional rectangular boundary of the new coordinate value.///Note:expandbypoint method and Expandbyvector method both pass a Vector2 object, The Expandbypoint method compares the maximum value of the current boundary with the x, Y coordinates of the minimum value to obtain a new boundary, but the Expandbyvector method adds the maximum value of the two-dimensional rectangle boundary to the parameter vector, the minimum value minus the parameter vector,*////< Summary>expandbypoint</summary>///<param name = "Points" type= "Vector2" >vector2 object </param>/// <returns type= "Box2" > returns the two-dimensional rectangular boundary </returns>expandbypoint:function (point) of the new coordinate value; This.max.max (point); return this;//returns a two-dimensional rectangular boundary},/*///expandbyvector method with a new coordinate value extending the minimum, maximum, Min of the two-dimensional rectangular boundary through the Vector2 object (vector parameter) The max coordinate value. And returns a two-dimensional rectangular boundary of the new coordinate value.///The Note:expandbyvector method and the Expandbypoint method both pass a Vector2 object, the Expandbypoint method will the maximum value of the current boundary, the minimum value of x,Y-coordinate to obtain a new boundary, but the Expandbyvector method adds the maximum value of the two-dimensional rectangle boundary to the parameter vector, the minimum value minus the parameter vector,///Note: The Expandbyvector method differs from the Expandbyscalar method in that Expandbyvector () receives a vector, and the Expandbyscalar () method receives a scalar .*////<summary> Expandbyvector</summary>///<param name = "Vector" type= "Vector2" >vector2 object </param>///<returns Type= "Box2" > returns the new coordinate value of the two-dimensional rectangular boundary </returns>expandbyvector:function (vector) {this.min.sub (vector); This.max.add ( vector); return this;//returns a two-dimensional rectangular boundary},/*///expandbyvector method with a new coordinate value by extending the minimum, maximum, Min, and of the two-dimensional rectangular boundary through the Vector2 object (vector parameter) The max coordinate value. and returns the two-dimensional rectangle boundary of the new coordinate value.///Note:expandbyvector method differs from Expandbyscalar method in that Expandbyvector () receives a vector, expandbyscalar () method receives a scalar. *////<summary>expandbyvector</summary>///<param name = "scalar" type= "number" > Numeric object </ Param>///<returns type= "Box2" > returns the two-dimensional rectangle boundary of the new coordinate value </returns>expandbyscalar:function (scalar) { This.min.addScalar (-scalar); this.max.addScalar (scalar); return this;//returns the two-dimensional rectangle boundary of the new coordinate value},/*/// The Containspoint method is used to get the parameter point (a Vector2 two-dimensional dot coordinate) within the current two-dimensional rectangular boundary. *////<sUmmary>containspoint</summary>///<param name = "point" type= "Vector2" > a Vector2 two-dimensional dot coordinate </param> <returns type= "Boolean" > Returns True or False</returns>containspoint:function (point) {if (Point.x < this. min.x | |     Point.x > this.max.x | | Point.y < THIS.MIN.Y | | Point.y > This.max.y) {return false;//not within the bounds, return False}return true;//within the bounds, return True},/*///containsbox method to get the parameter box ( A Box2 two-dimensional rectangular boundary) is within the current two-dimensional rectangular boundary. *////<summary>containsbox</summary>///<param name = "box" type= "Box2" > A Box2 two-dimensional rectangular boundary </param>///<returns type= "Boolean" > Returns TRUE or False</returns>containsbox: function (box) {if (this.min.x <= box.min.x) && (box.max.x <= this.max.x) && (this.min. Y <= box.min.y) && (box.max.y <= this.max.y) {return true;//within the bounds, return True}return false;//not within the bounds, return false},/ The *///getparameter method is used to obtain the height-to-width ratio of the parameter point (a Vector2 two-dimensional dot coordinate) at the current two-dimensional rectangular boundary.///Example: var point = new Vector2 (3,2);//var min= new Vector2 (a), max = new Vector2 (5,5);///var box = new Box2 (Min,max); A 4x4 border///var ot = new Vector2 ();////Box.getparameter (Point,ot);///Ot= 3/4,1/2*////<summary>containsbox< /summary>///<param name = "box" type= "Box2" > a Box2 two-dimensional rectangular boundary </param>///<param name = "Optionaltarget" Type= "Vector2" > Optional parameters, receive return results, two-dimensional vector with aspect ratio </param>///<returns type= "Vector2" > returns two-dimensional vector with aspect ratio. </returns >getparameter:function (Point, Optionaltarget) {//This can potentially had a divide by zero if the box//had a siz E Dimension of 0.//Note: There may be a divisor of 0.var result = Optionaltarget | | New three.  Vector2 (); return Result.set ((point.x-this.min.x)/(this.max.x-this.min.x), (POINT.Y-THIS.MIN.Y)/(this.max.y -THIS.MIN.Y));//Returns a two-dimensional vector},/*///containsbox method that contains the aspect ratio to obtain a parameter box (a Box2 two-dimensional rectangular boundary) that intersects with the current two-dimensional rectangular boundary .*////<summary> Containsbox</summary>///<param name = "box" type= "Box2" > a Box2 two-dimensional rectangular boundary </param>///<returns type= "Boolean" > returns TRUE or False</returns>isintersectIonbox:function (box) {//using 6 splitting planes to rule out Intersections.if (box.max.x < this.min.x | | box.min.     X > this.max.x | | Box.max.y < THIS.MIN.Y | | BOX.MIN.Y > This.max.y) {return false;//if not intersected, return False}return true;//if intersect, return true.},/*/// The Clamppoint method is used to restrict the parameter point to a two-dimensional rectangular boundary. If point is less than Min, return min, or return max if it is greater than Max, point*////<summary>clamppoint</ Summary>///<param name = "point" type= "Vector2" > a Vector2 two-dimensional dot coordinate </param>///<param name = " Optionaltarget "type=" Vector2 "> Optional parameters, receive returned results, return trimmed boundary points </param>///<returns type=" Vector2 "> Return trimmed boundary points. </returns>clamppoint:function (Point, Optionaltarget) {var result = Optionaltarget | | new three. Vector2 (); return result.copy (point). Clamp (this.min, This.max);//Returns the trimmed boundary points},/*///distancetopoint method used to obtain a point to the minimum boundary within the boundary, The length of the maximum boundary (the length of box's four sides). *////<summary>distancetopoint</summary>///<param name = "point" type= "Vector2" > The two-dimensional point coordinate of a Vector2 within a boundary </param>///<returns type= "number" > returns the boundary within aPoint to the minimum boundary, the length of the maximum boundary (the length of box's four sides). </returns>distancetopoint:function () {var v1 = new three. Vector2 (); return function (point) {var clampedpoint = v1.copy (point). Clamp (this.min, This.max); return Clampedpoint.s UB (point) Length ();//returns the minimum boundary within the boundary, the length of the maximum boundary (the length of box's four sides).};} (), the/*///intersect method is used to find the intersection of the current two-dimensional rectangular boundary and the parameter box by shrinking the current two-dimensional rectangular boundary. *////<summary>intersect</summary>///<param name = "box" type= "Box2" > a Box2 two-dimensional rectangular boundary </param>///<returns type= "Boolean" > returns the intersection of the current two-dimensional rectangle boundary and the parameter box </ Returns>intersect:function (Box) {This.min.max (box.min); This.max.min (Box.max); return this;// Returns the intersection of the current two-dimensional rectangle boundary and the parameter box},/*///intersect method is used to enclose the parameter box in the current two-dimensional rectangular boundary by extending the current two-dimensional rectangular boundary. is to take the two boundary of the set *////<summary> Intersect</summary>///<param name = "box" type= "Box2" > a Box2 two-dimensional rectangular boundary </param>///<returns type= " Boolean > returns the </returns>union:function of two boundaries (box) {this.min.min (box.min); This.max.max (Box.max); return this;//returns the two-boundary},/*///translate method used to move the position of the current two-dimensional rectangular boundary through the parameter offset. *////<sUmmary>translate</summary>///<param name = "offset" type= "Vector2" > Offset </param>///<returns Type= "Boolean" > returns the new coordinate value of the two-dimensional rectangular boundary </returns>translate:function (offset) {this.min.add (offset); This.max.add ( Offset) Return this;//returns a two-dimensional rectangular boundary},/*///equals method for returning a new coordinate value to get the parameter box (a Box2 two-dimensional rectangular boundary) that is exactly equal to the current two-dimensional rectangular boundary .*////<summary> Equals</summary>///<param name = "box" type= "Box2" > a Box2 two-dimensional rectangular boundary </param>///<returns type= " Boolean "> Returns True or false</returns>equals:function (box) {return box.min.equals (this.min) && Box.max. Equals (This.max);//returns True or False},/*clone method///clone method clones a two-dimensional rectangular bounding object. *////<summary>clone</summary>/// <returns type= "Box2" > returns the two-dimensional rectangle boundary Object </returns>clone:function () {return new three. Box2 (). copy (this);//return two-dimensional rectangle boundary object}};


Commercial Territory (http://blog.csdn.net/omni360/)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Business Domain-this blog focuses on Agile development and mobile and IoT device research: Data visualization, Golang, HTML5, WEBGL, three. JS, Otherwise, from this blog article refused to reprint or reprint, thank you for your cooperation.


The following code is a comment for the math/box2.js file in the three.js source file.

More updates in: https://github.com/omni360/three.js.sourcecode/blob/master/Three.js

Three.js Source Note (11) math/box2.js

Related Article

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.