-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
175 lines (146 loc) · 6.55 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html>
<head>
<title>sketch2math</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.css"
integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Inconsolata&display=swap" rel="stylesheet">
<script type="text/javascript" src="js/paper-full.min.js"></script>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script src="https://mauriciopoppe.github.io/function-plot/js/function-plot.js"></script>
<script src="https://www.desmos.com/api/v1.3/calculator.js?apiKey=dcb31709b452b1cf9dc26972add0fda6"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.js"
integrity="sha384-9Nhn55MVVN0/4OFx7EE5kpFBPsEMZxKTCnA+4fqDmg12eCTqGi6+BB2LjY8brQxJ" crossorigin="anonymous">
</script>
<script type="text/paperscript" canvas="myCanvas">
var currentPath;
var paths = [];
var segments = [];
var eqSet = [];
tool.minDistance = 20;
function onMouseDown(event) {
currentPath = new Path();
currentPath.strokeColor = 'white';
}
function onMouseUp(event) {
paths.push(currentPath);
segments = segments.concat(getLineSegment(currentPath));
eqSet = [];
for (var i = 0; i < segments.length; i++){
eq = segmentToEquation(segments[i])
eqSet.push(eq.substring(0, eq.length - 2));
calculator.setExpression({id:'graph' + i.toString(), latex:eq});
}
var finalEquation = ""
for (var j = 0; j < eqSet.length; j++){
if (j == eqSet.length - 1){
finalEquation += "\\left(" + eqSet[j] + "-1\\right)";
}
else{
finalEquation += "\\left(" + eqSet[j] + "-1\\right)" + "*";
}
}
finalEquation += "=0"
console.log(finalEquation);
katex.render(finalEquation , document.getElementById("katex"), {
throwOnError: false
});
}
function onMouseDrag(event) {
currentPath.add(event.point);
}
function getLineSegment(path) {
var segments = path._segments;
var lineSegments = [];
for (var i = 1; i < segments.length; i++) {
lineSegments.push({
"x1": (segments[i-1]._point._x / 100) - 5,
"y1": (-segments[i-1]._point._y / 100) + 5,
"x2": (segments[i]._point._x / 100) - 5,
"y2": (-segments[i]._point._y / 100) + 5
})
}
return lineSegments;
}
function segmentToEquation(segment) {
var midX = ((segment.x1 + segment.x2) / 2).toFixed(3);
var midY = ((segment.y1 + segment.y2) / 2).toFixed(3);
var distance = (getDistance(segment)).toFixed(3);
var _distance = (distance / 2).toFixed(3);
var cos = ((segment.x1 - segment.x2) / distance).toFixed(3);
var sin = ((segment.y1 - segment.y2) / distance).toFixed(3);
if (cos < 0.0011 && cos > 0) {cos = 0.0001;};
if (sin < 0.0011 && sin > 0) {cos = 0.0001;};
if (cos > -0.0011 && cos < 0) {cos = -0.0001;};
if (sin > -0.0011 && sin < 0) {cos = -0.0001;};
var p1 = "\\frac{\\left(\\left(x-" + midX.toString();
var p2 = "\\right)(" + cos.toString();
var p3 = ")+\\left(y-" + midY.toString();
var p4 = "\\right)(" + sin.toString();
var p5 = ")\\right)^2}{" + _distance.toString();
var p6 = "^2}+\\frac{\\left(\\left(x-" + midX.toString();
var p7 = "\\right)(" + sin.toString();
var p8 = ")-\\left(y-" + midY.toString();
var p9 = "\\right)(" + cos.toString();
var p10 = ")\\right)^2}{0.001^2}=1"
var eq = p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10;
return eq;
}
function getDistance(segment){
return Math.sqrt((segment.x2 - segment.x1) * (segment.x2 - segment.x1) + (segment.y2 - segment.y1) * (segment.y2 - segment.y1));
}
var elt = document.getElementById('calculator');
var calculator = Desmos.GraphingCalculator(elt, {expressions: false});
</script>
</head>
<body style="background-color: #f7f6f1;">
<div id="title">
<p>sketch2math - convert any sketch to a single mathematical equation
<br>
draw on the canvas below to get started - <a href="https://github.com/jtguibas/sketch2math">github</a></p>
</div>
<div class="container">
<div class="row">
<div class="col-6">
<h6>Canvas</h6>
<canvas id="myCanvas" resize></canvas>
</div>
<div class="col-6">
<h6>Graph <a style="font-size: 10px">(If you zoom-in enough, you'll see that they are, in fact, ellipses)</a></h6>
<div id="calculator" style="width: 600px; height: 600px;"></div>
</div>
</div>
<br>
<button class="btn btn-secondary" onclick="showEquation()">show/hide equation </button>
<button class="btn btn-secondary" onclick="document.location.reload()">clear canvas</button>
<br><br>
<h6 id="katex" style='display: block;'></h6>
</div>
<footer class="footer">
<div class="container">
<span class="text-muted">The full LaTeX equation is printed in the console.</span>
</div>
</footer>
</body>
<script>
function showEquation() {
var el = document.getElementById("katex");
if (el.style.display == "none") {
el.style.display = "block";
} else {
el.style.display = "none";
}
}
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-142526587-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-142526587-1');
</script>
</html>