Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Rectangle type #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<!-- include spec files here... -->
<script type="text/javascript" src="spec/SvgMathFunctionSpec.js"></script>
<script type="text/javascript" src="spec/SvgMathRectangle.js"></script>

<script type="text/javascript">
(function() {
Expand Down
52 changes: 52 additions & 0 deletions spec/spec/SvgMathRectangle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
describe('SVG.math.Rectangle', function() {
var Point = SVG.math.Point;
var Rectangle = SVG.math.Rectangle;
var p1 = new Point(-1,-2);
var p2 = new Point(-1,2);
var p3 = new Point(1,-2);
var p4 = new Point(1,2);
var rect1 = new Rectangle(p1,p4);

it('knows its dimensions', function() {
expect(rect1.xmin).toEqual(-1);
expect(rect1.ymin).toEqual(-2);
expect(rect1.xmax).toEqual(1);
expect(rect1.ymax).toEqual(2);
expect(rect1.width).toEqual(2);
expect(rect1.height).toEqual(4);
expect(rect1.area()).toEqual(8);
})

it('can be constructed with any pair of opposite corners', function() {
expect(new Rectangle(p2,p3)).toEqual(rect1);
expect(new Rectangle(p3,p2)).toEqual(rect1);
expect(new Rectangle(p4,p1)).toEqual(rect1);
expect(new Rectangle(p1,p2)).not.toEqual(rect1);
});

it('can have zero width', function() {
var rect5 = new Rectangle(p1,p2);
expect(rect5.xmin).toEqual(-1);
expect(rect5.ymin).toEqual(-2);
expect(rect5.xmax).toEqual(-1);
expect(rect5.ymax).toEqual(2);
expect(rect5.width).toEqual(0);
expect(rect5.height).toEqual(4);
expect(rect5.area()).toEqual(0);
});

it ('knows what points it contains', function() {
expect(rect1.contains(new Point(0.5,1.5))).toBe(true);
expect(rect1.contains(new Point(1.5,0.5))).toBe(false);
expect(rect1.contains(new Point(0.5,2.5))).toBe(false);
})

it ('knows what rectangles it intersects', function() {
expect(rect1.intersects(rect1)).toBe(true);
expect(rect1.intersects(new Rectangle(new Point(-3,-4),new Point(3,-3)))).toBe(false);
expect(rect1.intersects(new Rectangle(new Point(-3,-4),new Point(3,1)))).toBe(true);
expect(rect1.intersects(new Rectangle(new Point(-3,-4),new Point(3,4)))).toBe(true);
expect(rect1.intersects(new Rectangle(new Point(-3,-4),new Point(3,4)))).toBe(true);
})
});

76 changes: 75 additions & 1 deletion svg.math.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,81 @@
)
);
}

});




/********** AXIS-PARALLEL RECTANGLE *************/

/**
* Construct a rectangle from two opposite corners (points)
*/
SVG.math.Rectangle = function Rectangle(corner1, corner2) {
this.xmin = Math.min(corner1.x,corner2.x);
this.xmax = Math.max(corner1.x,corner2.x);
this.ymin = Math.min(corner1.y,corner2.y);
this.ymax = Math.max(corner1.y,corner2.y);

this.width = this.xmax-this.xmin;
this.height = this.ymax-this.ymin;
};

/**
* Default draw attributes
*/
SVG.math.Rectangle.attr = {
stroke: '#000',
fill: 'none',
};

SVG.extend(SVG.math.Rectangle, {
/**
* With a single parameter - draw a rectangle with default attributes.
* With two parameters - draw a rectangle with the given attributes.
*/
draw: function(svg, attr) {
attr = attr || SVG.math.Rectangle.attr;
this.svg = svg;
this.rectangle = svg.rect(this.width,this.height).move(this.xmin,this.ymin).attr(attr);
return this;
},

/**
* remove the currently drawn rectangle.
*/
remove: function() {
if (this.rectangle) {
this.rectangle.remove();
delete this.svg;
delete this.rectangle;
}
},

/**
* @return true if this rectangle interior-contains the given point
*/
contains: function(point) {
var xcontains = this.xmin<point.x && point.x<this.xmax;
var ycontains = this.ymin<point.y && point.y<this.ymax;
return ycontains && xcontains;
},

/**
* @return true if this rectangle interior-intersects the other rectangle
*/
intersects: function(other) {
var xintersects = this.xmax>other.xmin && other.xmax>this.xmin;
var yintersects = this.ymax>other.ymin && other.ymax>this.ymin;
return yintersects && xintersects;
},

/**
* @return the area of this rectangle.
*/
area: function() {
return this.width*this.height;
}
});

})();