Skip to content

Instantly share code, notes, and snippets.

@to
Created August 22, 2010 14:40
Show Gist options
  • Save to/543839 to your computer and use it in GitHub Desktop.
Save to/543839 to your computer and use it in GitHub Desktop.
update(MochiKit.Style.Coordinates.prototype, {
equals : function(target){
return target && (this.x == target.x && this.y == target.y);
}
,
clone : function(){
return new Coordinates(this.x, this.y);
}
,
getDistance : function(pos){
pos = pos || new Coordinates(0, 0);
return Math.sqrt(Math.pow(this.x - pos.x, 2) + Math.pow(this.y - pos.y, 2))
}
,
subtract : function(target){
return new Coordinates(this.x - target.x, this.y - target.y);
}
,
plus : function(target){
if(target.w)
return this.plus({x : target.w, y: target.h});
return new Coordinates(this.x + target.x, this.y + target.y);
}
,
multiply : function(target){
if(typeof(target)=='number')
target = {x:target, y:target};
return new Coordinates(this.x * target.x, this.y * target.y);
}
,
divide : function(target){
if(typeof(target)=='number')
target = {x:target, y:target};
return new Coordinates(this.x / target.x, this.y / target.y);
}
,
max : function(max){
return new Coordinates(
Math.max(this.x, max.x), Math.max(this.y, max.y));
}
,
min : function(min){
return new Coordinates(
Math.min(this.x, min.x), Math.max(this.y, min.y));
}
,
range : function(min, max){
return new Coordinates(
Math.min(Math.max(this.x, min.x), max.x),
Math.min(Math.max(this.y, min.y), max.y));
}
,
between : function(min, max){
return (min.x <= this.x && this.x <= max.x) &&
(min.y <= this.y && this.y <= max.y);
}
});
update(MochiKit.Style.Coordinates, {
convert : function(src){
return new Coordinates(src.w, src.h);
}
});
update(MochiKit.Style.Dimensions.prototype, {
equals : function(target){
return target && (this.w == target.w && this.h == target.h);
}
,
subtract : function(target){
return new Dimensions(this.w - target.w, this.h - target.h);
}
,
multiply : function(target){
if(typeof(target)=='number')
target = {w:target, h:target};
return new Dimensions(this.w * target.w, this.h * target.h);
}
,
divide : function(target){
if(typeof(target)=='number')
target = {w:target, h:target};
return new Dimensions(this.w / target.w, this.h / target.h);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment