quadratic-vis.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. 'use strict';
  2. /* global variables */
  3. var STRETCH_X = 3;
  4. var STRETCH_Y = 0.5;
  5. var X_MIN = -10;
  6. var X_MAX = +10;
  7. var Y_MIN = -10;
  8. var Y_MAX = +10;
  9. var X_OFFSET = -128;
  10. var Y_OFFSET = 256;
  11. var INITIAL_RADIUS = 20;
  12. var POINT_RADIUS = 5;
  13. /*******************************************************************/
  14. /* Graphics */
  15. /*******************************************************************/
  16. /**
  17. * Calculates coordinates from worldspace to screenspace
  18. * @param {Number} x the coordinate you want to transform
  19. * @param {bool} isX true iff x is a x-coordinate, otherwise false
  20. * @return {Number} transformed coordinate
  21. */
  22. function c(x, isX) {
  23. if (isX) {
  24. return STRETCH_X * (x - X_OFFSET);
  25. }
  26. return STRETCH_Y * (-x + Y_OFFSET);
  27. }
  28. /**
  29. * Calculates coordinates from screenspace to worldspace
  30. * @param {Number} x the coordinate you want to transform
  31. * @param {bool} isX true iff x is a x-coordinate, otherwise false
  32. * @return {Number} transformed coordinate
  33. */
  34. function r(x, isX) {
  35. if (isX) {
  36. return (x / STRETCH_X) + X_OFFSET;
  37. }
  38. return (-x / STRETCH_Y) + Y_OFFSET;
  39. }
  40. function setCursorByID(id,cursorStyle) {
  41. var elem;
  42. if (document.getElementById &&
  43. (elem=document.getElementById(id)) ) {
  44. if (elem.style) elem.style.cursor=cursorStyle;
  45. }
  46. }
  47. function drawEllipse(centerX, centerY, width, height) {
  48. context.beginPath() ;
  49. var x = centerX;
  50. var y = centerY;
  51. var rx = width;
  52. var ry = height;
  53. var rotation = 0; // The rotation of the ellipse (in radians)
  54. var start = 0; // The start angle (in radians)
  55. var end = 2 * Math.PI; // The end angle (in radians)
  56. var anticlockwise = false;
  57. context.ellipse(x, y, rx, ry, rotation, start, end, anticlockwise);
  58. context.fillStyle = "rgba(255, 0, 0, 0.5)";
  59. context.fill();
  60. }
  61. function getColor(i, transparency) {
  62. //var t = (i+1)*(360/k);
  63. //var color = 'hsla('+t+', 100%, 50%, '+transparency+')';
  64. var x = i / 256;
  65. if (x > 1) {x = 1.0;}
  66. x = parseInt(x*255);
  67. var color = 'rgba('+x+','+x+','+x+','+transparency+')';
  68. return color;
  69. }
  70. function drawFunction(canvas) {
  71. var add = parseInt(document.getElementById("density").value);
  72. context.beginPath();
  73. context.fillStyle = 'red';
  74. context.moveTo(0, c(getValue(r(0))), false);
  75. for (var xS=0; xS < canvas.width; xS+=add) {
  76. var x = r(xS);
  77. var y = getValue(x);
  78. context.lineTo(c(x, true), c(y, false));
  79. }
  80. context.closePath();
  81. context.stroke();
  82. }
  83. /*******************************************************************/
  84. /* Math */
  85. /*******************************************************************/
  86. function euklideanDist(p1, p2) {
  87. return Math.sqrt(
  88. Math.pow(p1["x"]-p2["x"], 2)
  89. + Math.pow(p1["y"]-p2["y"], 2));
  90. }
  91. /**
  92. * Calculates the value of a cubic function at x
  93. * @param {Number} x
  94. * @return {Number} f(x)
  95. */
  96. function getValue(x) {
  97. var a = parseFloat(document.getElementById("a").value);
  98. var b = parseFloat(document.getElementById("b").value);
  99. var c1 = parseFloat(document.getElementById("c").value);
  100. return a*x*x+b*x+c1;
  101. }
  102. /**
  103. * Calculates the drivate f'(x)
  104. * @param {Number} x
  105. * @return {Number} f'(x)
  106. */
  107. function getDValue(x) {
  108. var a = parseFloat(document.getElementById("a").value);
  109. var b = parseFloat(document.getElementById("b").value);
  110. return 2*a*x+b;
  111. }
  112. /**
  113. * Calculates the drivate f''(x)
  114. * @param {Number} x
  115. * @return {Number} f''(x)
  116. */
  117. function getDDValue(x) {
  118. var a = parseFloat(document.getElementById("a").value);
  119. return 2*a;
  120. }
  121. /**
  122. * Calculates (f(x)^2)' = ((a*x*x+b*x+c)^2)' = 2 (b + 2 a x) (c + x (b + a x))
  123. * @param {Number} x
  124. * @return {Number} (f(x)^2)'
  125. */
  126. function gedSquaredValueD(x) {
  127. var a = parseFloat(document.getElementById("a").value);
  128. var b = parseFloat(document.getElementById("b").value);
  129. var c1 = parseFloat(document.getElementById("c").value);
  130. return 2*(2*a*x+b)*(x*(a*x+b)+c1);
  131. }
  132. /**
  133. * Calculates (f(x)^2)'' = ((a*x*x+b*x+c)^2)'' = 2 (b^2 + 6 a b x + 2 a (c + 3 a x^2))
  134. * @param {Number} x
  135. * @return {Number} (f(x)^2)''
  136. */
  137. function gedSquaredValueDD(x) {
  138. var a = parseFloat(document.getElementById("a").value);
  139. var b = parseFloat(document.getElementById("b").value);
  140. var c1 = parseFloat(document.getElementById("c").value);
  141. return 2*(b*b+6*a*b*x+2*a*(c1+3*a*x*x));
  142. }
  143. function findMin(p) {
  144. var a = parseFloat(document.getElementById("a").value);
  145. var b = parseFloat(document.getElementById("b").value);
  146. var c1 = parseFloat(document.getElementById("c").value);
  147. /* old iterative solution that had problems near the center*/
  148. var lastx = -10000;
  149. var x = p.x;
  150. var i = 0;
  151. while (Math.abs(lastx-x) > 1 && i < 100) {
  152. // first derivate of the square of the distance function
  153. var fx = -2.0*p.x+2.0*x-2.0*p.y*getDValue(x) +2*getValue(x)*getDValue(x);
  154. var fxd = 2.0 -2.0*p.y*getDDValue(x)+2*(getDValue(x)*getDValue(x) + getValue(x)*getDDValue(x));
  155. if (fxd == 0) {
  156. console.log("wow!");
  157. return x;
  158. }
  159. // x_{n+1} = x_n - f(x_n)/f'(x_n) wenn x gesucht, sodass f(x) = 0 gilt
  160. lastx = x;
  161. x -= fx / fxd;
  162. i++;
  163. }
  164. // New direct solution
  165. /*var xs = -b / (2*a);
  166. var w = p.y + b*b/(4*a)-c1;
  167. var z = p.x + b / (2*a);
  168. var alpha = (1-2*a*w)/(2*a*a);
  169. var beta = -z/(2*a*a);
  170. var t = Math.pow(Math.pow(3*(4*Math.pow(alpha,3) +27* Math.pow(beta,2)),1/2) - 9*beta, 1/3);
  171. var currentMinX = t / (Math.pow(18, 1/3)) - Math.pow(2/3*alpha, 1/3) / t;
  172. */
  173. return x;
  174. }
  175. function getDist(p, minX) {
  176. var minY = getValue(minX);
  177. return euklideanDist({"x":minX, "y":minY}, {"x":p.x, "y":p.y});
  178. }
  179. /*******************************************************************/
  180. /* Start / Update */
  181. /*******************************************************************/
  182. function drawBoard(canvas, mouseCoords, radius) {
  183. var context = canvas.getContext('2d');
  184. context.canvas.width = window.innerWidth - 50;
  185. context.canvas.height = window.innerHeight - 120;
  186. context.clearRect(0, 0, canvas.width, canvas.height);
  187. drawFunction(canvas);
  188. if (document.getElementById("pDistance").checked) {
  189. var add = parseInt(document.getElementById("density").value)+10;
  190. for (var x=0; x < canvas.width; x+=add) {
  191. for (var y=0; y < canvas.height; y+=add) {
  192. var dist = getDist({"x":r(x,true), "y":r(y,false)}, findMin({"x":r(x,true), "y":r(y,false)}));
  193. context.fillStyle = getColor(dist,0.5);
  194. context.fillRect(x, y, add/2, add/2);
  195. }
  196. }
  197. }
  198. }
  199. function updateBoard(){
  200. var canvas = document.getElementById("myCanvas");
  201. STRETCH_X = parseFloat(document.getElementById("STRETCH_X").value);
  202. STRETCH_Y = parseFloat(document.getElementById("STRETCH_Y").value);
  203. X_OFFSET = parseFloat(document.getElementById("X_OFFSET").value);
  204. Y_OFFSET = parseFloat(document.getElementById("Y_OFFSET").value);
  205. drawBoard(canvas, {"x":0,"y":0}, INITIAL_RADIUS);
  206. }
  207. var canvas = document.getElementById("myCanvas");
  208. var context = canvas.getContext("2d");
  209. drawBoard(canvas, {"x":0,"y":0}, INITIAL_RADIUS);
  210. setCursorByID("myCanvas", "crosshair");
  211. /** get the current position of the mouse */
  212. function getMouseCoords(canvas, evt) {
  213. var rect = canvas.getBoundingClientRect();
  214. return {
  215. "x": evt.clientX - rect.left,
  216. "y": evt.clientY - rect.top
  217. };
  218. }
  219. /** event listeners */
  220. canvas.addEventListener('mousemove',
  221. function (evt) {
  222. var mouseCoords = getMouseCoords(canvas, evt);
  223. drawBoard(canvas, mouseCoords, 10);
  224. // draw coordinates next to mouse
  225. context.fillStyle = "blue";
  226. context.font = "bold 16px Arial";
  227. var x = r(mouseCoords.x, true).toFixed(3);
  228. var y = r(mouseCoords.y, false).toFixed(3);
  229. context.fillText("(" + x + ", " + y + ")", mouseCoords.x + 5, mouseCoords.y - 5);
  230. var minX = findMin({"x": r(mouseCoords.x,true), "y": r(mouseCoords.y,false)});
  231. var minY = getValue(minX);
  232. context.beginPath();
  233. context.moveTo(c(minX,true), c(minY, false));
  234. context.lineTo(mouseCoords.x, mouseCoords.y, false);
  235. context.stroke();
  236. var minRadius = euklideanDist({"x": r(mouseCoords.x,true), "y": r(mouseCoords.y,false)}, {"x":minX,"y":minY});
  237. /* Draw circle */
  238. drawEllipse(mouseCoords.x, mouseCoords.y, minRadius*STRETCH_X, minRadius*STRETCH_Y);
  239. }, false);