-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransforms.js
91 lines (73 loc) · 3.03 KB
/
transforms.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
/*
* Created By Gustaf Sterner 2012-11-29
*/
/*
function getMaxOfArray(numArray) {
return Math.max.apply(null, numArray);
}
function getMinOfArray(numArray) {
return Math.min.apply(null, numArray);
}
*/
function Transform(xarray, yarray, canvasWidth, canvasHeight) {
this.minX = Numerics.getMinOfArray(xarray);
this.maxX = Numerics.getMaxOfArray(xarray);
this.minY = Numerics.getMinOfArray(yarray);
this.maxY = Numerics.getMaxOfArray(yarray);
this.frameX = canvasWidth * 0.1;
this.frameY = canvasHeight * 0.1;
this.width = canvasWidth;
this.height = canvasHeight;
}
Transform.translationToPlotCoordinate = function(plotLength, realLength, translation, frameStart) {
return plotLength / realLength * translation + frameStart;
}
Transform.xToPlotCoordinate = function(width, xmax, xmin, xval, frameStart, frameEnd ) {
return Transform.translationToPlotCoordinate(width - frameStart - frameEnd, xmax - xmin, xval - xmin, frameStart);
}
Transform.yToPlotCoordinate = function(height, ymax, ymin, yval , frameStart, frameEnd ) {
return Transform.translationToPlotCoordinate(height - frameStart - frameEnd, ymax - ymin, ymax - yval, frameStart);
}
Transform.prototype.xCoordinate = function(xval) {
return Transform.xToPlotCoordinate(this.width, this.maxX, this.minX, xval, this.frameX, this.frameX);
}
Transform.prototype.yCoordinate = function(yval) {
return Transform.yToPlotCoordinate(this.height, this.maxY, this.minY, yval, this.frameY, this.frameY);
}
/*******************************************/
function transformToPlotCoordinates(plotLength, realLength, translation, frameStart ) {
return plotLength / realLength * translation + frameStart;
}
function transformX(width, xmax, xmin, xval, frameStart, frameEnd ) {
return transformToPlotCoordinates (width - frameStart - frameEnd, xmax - xmin, xval - xmin, frameStart);
}
function transformY(height, ymax, ymin, yval , frameStart, frameEnd ) {
return transformToPlotCoordinates (height - frameStart - frameEnd, ymax - ymin, ymax - yval, frameStart);
}
function transformArrayToCoord(array, canvasWidth, transform, frameStart, frameEnd) {
var vmin = Numerics.getMinOfArray(array);
var vmax = Numerics.getMaxOfArray(array);
var coordArray = [];
for (i in array) {
coordArray.push(transform(canvasWidth, vmax, vmin, array[i], frameStart, frameEnd));
}
return coordArray
}
function transformXArrayToCoord(xRealArray, canvasWidth) {
var xmin = Numerics.getMinOfArray(xRealArray);
var xmax = Numerics.getMaxOfArray(xRealArray);
var xCoordArray = [];
for (i in xRealArray) {
xCoordArray.push(transformX(canvasWidth, xmax, xmin, xRealArray[i]));
}
return xCoordArray
}
function transformYArrayToCoord(yRealArray, canvasHeight) {
var ymin = Numerics.getMinOfArray(yRealArray);
var ymax = Numerics.getMaxOfArray(yRealArray);
var yCoordArray = [];
for (i in yRealArray) {
yCoordArray.push(transformY(canvasHeight, ymax, ymin, yRealArray[i]));
}
return yCoordArray
}