Three.js Source Note (14) Math/sphere.js

Source: Internet
Author: User

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/sphere.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/sphere.js/** * @author bhouston/http://exocortex.com * @author mrdoob/http://mrdoob.com/*//*///Spher The constructor of the E object. Used to create a sphere object within a three-dimensional space. The function function of the sphere object is implemented using the function prototype object defined by///.//////usage: var center = new Vector3 (0,0,0), radius = 5; var sphere = new Sphere (Center,radius);///Create a sphere with a center of 0,0,0 radius of 5. .*////<summary>sphere</summary>///< PARAM NAME = "center" type= "Vector3" > Center point coordinate value </param>///<param name = "radius" type= "number" >number sphere radius </param>three. Sphere = function (center, radius) {this.center = (center!== undefined)? center:new three. Vector3 ();//assignment or initialization of Centerthis.radius = (radius!== undefined)? radius:0;//Assignment or initialization radius};/******************************************** The following are the function functions provided by the Sphere object. ******************** /three. Sphere.prototype = {Constructor:three. The sphere,//constructor, which returns a reference to the Sphere function that created this object/*///set method is used to set the starting point, end point, and Center,radius coordinate value of the sphere from the new setting. and returns a sphere of the value of the coordinate of the newly-formed radius. *////<summary >set</summary>///<param name = "Center" type= "Vector3" > CenterPoint coordinate value </param>///<param name = "radius" type= "number" >number sphere radius </param>///<returns type= "Sphere "> returns the new RADIUS, the coordinate value of the Sphere </returns>set:function (center, RADIUS) {this.center.copy (center); this.radius = radius; Return this;//returns the new RADIUS, the coordinate value of the sphere},/*///setfrompoints method to reset the radius of the sphere by obtaining the largest value in the points array of Vector3 objects to the center distance. The optional parameter optionalcenter is used to set the center of the sphere. and returns the new RADIUS, the coordinate value of the sphere.//NOTE: If you set the Optionalcenter parameter to the Setfrompoints () method, The distance from the value to the center of the points array will change. *////<summary>setfrompoints</summary>///<param name = "Points" type= " Vector3array ">vector3 object composed of points array </param>///<param name =" Optionalcenter "type=" Vector3 "> Optional parameters, Receives the returned result, the center point of the Sphere </param>///<returns type= "Sphere" > returns the new RADIUS, the coordinate value of the sphere </returns>setfrompoints:function () {var box = new three. Box3 (); return function (points, optionalcenter) {var center = this.center;if (optionalcenter!== undefined) {center.c Opy (Optionalcenter);} else {box.setfrompoints (points). Center (center);} var maxradiussq = 0;for (var i = 0, il = points.length; i < il; i + +) {maxradiussq = Math.max (maxradiussq, center.distancetosquared (points[i]));//To points the maximum value in the array to the center of the circle and assign a value to MAXRADIUSSQ} This.radius = Math.sqrt (MAXRADIUSSQ); return this;//returns the new RADIUS, the coordinate value of the sphere}; (), the/*///copy method is used to copy the center, RADIUS, Center,radius value of the sphere. Returns the new RADIUS, the coordinate value of the sphere *////<summary>copy</summary>///<param name = "Sphere" type= "sphere" > Sphere </param>///<returns type= "Sphere" > returns the new RADIUS, the coordinate value of the sphere </returns>copy: function (Sphere) {this.center.copy (sphere.center); This.radius = Sphere.radius;return this;//Returns the new RADIUS, the coordinate value of the sphere},/*/// The empty method is used to determine whether the radius of the sphere is less than or equal to 0 and is used to determine the sphere with a radius of 0 or less than 0 in space. *////<summary>empty</summary>///<returns type= " Boolean "> Returns TRUE or False</returns>empty:function () {return (This.radius <= 0);//returns TRUE or False},/*///cont The Ainspoint method is used to get the parameter point (a Vector3 's three-dimensional dot coordinate) within the current sphere. *////<summary>containspoint</summary>///<param Name = "point" type= "Vector3" > a Vector3 three-dimensional dot coordinate </param>///<returns type= "Boolean" > Returns True or false</returns>containspoint:function (point) {return (point.distancetosquared (this.center) <= (THIS.R Adius * This.radius));//returns True or False},/*///distancetopoint method is used to obtain the minimum length of a point to a sphere sphere object surface within a three-dimensional space .*////<summary> Distancetopoint</summary>///<param name = "point" type= "Vector3" > three-dimensional points coordinates of VECTOR3 in a three-dimensional space </param>// /<returns type= "Number" > returns the minimum length of a point to the sphere sphere object's surface in a three-dimensional space. </returns>distancetopoint:function (point) { Return (Point.distanceto (this.center)-This.radius);//returns the minimum length of a point to the sphere sphere object's surface within a three-dimensional space.},/*/// The Intersectssphere method gets whether the current sphere intersects the parameter sphere sphere object, returns True, or false*////<summary>intersectssphere</summary>/// <param name = "Sphere" type= "sphere" > a sphere Sphere </param>///<returns type= "Boolean" > returns TRUE or False </returns>intersectssphere:function (Sphere) {var radiussum = This.radius + Sphere.radius;return SPHERE.CENTER.D Istancetosquared (This.center) <= (radiussum * radiussum);//return TRUE or False},/*///clamppoinThe T method is used to shrink a sphere by a parameter point. If point is outside the sphere, force point to be set to the sphere surface, and if point is inside the sphere, reset the radius of the sphere to point to the current sphere radius .*////<summary> Clamppoint</summary>///<param name = "point" type= "Vector3" > a Vector3 three-dimensional dot coordinate </param>///<param Name = "Optionaltarget" type= "Vector3" > Optional parameters, receive returned results, return trimmed boundary points </param>///<returns type= "Vector3" > Returns the trimmed boundary points. </returns>clamppoint:function (point, Optionaltarget) {var deltalengthsq = This.center.distanceToSquared (point); var result = Optionaltarget | | New three. Vector3 (); Result.copy (point), if (Deltalengthsq > (This.radius * this.radius)) {result.sub (this.center). Normaliz E (); Result.multiplyscalar (This.radius). Add (This.center);} Return result;//returns the trimmed boundary point},/*///getboundingbox method returns the Box3 cube boundary of the current sphere (which should be circumscribed to a cube in the sphere)///And Getboundingsphere () in the Box3 class Method corresponds to. *////<summary>getboundingbox</summary>///<param name = "Optionaltarget" type= "THREE. Box3 () "> Optional parameters, three. The Box3 () cube object, which is used to receive the return value </param>///<returns type= "three. Sphere () > Return to the current sphereThe Box3 cube boundary (which should be circumscribed to a cube in the sphere) </returns>getboundingbox:function (optionaltarget) {var box = Optionaltarget | | new T HREE. Box3 (); Box.set (This.center, This.center); Box.expandbyscalar (This.radius); return box;//returns the BOX3 cube boundary of the current sphere ( This should be circumscribed to a cube of the sphere) the},/*///applymatrix4 method applies transformations to the center and radius of the current sphere sphere object by passing the matrix (rotation, scaling, moving, and other transformation matrices) .*////<summary> Applymatrix4</summary>///<param name = "Matrix" type= "Matrix4" > (rotation, scaling, moving and other transformation matrices </param>///< Returns type= "Boolean" > returns the transformed Sphere. </returns>applymatrix4:function (Matrix) {this.center.applyMatrix4 ( Matrix); This.radius = This.radius * Matrix.getmaxscaleonaxis (); return this;//returns the transformed sphere.},/*/// The Translate method is used to move the position of the current sphere through the parameter offset. *////<summary>translate</summary>///<param name = "offset" type= " Vector3 "> Offset </param>///<returns type=" Boolean "> returns the new RADIUS, the coordinate value of the sphere </returns>translate:function ( Offset) {This.center.add (offset); return this;//returns the new RADIUS, the coordinate value of the sphere},/*///equals method used to get the parameter sphere (a sphere sphere) is exactly equal to the current sphere, That is, the center and RADIUSEqual. *////<summary>equals</summary>///<param name = "Sphere" type= "sphere" > a sphere Sphere </param >///<returns type= "Boolean" > Returns True or False</returns>equals:function (Sphere) {return Sphere.center.equals (This.center) && (Sphere.radius = = = This.radius);//returns TRUE or False},/*clone method// Clone method clones a Sphere object. *////<summary>clone</summary>///<returns type= "Sphere" > Return Sphere Object </returns> Clone:function () {return new three. Sphere (). copy (this);//Return Sphere 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/line3.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 (14) Math/sphere.js

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.