-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.js
161 lines (149 loc) · 5.67 KB
/
graph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var Graph = (function(){
function Graph(f, c){
this.points = [];
this.backbone = [];
this.config = c;
for(var x=c.range.x.start; x<=c.range.x.end; x+=c.step) {
var _points = [];
for(var y=c.range.y.start; y<=c.range.y.end; y+=c.step) {
var z = new Complex(x, y);
var w = f(z);
_points.push([z.x, z.y, w.x, w.y]);
}
this.points.push(_points);
}
for(var x=c.range.x.start; x<=c.range.x.end; x+=c.step) {
var z = new Complex(x, 0);
var w = f(z);
this.backbone.push([z.x, z.y, w.x, w.y]);
}
//this.R = Matrix.rotation(0, 3, Math.PI/7);
this.setAngles([0,0,0,0,0,0]);
}
Graph.rotations = [];
for(var i=0; i<3; i++) {
for(var j=i+1; j<4; j++) {
Graph.rotations.push((function(i,j){
return (function(theta) {
return Matrix.rotation(i, j, theta);
});
})(i,j));
}
}
if(Graph.rotations.length != 6) {
alert("Error: Graph.rotations.length != 6");
}
Graph.prototype = {
setAngles: function(angles){
this.R = Matrix.identity();
this.log(Graph.rotations);
for(var i in Graph.rotations) {
var rot = Graph.rotations[i](angles[i]);
this.log(rot.data);
this.R = Matrix.multiply(this.R, rot);
}
},
draw: function(){
var c = this.config;
var width = c.ctx.canvas.width;
var height = c.ctx.canvas.height;
c.ctx.clearRect(0,0,width,height);
//this.clearLog();
var canvas_points = [];
var canvas_backbone = [];
for(var i=0; i<this.points.length; i++) {
var _canvas_points = [];
for(var j=0; j<this.points[0].length; j++) {
_canvas_points.push(
this.convert_to_canvas(this.points[i][j])
);
}
canvas_points.push(_canvas_points);
}
for(var i=0; i<this.backbone.length; i++) {
canvas_backbone.push(
this.convert_to_canvas(this.backbone[i])
);
}
var squares = [];
for(var i=0; i<this.points.length-1; i++) {
for(var j=0; j<this.points[0].length-1; j++) {
squares.push([
canvas_points[i][j],
canvas_points[i+1][j],
canvas_points[i+1][j+1],
canvas_points[i][j+1]
]);
}
}
/* sort by the depth of the first point in the square */
var cmp = function(f){
return (function(a,b) {
if (f(a) > f(b)) {
return 1;
} else {
return -1;
}
});
}
squares.sort(cmp(function(square){
//return (square[0].z + square[1].z + square[2].z + square[3].z);
return -square[0].z;
}));
{
this.config.ctx.strokeStyle = "rgba(0,0,255,1)";
this.config.ctx.lineWidth = 1;
this.canvas('beginPath');
this.canvas('moveTo', canvas_backbone[0]);
for(var i in canvas_backbone) {
this.canvas('lineTo', canvas_backbone[i]);
}
this.canvas('stroke');
}
for(var k in squares) {
h = (function(z){return Math.floor(z < 0 ? 0 : (z > 255 ? 255 : z));})(squares[k][0].z);
this.config.ctx.fillStyle = "rgba("+h+","+h+","+h+",.5)";
drk = Math.floor(h/2);
this.config.ctx.strokeStyle = "rgba("+drk+","+drk+","+drk+",.3)";
this.canvas('beginPath');
this.canvas('moveTo', squares[k][0]);
this.canvas('lineTo', squares[k][1]);
this.canvas('lineTo', squares[k][2]);
this.canvas('lineTo', squares[k][3]);
this.canvas('moveTo', squares[k][0]);
this.canvas('closePath');
this.canvas('stroke');
this.canvas('fill');
}
},
convert_to_canvas: function(point) {
var c = this.config;
var width = c.ctx.canvas.width;
var height = c.ctx.canvas.height;
var point = Matrix.apply(this.R, point);
var _x = point[0];
var _y = point[2];
var _z = point[1]; // for depth
return {
x: width /2 + (c.center.x + _x)/c.scale*width,
y: height/2 - (c.center.y + _y)/c.scale*height,
z: 127 + (_z)/c.scale*256 // 256 is the width of a color channel
};
},
log: function(msg) {
$("#log").append("<li>" + msg + "</li>");
},
clearLog: function() {
$("#log").html("");
},
canvas: function(funcname, point) {
if(typeof point != 'undefined' && point !== null) {
return this.config.ctx[funcname](point.x, point.y);
}
else {
return this.config.ctx[funcname]();
}
}
}
return Graph;
})();