The Deep exploration of the function overload of JavaScript implementation

Source: Internet
Author: User
Tags array end return
javascript| function

<script>
function point2d (x, y)
{
this.x = x;
This.y = y;
Point2D.prototype.quadrant = function ()
{
if (x > 0 && y > 0) return "I";
else if (x < 0 && y > 0) return "II";
else if (x < 0 && y < 0) return "III";
else if (x > 0 && y < 0) return "IV";
else if (x = = 0) return "x-axis";
else if (y = = 0) return return "Y-axis";
else throw new Error ();
}
Point2D.prototype.toVector = function ()
{
return new vector2d (x, y);
}
Point2D.prototype.distance = function ()//Distance
{
if (arguments.length = = 1 && arguments[0] instanceof point2d)
{
Return this._point_distance.apply (this, arguments);
}
else if (arguments.length = = 1 && arguments[0] instanceof vector2d)
{
Return this._vector_distance.apply (this, arguments);
}
Else
{
throw new Error ("Argument error!");
}
}
point2d.prototype._point_distance = function (p)//find the distance between two points (function overload)
{
Return (new Vector2d (P,this)). Length ();
}
Point2d.prototype._vector_distance = function (v)//distance from point to vector (function overload)
{
var v1 = new Vector2d (this, v.start);
var v2 = new Vector2d (this, v.end);

var area = Math.Abs (V1.cross (v2)); Parallelogram area = V1 X v2 = |v1v2|sin (V1,V2)

return Area/v.length (); The parallelogram area divided by the bottom length is the distance from point to vector
}
}
function vector2d ()
{
if (arguments.length = = 2 && arguments[0] instanceof point2d && arguments[1] instanceof point2d)
{
_point_point_vector2d.apply (this, arguments);
}
else if (arguments.length = = 2 &&!isnan (arguments[0)) &&!isnan (arguments[1))
{
_double_double_vector2d.apply (this, arguments);
}
else if (arguments.length = = 4 &&!isnan (arguments[0)) &&!isnan (arguments[1))
&&!isnan (arguments[2]) &&!isnan (arguments[3))
{
_double_double_double_double_vector2d.apply (this, arguments);
}
Else
{
throw new Error ("Argument error!");
}
}
function _point_point_vector2d (p1, p2)
{
This.start = p1;
This.end = p2;
Vector2D.prototype.length = function ()//Find the length of a vector
{
Return Math.sqrt (this.pond_x () * this.pond_x () + this.pond_y () * this.pond_y ());
}
Vector2D.prototype.pond_x = function ()//x direction component
{
return this.start.x-this.end.x;
}
Vector2D.prototype.pond_y = function ()
{
return this.start.y-this.end.y;
}
Vector2D.prototype.cross = function (v)//quadrature of vector P1 X P2 = x1y2-x2y1
{
return this.pond_x () * v.pond_y ()-v.pond_x () * this.pond_y ();
}
}
function _double_double_vector2d (x,y)//overloaded constructors vector2d
{
This.pointpairs = new Array ();
This.pointpairs[0] = new point2d (0, 0);
THIS.POINTPAIRS[1] = new point2d (x, y);

_point_point_vector2d.apply (this, this.pointpairs);
}
function _double_double_double_double_vector2d (x1, y1, x2, y2)//overloaded constructors vector2d
{
This.pointpairs = new Array ();
This.pointpairs[0] = new POINT2D (x1, y1);
THIS.POINTPAIRS[1] = new point2d (x2, y2);

_point_point_vector2d.apply (this, this.pointpairs);
}
var p1 = new point2d (0,0);
var P2 = new point2d (10,10);
var v1 = new Vector2d (P1,P2); The vector V1 is constructed by means of a two-point (P1,P2)
Alert ("Vector v1 length:" +v1.length ());
var v2 = new vector2d (0,0,5,5); A vector V2 is constructed by means of four coordinates (X1,Y1,X2,Y2)
Alert ("Vector v2 Length:" +v2.length ());
var v3 = new vector2d (0,10); Constructs a vector V3 by specifying the end point
Alert ("Vector v3 length:" +v3.length ());
Alert ("Vector v1 with v2": +v1.cross (v2)); Find V1 X V2 (because parallel, so the result is 0)

var p3 = new point2d (10,0);
Alert ("Point P1 distance from P3:" +p1.distance (p3));
Alert ("The distance from the point P3 to the vector v1:" +p3.distance (v1));
</script>



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.