Advertisement
Guest User

Untitled

a guest
Oct 17th, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. (function () {
  2. /*
  3. * Utils
  4. */
  5. function getRandomInt(min, max) {
  6. return Math.floor(Math.random() * (max - min + 1)) + min;
  7. }
  8.  
  9.  
  10. /*
  11. * Class constructors
  12. */
  13. function Point(x, y) {
  14. this.x = x || 0;
  15. this.y = y || 0;
  16.  
  17. this.setToRandomPosition = function (maxX, maxY) {
  18. this.x = getRandomInt(0, maxX);
  19. this.y = getRandomInt(0, maxY);
  20. };
  21. }
  22.  
  23. function Rectangle(options) { //fini Exo1
  24. this.__proto__ = new Shape();
  25.  
  26. this.generatePoints = function (options) {
  27. var pointA = new Point();
  28. pointA.setToRandomPosition(10, 10);
  29. this.points.push(pointA);
  30. var pointB = new Point();
  31. pointB.x = pointA.x+options.maxWidth;
  32. pointB.y = pointA.y;
  33. this.points.push(pointB);
  34. var pointD = new Point();
  35. pointD.x = pointA.x+options.maxWidth;
  36. pointD.y = pointA.y+options.maxHeight;
  37. this.points.push(pointD);
  38. var pointC = new Point();
  39. pointC.x = pointA.x-options.maxWidth;
  40. pointC.y = pointA.y+options.maxHeight;
  41. this.points.push(pointC);
  42. }
  43. }
  44.  
  45. function Square(size) {
  46. return new Rectangle(size, size);
  47. }
  48.  
  49. function Circle(radius) {
  50. this.__proto__ = new Shape();
  51.  
  52. this.draw = function (context) {
  53. context.fillStyle = this.color;
  54. context.beginPath();
  55. context.arc(this.points[0].x, this.points[0].y, radius, 0, 2 * Math.PI, false);
  56. context.fill();
  57. };
  58.  
  59. this.generatePoints = function () {
  60. var c = new Point();
  61. c.setToRandomPosition(1000, 1000);
  62. this.points.push(c)
  63. }
  64.  
  65. // OLD METHOD WITH MULTIPLE POINTS
  66. /*this.generatePoints = function (width, height) {
  67. var step = 2*Math.PI/20; // see note 1
  68. var h = 150;
  69. var k = 150;
  70. var r = 50;
  71. for(var theta=0; theta < 2*Math.PI; theta+=step){
  72. var x = h + r*Math.cos(theta);
  73. var y = k - r*Math.sin(theta);
  74. var point = new Point();
  75. point.x = x;
  76. point.y = y;
  77. this.points.push(point);
  78. }
  79. }*/
  80. }
  81.  
  82. function Shape() {
  83. this.color = 'black';
  84. this.points = [];
  85.  
  86. this.draw = function (context) {
  87. context.fillStyle = this.color;
  88. context.beginPath();
  89.  
  90. //console.log(this.points);
  91.  
  92. this.points.forEach(function (point, index, points) {
  93. if (index === 0) {
  94. context.moveTo(point.x, point.y);
  95.  
  96. } else {
  97. context.lineTo(point.x, point.y);
  98. }
  99.  
  100. if (index === points.length - 1) {
  101. context.fill();
  102. }
  103. });
  104. };
  105.  
  106. this.generatePoints = function (options) {
  107. for (var i = 0; i < options.numberOfPoints; i++) {
  108. var point = new Point();
  109. point.setToRandomPosition(options.maxWidth, options.maxHeight);
  110. this.points.push(point);
  111. }
  112.  
  113. if (options.shouldSort) {
  114. this.sortPoints();
  115. }
  116. };
  117.  
  118. this.sortPoints = function () {
  119. function lineDistance(point1, point2) {
  120. var xs = point2.x - point1.x;
  121. var ys = point2.y - point1.y;
  122.  
  123. xs = xs * xs;
  124. ys = ys * ys;
  125. return Math.sqrt(xs + ys);
  126. }
  127.  
  128. // calculate max and min x and y
  129. var minX = this.points[0].x;
  130. var maxX = this.points[0].x;
  131. var minY = this.points[0].y;
  132. var maxY = this.points[0].y;
  133.  
  134. for (var i = 1; i < this.points.length; i++) {
  135. if (this.points[i].x < minX) minX = this.points[i].x;
  136. if (this.points[i].x > maxX) maxX = this.points[i].x;
  137. if (this.points[i].y < minY) minY = this.points[i].y;
  138. if (this.points[i].y > maxY) maxY = this.points[i].y;
  139. }
  140.  
  141.  
  142. // choose a "central" point
  143. var center = {
  144. x: minX + (maxX - minX) / 2,
  145. y: minY + (maxY - minY) / 2
  146. };
  147.  
  148. // precalculate the angles of each point to avoid multiple calculations on sort
  149. for (var j = 0; j < this.points.length; j++) {
  150. this.points[j].angle = Math.acos((this.points[j].x - center.x) / lineDistance(center, this.points[j]));
  151.  
  152. if (this.points[j].y > center.y) {
  153. this.points[j].angle = Math.PI + Math.PI - this.points[j].angle;
  154. }
  155. }
  156.  
  157. // sort by angle
  158. this.points = this.points.sort(function (a, b) {
  159. return a.angle - b.angle;
  160. });
  161. };
  162. }
  163.  
  164.  
  165. /*
  166. * Core of the app
  167. */
  168. function drawShapeHandler() {
  169. var canvasElement = document.getElementById('draw-area');
  170.  
  171. if (!canvasElement.getContext) {
  172. return;
  173. }
  174.  
  175. var colorInput = document.getElementById('color-input');
  176. var mode = document.querySelector('input[name="mode"]:checked').value;
  177. var ctx = canvasElement.getContext('2d');
  178.  
  179. var shape = createShape(mode, canvasElement);
  180. shape.color = colorInput.value;
  181. shape.draw(ctx, true);
  182. }
  183.  
  184. function createShape(mode, canvas) {
  185. var pointsInput = document.getElementById('points-input');
  186. var isRandomInput = document.getElementById('is-random-input');
  187. var numberOfPoints = parseInt(pointsInput.value);
  188.  
  189. var options = {
  190. maxWidth: canvas.offsetWidth,
  191. maxHeight: canvas.offsetHeight,
  192. numberOfPoints: numberOfPoints,
  193. shouldSort: !isRandomInput.checked
  194. };
  195.  
  196. var mode = document.querySelector('input[name="mode"]:checked').value;
  197.  
  198. var shape;
  199. // Fonctionne mais pas dans le carré d'affichage il faut mettre en gros
  200. switch(mode){
  201. case "rectangle":
  202. options.maxWidth = document.getElementById('rect-width-input').value;
  203. options.maxHeight = document.getElementById('rect-height-input').value;
  204. shape = new Rectangle();
  205. break;
  206. case "square":
  207. shape = new Square();
  208. options.maxWidth = document.getElementById('rect-width-input').value;
  209. break;
  210. case "circle":
  211. shape = new Circle();
  212. break;
  213. default:
  214. shape = new Shape();
  215. break;
  216. }
  217. console.log(shape);
  218. shape.generatePoints(options);
  219.  
  220. return shape;
  221. }
  222.  
  223. document.getElementById('draw-button').addEventListener('click', drawShapeHandler);
  224. })();
  225.  
  226. (function canvasUtils() {
  227. function redraw() {
  228. var canvas = document.getElementById('draw-area');
  229. canvas.width = canvas.parentElement.offsetWidth - 15;
  230. canvas.height = canvas.parentElement.offsetHeight - 15;
  231. }
  232.  
  233. redraw();
  234. window.addEventListener('resize', redraw);
  235.  
  236. function resetCanvas() {
  237. var canvasElement = document.getElementById('draw-area');
  238.  
  239. if (!canvasElement.getContext) {
  240. return;
  241. }
  242. var context = canvasElement.getContext('2d');
  243. context.clearRect(0, 0, canvasElement.width, canvasElement.height);
  244.  
  245. }
  246.  
  247. document.getElementById('reset-button').addEventListener('click', resetCanvas);
  248. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement