ExMandelbrot.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Copyright (c) 2011-2018 by Andrew Mustun. All rights reserved.
  3. *
  4. * This file is part of the QCAD project.
  5. *
  6. * QCAD is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * QCAD is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with QCAD.
  18. */
  19. include("scripts/EAction.js");
  20. include("scripts/simple.js");
  21. /**
  22. * \ingroup ecma_misc_examples_mathexamples
  23. * \class ExMandelbrot
  24. * This action plots the Mandelbrot set.
  25. */
  26. function ExMandelbrot(guiAction) {
  27. EAction.call(this, guiAction);
  28. }
  29. ExMandelbrot.prototype = new EAction();
  30. /**
  31. * Draws Mandelbrot set.
  32. */
  33. ExMandelbrot.prototype.beginEvent = function() {
  34. EAction.prototype.beginEvent.call(this);
  35. startTransaction(this.getDocumentInterface());
  36. mandelbrot(-2, -1, 1, 1, 100);
  37. endTransaction();
  38. this.terminate();
  39. };
  40. ExMandelbrot.init = function(basePath) {
  41. var action = new RGuiAction(qsTr("&Mandelbrot Example"), RMainWindowQt.getMainWindow());
  42. action.setRequiresDocument(true);
  43. action.setScriptFile(basePath + "/ExMandelbrot.js");
  44. action.setGroupSortOrder(79700);
  45. action.setSortOrder(200);
  46. action.setWidgetNames(["MathExamplesMenu"]);
  47. };
  48. function mandelIter(cx, cy, maxIter) {
  49. var x = 0.0;
  50. var y = 0.0;
  51. var xx = 0;
  52. var yy = 0;
  53. var xy = 0;
  54. var i = maxIter;
  55. while (i-- && xx + yy <= 4) {
  56. xy = x * y;
  57. xx = x * x;
  58. yy = y * y;
  59. x = xx - yy + cx;
  60. y = xy + xy + cy;
  61. }
  62. return maxIter - i;
  63. }
  64. function mandelbrot(xmin, ymin, xmax, ymax, iterations) {
  65. var r,g,b;
  66. var res = 0.01;
  67. var factor=5;
  68. for (var x = xmin; x < xmax; x+=res) {
  69. qDebug(x);
  70. for (var y = ymin; y < ymax; y+=res) {
  71. var i = mandelIter(x, y, iterations);
  72. if (i > iterations) {
  73. r = 0;
  74. g = 0;
  75. b = 0;
  76. } else {
  77. var c = 3 * Math.log(i) / Math.log(iterations - 1.0);
  78. if (c < 1) {
  79. r = 255 * c;
  80. g = 0;
  81. b = 0;
  82. }
  83. else if ( c < 2 ) {
  84. r = 255;
  85. g = 255 * (c - 1);
  86. b = 0;
  87. } else {
  88. r = 255;
  89. g = 255;
  90. b = 255 * (c - 2);
  91. }
  92. }
  93. var color = new RColor(r,g,b);
  94. var pt = new RPoint(new RVector(x*factor, y*factor, 0));
  95. addShape(pt, color, "CONTINUOUS", RLineweight.Weight005);
  96. }
  97. }
  98. }