pointvec/sources/scripts/pos.js

47 lines
913 B
JavaScript
Raw Normal View History

2017-11-04 19:59:11 -04:00
function Pos(x,y)
{
2017-11-21 16:24:25 -05:00
this.__serialized_name__ = ".";
2017-11-04 19:59:11 -04:00
this.x = x;
this.y = y;
this.toString = function()
{
return x+","+y;
}
this.sub = function(pos2)
{
return new Pos(this.x - pos2.x,this.y - pos2.y)
}
2017-11-05 00:33:04 -04:00
2017-11-05 19:18:57 -05:00
this.add = function(pos2)
{
return new Pos(this.x + pos2.x,this.y + pos2.y)
}
2017-11-05 00:33:04 -04:00
this.is_equal = function(pos2)
{
2017-11-07 16:40:13 -05:00
return Math.abs(pos2.x) == Math.abs(this.x) && Math.abs(pos2.y) == Math.abs(this.y);
2017-11-05 00:33:04 -04:00
}
2017-11-12 16:47:34 -05:00
this.scale = function(a)
{
return new Pos(this.x*a,this.y*a)
}
2017-11-13 14:45:31 -05:00
this.mirror = function(x = -1,y = 1)
{
return new Pos(this.x * x,this.y * y);
}
this.clamp = function(min,max)
{
return new Pos(clamp(this.x,min,max),clamp(this.y,min,max));
}
function clamp(v, min, max) { return v < min ? min : v > max ? max : v; }
2017-11-21 16:24:25 -05:00
}
// This is ugly, but Pos.__serialized_name__ == ".";
// Let's keep the character count low.
window["."] = Pos;