我想创建像点和点的几何对象。矢量 但我更喜欢可以使用数学运算符(+, - ,*)
来计算它请参阅下面的code
:
var Point = function(px,py,pz){return {x:px,y:py,z:pz,class:'point'}};
var Vector = function(p,d){
//p is origin point of vector
//d can be other Point or distance(not use it here)
var V = {};
if(d.class=='point'){
V.class = 'vector';
V.origin = p;
V.x = d.x-p.x,
V.y = d.y-p.y;
V.z = d.z-p.z;
V.magnitude = Math.sqrt(V.x*V.x+V.y*V.y+V.z*V.z);
//can be add here some other properties and Methods
//...
}else{/* some code not required */};
return V
};
// My question is if there way can be calculate two vectors using Mathematical Operators
var A = Point(2,4,1); // Point A
var B = Point(3,-2,5); // other Point B
var V1 = Vector(A,B); // 1st Vector
var V2 = Vector(A,Point(0,1,1)); // 2nd Vector
//now when add 1st Vector with 2nd Vector have a result new Vector like a Math
Va = V1 + V2 ; // i know i must add methods for calculate it
// but I'm ask if can put this Methods as Operator(+,*,^...)
如果不是:使用RegExp
进行上述操作的最佳方法是什么
对不起,如果有错误......