constant-vis.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 constant function at x
  93. * @param {Number} x
  94. * @return {Number} f(x)
  95. */
  96. function getValue(x) {
  97. var c1 = parseFloat(document.getElementById("c").value);
  98. return c1;
  99. }
  100. /**
  101. * Calculates the drivate f'(x)
  102. * @param {Number} x
  103. * @return {Number} f'(x)
  104. */
  105. function getDValue(x) {
  106. var a = parseFloat(document.getElementById("a").value);
  107. var b = parseFloat(document.getElementById("b").value);
  108. return 2*a*x+b;
  109. }
  110. /**
  111. * Calculates the drivate f''(x)
  112. * @param {Number} x
  113. * @return {Number} f''(x)
  114. */
  115. function getDDValue(x) {
  116. var a = parseFloat(document.getElementById("a").value);
  117. return 2*a;
  118. }
  119. /**
  120. * Calculates (f(x)^2)' = ((a*x*x+b*x+c)^2)' = 2 (b + 2 a x) (c + x (b + a x))
  121. * @param {Number} x
  122. * @return {Number} (f(x)^2)'
  123. */
  124. function gedSquaredValueD(x) {
  125. var a = parseFloat(document.getElementById("a").value);
  126. var b = parseFloat(document.getElementById("b").value);
  127. var c1 = parseFloat(document.getElementById("c").value);
  128. return 2*(2*a*x+b)*(x*(a*x+b)+c1);
  129. }
  130. /**
  131. * 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))
  132. * @param {Number} x
  133. * @return {Number} (f(x)^2)''
  134. */
  135. function gedSquaredValueDD(x) {
  136. var a = parseFloat(document.getElementById("a").value);
  137. var b = parseFloat(document.getElementById("b").value);
  138. var c1 = parseFloat(document.getElementById("c").value);
  139. return 2*(b*b+6*a*b*x+2*a*(c1+3*a*x*x));
  140. }
  141. function findMin(p) {
  142. return p.x;
  143. }
  144. /*******************************************************************/
  145. /* Start / Update */
  146. /*******************************************************************/
  147. function drawBoard(canvas, mouseCoords, radius) {
  148. var context = canvas.getContext('2d');
  149. context.canvas.width = window.innerWidth - 50;
  150. context.canvas.height = window.innerHeight - 120;
  151. context.clearRect(0, 0, canvas.width, canvas.height);
  152. drawFunction(canvas);
  153. if (document.getElementById("pDistance").checked) {
  154. var add = parseInt(document.getElementById("density").value)+10;
  155. for (var x=0; x < canvas.width; x+=add) {
  156. for (var y=0; y < canvas.height; y+=add) {
  157. var dist = getDist({"x":r(x,true), "y":r(y,false)}, findMin({"x":r(x,true), "y":r(y,false)}));
  158. context.fillStyle = getColor(dist,0.5);
  159. context.fillRect(x, y, add/2, add/2);
  160. }
  161. }
  162. }
  163. }
  164. function updateBoard(){
  165. var canvas = document.getElementById("myCanvas");
  166. STRETCH_X = parseFloat(document.getElementById("STRETCH_X").value);
  167. STRETCH_Y = parseFloat(document.getElementById("STRETCH_Y").value);
  168. X_OFFSET = parseFloat(document.getElementById("X_OFFSET").value);
  169. Y_OFFSET = parseFloat(document.getElementById("Y_OFFSET").value);
  170. drawBoard(canvas, {"x":0,"y":0}, INITIAL_RADIUS);
  171. }
  172. var canvas = document.getElementById("myCanvas");
  173. var context = canvas.getContext("2d");
  174. drawBoard(canvas, {"x":0,"y":0}, INITIAL_RADIUS);
  175. setCursorByID("myCanvas", "crosshair");
  176. /** get the current position of the mouse */
  177. function getMouseCoords(canvas, evt) {
  178. var rect = canvas.getBoundingClientRect();
  179. return {
  180. "x": evt.clientX - rect.left,
  181. "y": evt.clientY - rect.top
  182. };
  183. }
  184. /** event listeners */
  185. canvas.addEventListener('mousemove',
  186. function (evt) {
  187. var mouseCoords = getMouseCoords(canvas, evt);
  188. drawBoard(canvas, mouseCoords, 10);
  189. // draw coordinates next to mouse
  190. context.fillStyle = "blue";
  191. context.font = "bold 16px Arial";
  192. var x = r(mouseCoords.x, true).toFixed(3);
  193. var y = r(mouseCoords.y, false).toFixed(3);
  194. context.fillText("(" + x + ", " + y + ")", mouseCoords.x + 5, mouseCoords.y - 5);
  195. var minX = findMin({"x": r(mouseCoords.x,true), "y": r(mouseCoords.y,false)});
  196. var minY = getValue(minX);
  197. context.beginPath();
  198. context.moveTo(c(minX,true), c(minY, false));
  199. context.lineTo(mouseCoords.x, mouseCoords.y, false);
  200. context.stroke();
  201. var minRadius = euklideanDist({"x": r(mouseCoords.x,true), "y": r(mouseCoords.y,false)}, {"x":minX,"y":minY});
  202. /* Draw circle */
  203. drawEllipse(mouseCoords.x, mouseCoords.y, minRadius*STRETCH_X, minRadius*STRETCH_Y);
  204. }, false);