Skip to content

Commit 339819e

Browse files
committed
🛠 metafizzy#26 refactor & recalc updateSortValue
+ Use Vector.lerp + Add Hemisphere apex
1 parent 363557f commit 339819e

File tree

4 files changed

+40
-39
lines changed

4 files changed

+40
-39
lines changed

js/cone.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Cone.prototype.create = function(/* options */) {
3434

3535
// vectors used for calculation
3636
this.renderApex = new Vector();
37+
this.renderCentroid = new Vector();
3738
this.tangentA = new Vector();
3839
this.tangentB = new Vector();
3940

@@ -45,16 +46,10 @@ Cone.prototype.create = function(/* options */) {
4546
};
4647

4748
Cone.prototype.updateSortValue = function() {
48-
// call super
49-
Ellipse.prototype.updateSortValue.apply( this, arguments );
50-
var apexNormal = new Vector();
51-
apexNormal.set( this.renderOrigin )
52-
.subtract( this.apex.renderOrigin );
53-
var apexAngleZ = Math.atan2( apexNormal.z, apexNormal.y );
54-
apexAngleZ = utils.modulo( apexAngleZ, TAU );
55-
//center of cone is one third of its length.
56-
var apexZ = this.length/3 * Math.sin(apexAngleZ);
57-
this.sortValue -= apexZ;
49+
// center of cone is one third of its length
50+
this.renderCentroid.set( this.renderOrigin )
51+
.lerp( this.apex.renderOrigin, 1/3 );
52+
this.sortValue = this.renderCentroid.z;
5853
};
5954

6055
Cone.prototype.render = function( ctx, renderer ) {

js/hemisphere.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,38 @@
66
// module definition
77
if ( typeof module == 'object' && module.exports ) {
88
// CommonJS
9-
module.exports = factory( require('./boilerplate'), require('./ellipse') );
9+
module.exports = factory( require('./boilerplate'), require('./vector'),
10+
require('./anchor'), require('./ellipse') );
1011
} else {
1112
// browser global
1213
var Zdog = root.Zdog;
13-
Zdog.Hemisphere = factory( Zdog, Zdog.Ellipse );
14+
Zdog.Hemisphere = factory( Zdog, Zdog.Vector, Zdog.Anchor, Zdog.Ellipse );
1415
}
15-
}( this, function factory( utils, Ellipse ) {
16+
}( this, function factory( utils, Vector, Anchor, Ellipse ) {
1617

1718
var Hemisphere = Ellipse.subclass({
1819
fill: true,
1920
});
2021

2122
var TAU = utils.TAU;
2223

23-
Hemisphere.prototype.updateSortValue = function() {
24+
Hemisphere.prototype.create = function(/* options */) {
2425
// call super
25-
Ellipse.prototype.updateSortValue.apply( this, arguments );
26-
var contourAngleZ = Math.atan2( this.renderNormal.z, this.renderNormal.y );
27-
contourAngleZ = utils.modulo( contourAngleZ, TAU );
28-
//center of dome is half the radius.
29-
var domeZ = this.diameter/2/2 * Math.sin(contourAngleZ);
30-
this.sortValue -= domeZ;
26+
Ellipse.prototype.create.apply( this, arguments );
27+
// composite shape, create child shapes
28+
this.apex = new Anchor({
29+
addTo: this,
30+
translate: { z: this.diameter/2 },
31+
});
32+
// vector used for calculation
33+
this.renderCentroid = new Vector();
34+
};
35+
36+
Hemisphere.prototype.updateSortValue = function() {
37+
// centroid of hemisphere is 3/8 between origin and apex
38+
this.renderCentroid.set( this.renderOrigin )
39+
.lerp( this.apex.renderOrigin, 3/8 );
40+
this.sortValue = this.renderCentroid.z;
3141
};
3242

3343
Hemisphere.prototype.render = function( ctx, renderer ) {

js/shape.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,25 +108,23 @@ Shape.prototype.transform = function( translation, rotation, scale ) {
108108
});
109109
};
110110

111-
112111
Shape.prototype.updateSortValue = function() {
113-
// average sort all z points.
114-
// ignore the final point if it is a closed shape.
115-
var howManyPoints = this.pathCommands.length;
116-
var sortValueTotal = 0;
112+
// sort by average z of all points
113+
// def not geometrically correct, but works for me
114+
var pointCount = this.pathCommands.length;
117115
var firstPoint = this.pathCommands[0].endRenderPoint;
118-
var lastPoint = this.pathCommands[this.pathCommands.length-1].endRenderPoint;
119-
if (howManyPoints > 2 &&
120-
firstPoint.isSame(lastPoint)) {
121-
howManyPoints -= 1; // closed shape; ignore final point.
116+
var lastPoint = this.pathCommands[ pointCount - 1 ].endRenderPoint;
117+
// ignore the final point if self closing shape
118+
var isSelfClosing = pointCount > 2 && firstPoint.isSame( lastPoint );
119+
if ( isSelfClosing ) {
120+
pointCount -= 1;
122121
}
123-
124-
for (var i = 0; i < howManyPoints; i++) {
125-
sortValueTotal += this.pathCommands[i].endRenderPoint.z;
126-
}
127-
// average sort value of all points
128-
// def not geometrically correct, but works for me
129-
this.sortValue = sortValueTotal / howManyPoints;
122+
123+
var sortValueTotal = 0;
124+
for ( var i = 0; i < pointCount; i++ ) {
125+
sortValueTotal += this.pathCommands[i].endRenderPoint.z;
126+
}
127+
this.sortValue = sortValueTotal / pointCount;
130128
};
131129

132130
// ----- render ----- //

js/vector.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ Vector.prototype.isSame = function( pos ) {
7979
if ( !pos ) {
8080
return false;
8181
}
82-
return (this.x === pos.x &&
83-
this.y === pos.y &&
84-
this.z === pos.z);
82+
return this.x === pos.x && this.y === pos.y && this.z === pos.z;
8583
};
8684

8785
Vector.prototype.add = function( pos ) {

0 commit comments

Comments
 (0)