-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph.html
159 lines (129 loc) · 4.2 KB
/
graph.html
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
<html>
<head>
<title>Foxduino</title>
<link rel="stylesheet" href="styles/general.css">
<!--<script type="text/javascript" src="library/invierteme.js"></script>-->
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro' rel='stylesheet' type='text/css'>
</head>
<body class="width480px" onload="recalc()">
<header>Foxduino
</header>
<div class="subtitle">Graph
</div>
<div class="content">
<canvas class="A" id="plot" height="150">
<p>Your browser doesn't support canvas.</p>
</canvas>
<form name="funf">
<input type = checkbox name = markers onclick="recalc(0)"><span style="font-size:10px;">See Markers </span>
</form>
<center><input type="button" value="Get new data" onclick="recalc(1)"></center>
<script type="text/javascript">
function rand(l,u) // lower bound and upper bound
{
return Math.floor((Math.random() * (u-l+1))+l);
}
/*this is the main function, updates points and options*/
function recalc(what)
{
var points = [];
var fun = "ring";
if (what==1)
{
var ran = rand(0,4);
/*function*/
if (ran==0) fun = "ring";
if (ran==1) fun = "x3";
if (ran==2) fun = "sin";
if (ran==3) fun = "cos";
if (ran==4) fun = "x2";
}
/*number of points*/
var num = 34;
dx = 2.0/(num-1);
/*evaluate function*/
for (x=-1,i=0;i<=num;i++)
{
if (fun=="x2")
y=x*x;
else if (fun=="x3")
y=x*x*x;
else if (fun=="ring")
y=0.02*Math.exp(-4*x)*Math.sin(10*Math.PI*x);
else if (fun=="sin")
y = Math.sin(Math.PI*x);
else if (fun=="cos")
y = Math.cos(Math.PI*x);
points.push([x,y]);
x+=dx;
}
/*draw markers?*/
markers=false; /*default*/
if (document.funf.markers.checked)
markers=true;
draw(points, markers);
}
/*this function does the actual drawing*/
function draw(points, markers)
{
var canvas = document.getElementById('plot');
var i, x, y;
if(canvas.getContext)
{
var scale=100;
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, 250, 150);
ctx.strokeStyle = "#000000";
ctx.strokeRect(0, 0, 250, 150);
// draw points
ctx.save();
ctx.translate(100,100);
ctx.scale(1.0, -1.0);
ctx.strokeStyle = "#FF0000";
ctx.lineWidth = 3;
ctx.beginPath();
if(points.length > 0)
{
x = points[0][0]*scale;
y = points[0][1]*scale;
ctx.moveTo(x, y);
for(i = 0; i < points.length; i += 1)
{
x = points[i][0]*scale;
y = points[i][1]*scale;
ctx.lineTo(x, y);
}
ctx.stroke();
if (markers)
{
ctx.fillStyle="#009900";
for(i = 0; i < points.length; i += 1)
{
x = points[i][0]*scale;
y = points[i][1]*scale;
ctx.beginPath();
ctx.arc(x, y, 5, 0, 2*Math.PI, false);
ctx.fill();
ctx.stroke();
}
}
}
ctx.restore();
ctx.font = 'italic 20px sans-serif';
ctx.textBaseline = 'top';
}
}
</script>
</div>
<div style="height:64px;"></div>
<footer style="margin-left:-7px; bottom:0px; position:fixed; background-color:#000000; width:100%; height:64px; padding:5px; ">
<center><table>
<tr><td width="120" style="font-size:8px; text-align:center;"><a href="index.html"><img src="img/connect.png" width="26"/></a></td>
<td width="120" style="font-size:8px; text-align:center;"> <a href="controls.html"><img src="img/controls.png" width="26"/></a></td>
<td width="120" style="font-size:8px; text-align:center;"><a href="serial.html"><img src="img/serial.png" width="26"/></a></td>
<td width="120" style="font-size:8px; text-align:center;"><a href="graph.html"><img src="img/graph.png" width="26"/></a></td></tr>
<tr><td width="120" style="font-size:8px; text-align:center;">Connect</td><td width="120" style="font-size:8px; text-align:center;">Controls</td><td width="120" style="font-size:8px; text-align:center;">Serial</td><td width="120" style="font-size:8px; text-align:center;">Graph</td></tr></table></center>
<center><div style="margin-top:7px;font-size:10px;color:#DDDDDD;">Foxduino by <a href="mailto:terodelarosa@gmail.com">Tero</a>&<a href="mailto:enriquemendozarobaina@gmail.com">Enri</div></center>
</footer>
</body>
</html>