Skip to content

Commit

Permalink
Fix size of triangle.
Browse files Browse the repository at this point in the history
Also add tests to verify symbol sizes.
  • Loading branch information
mbostock committed Dec 8, 2015
1 parent 7691797 commit 02c4431
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
Binary file modified img/triangle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"d3-path": "~0.1.2"
},
"devDependencies": {
"d3-polygon": "~0.0.3",
"faucet": "0.0",
"rollup": "0.20.5",
"tape": "4",
Expand Down
2 changes: 1 addition & 1 deletion src/symbol/triangle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var sqrt3 = Math.sqrt(3);

export default {
draw: function(context, size) {
var y = -Math.sqrt(size * 4 / 27);
var y = -Math.sqrt(size / (sqrt3 * 3));
context.moveTo(0, y * 2);
context.lineTo(-sqrt3 * y, -y);
context.lineTo(sqrt3 * y, -y);
Expand Down
10 changes: 10 additions & 0 deletions test/inDelta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var tape = require("tape");

tape.Test.prototype.inDelta = function(actual, expected) {
this._assert(expected - 1e-6 < actual && actual < expected + 1e-6, {
message: "should be in delta",
operator: "inDelta",
actual: actual,
expected: expected
});
};
12 changes: 12 additions & 0 deletions test/polygonContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var polygon = require("d3-polygon");

module.exports = function() {
return {
points: null,
area: function() { return Math.abs(polygon.area(this.points)); },
moveTo: function(x, y) { this.points = [[x, y]]; },
lineTo: function(x, y) { this.points.push([x, y]); },
rect: function(x, y, w, h) { this.points = [[x, y], [x + w, y], [x + w, y + h], [x, y + h]]; },
closePath: function() {}
};
};
55 changes: 53 additions & 2 deletions test/symbol-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var tape = require("tape"),
shape = require("../");
shape = require("../"),
polygonContext = require("./polygonContext");

require("./inDelta");
require("./pathEqual");

tape("symbol() returns a default symbol shape", function(test) {
Expand Down Expand Up @@ -47,27 +49,62 @@ tape("symbol.size(size) observes the specified size constant", function(test) {
test.end();
});

tape("symbol.type(circle) generates the expected path", function(test) {
var s = shape.symbol().type(shape.circle).size(function(d) { return d; });
test.pathEqual(s(0), "M0,0");
test.pathEqual(s(20), "M2.523133,0A2.523133,2.523133,0,1,1,-2.523133,0A2.523133,2.523133,0,1,1,2.523133,0");
test.end();
});

tape("symbol.type(cross) generates a polygon with the specified size", function(test) {
var p = polygonContext(), s = shape.symbol().type(shape.cross).context(p);
s.size(1)(); test.inDelta(p.area(), 1);
s.size(240)(); test.inDelta(p.area(), 240);
test.end();
});

tape("symbol.type(cross) generates the expected path", function(test) {
var s = shape.symbol().type(shape.cross).size(function(d) { return d; });
test.pathEqual(s(0), "M0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0Z");
test.pathEqual(s(20), "M-3,-1L-1,-1L-1,-3L1,-3L1,-1L3,-1L3,1L1,1L1,3L-1,3L-1,1L-3,1Z");
test.end();
});

tape("symbol.type(diamond) generates a polygon with the specified size", function(test) {
var p = polygonContext(), s = shape.symbol().type(shape.diamond).context(p);
s.size(1)(); test.inDelta(p.area(), 1);
s.size(240)(); test.inDelta(p.area(), 240);
test.end();
});

tape("symbol.type(diamond) generates the expected path", function(test) {
var s = shape.symbol().type(shape.diamond).size(function(d) { return d; });
test.pathEqual(s(0), "M0,0L0,0L0,0L0,0Z");
test.pathEqual(s(10), "M0,-2.942831L1.699044,0L0,2.942831L-1.699044,0Z");
test.end();
});

tape("symbol.type(star) generates a polygon with the specified size", function(test) {
var p = polygonContext(), s = shape.symbol().type(shape.star).context(p);
s.size(1)(); test.inDelta(p.area(), 1);
s.size(240)(); test.inDelta(p.area(), 240);
test.end();
});

tape("symbol.type(star) generates the expected path", function(test) {
var s = shape.symbol().type(shape.star).size(function(d) { return d; });
test.pathEqual(s(0), "M0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0L0,0Z");
test.pathEqual(s(10), "M0,-2.984649L0.670095,-0.922307L2.838570,-0.922307L1.084237,0.352290L1.754333,2.414632L0,1.140035L-1.754333,2.414632L-1.084237,0.352290L-2.838570,-0.922307L-0.670095,-0.922307Z");
test.end();
});

tape("symbol.type(square) generates a polygon with the specified size", function(test) {
var p = polygonContext(), s = shape.symbol().type(shape.square).context(p);
s.size(1)(); test.inDelta(p.area(), 1);
s.size(240)(); test.inDelta(p.area(), 240);
test.end();
});

tape("symbol.type(square) generates the expected path", function(test) {
var s = shape.symbol().type(shape.square).size(function(d) { return d; });
test.pathEqual(s(0), "M0,0h0v0h0Z");
Expand All @@ -76,10 +113,24 @@ tape("symbol.type(square) generates the expected path", function(test) {
test.end();
});

tape("symbol.type(triangle) generates a polygon with the specified size", function(test) {
var p = polygonContext(), s = shape.symbol().type(shape.triangle).context(p);
s.size(1)(); test.inDelta(p.area(), 1);
s.size(240)(); test.inDelta(p.area(), 240);
test.end();
});

tape("symbol.type(triangle) generates the expected path", function(test) {
var s = shape.symbol().type(shape.triangle).size(function(d) { return d; });
test.pathEqual(s(0), "M0,0L0,0L0,0Z");
test.pathEqual(s(10), "M0,-2.434322L2.108185,1.217161L-2.108185,1.217161Z");
test.pathEqual(s(10), "M0,-2.774528L2.402811,1.387264L-2.402811,1.387264Z");
test.end();
});

tape("symbol.type(wye) generates a polygon with the specified size", function(test) {
var p = polygonContext(), s = shape.symbol().type(shape.wye).context(p);
s.size(1)(); test.inDelta(p.area(), 1);
s.size(240)(); test.inDelta(p.area(), 240);
test.end();
});

Expand Down

0 comments on commit 02c4431

Please sign in to comment.