Advertisement
filipao223

ficha2_mult.js

Feb 21st, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. (function()
  4. {
  5.     //automatically called as soon as the javascript is loaded
  6.     window.addEventListener("load", main);
  7. }());
  8.  
  9.  
  10. function main()
  11. {
  12.     var canvas = document.getElementById("myCanvas");
  13.     var ctx = canvas.getContext("2d");
  14.     var cw = canvas.width;
  15.     var ch = canvas.height;
  16.  
  17.     /* add your code */
  18.     var i, n=15;
  19.     var lista = [];
  20.     for( i=0; i<n; i++){
  21.         var radius = Math.floor((Math.random()*50)+5);
  22.         var shape = Math.floor((Math.random()*10) + 1)
  23.         /*console.log(shape);*/
  24.         if(shape<=5){
  25.             /*Rectangulo*/
  26.             var w = Math.floor((Math.random()*75) + 1);
  27.             var h = Math.floor((Math.random()*75) + 1);
  28.             var x = Math.floor((Math.random()* (cw-w)) + 1);
  29.             var y = Math.floor((Math.random()*(ch-h)) + 1);
  30.             var rect = new Rectangle(x,y,w,h);
  31.             rect.draw(ctx);
  32.             lista.push(rect);
  33.         }
  34.         else{
  35.             var x = Math.floor((Math.random()*(cw-radius)) + radius);
  36.             var y = Math.floor((Math.random()*(ch-radius)) + radius);
  37.             if(y+radius>ch) y = y-radius;
  38.             if(x+radius>cw) x = x-radius;
  39.             var circle = new Circle(x,y,radius);
  40.             circle.draw(ctx);
  41.             lista.push(circle);
  42.         }
  43.     }
  44.  
  45.     intersect(lista);
  46. }
  47.  
  48. function intersect()
  49. {
  50.     var i,j;
  51.     var x1,y1,x2,y2;
  52.     var w1,w2,h1,h2;
  53.     var temp = [];
  54.     var total_intersect = 0, total_inclusion=0;
  55.     for(i=0; i<arguments[0].length; i++){
  56.         /*Circulo*/
  57.         if(typeof arguments[0][i].w == 'undefined'){
  58.             x1 = arguments[0][i].x - arguments[0][i].r;
  59.             y1 = arguments[0][i].y - arguments[0][i].r;
  60.             w1 = 2*arguments[0][i].r;
  61.             h1 = w1;
  62.             temp = intersectUtil(arguments[0],i,x1,y1,w1,h1);
  63.             total_intersect+=temp[0];
  64.             total_inclusion+=temp[1];
  65.         }
  66.         /*Não é circulo*/
  67.         else{
  68.             x1 = arguments[0][i].x;
  69.             y1 = arguments[0][i].y;
  70.             w1 = arguments[0][i].w;
  71.             h1 = arguments[0][i].h;
  72.             temp = intersectUtil(arguments[0],i,x1,y1,w1,h1);
  73.             total_intersect+=temp[0];
  74.             total_inclusion+=temp[1];
  75.         }
  76.     }
  77.     console.log("Numero de interseçoes: " + total_intersect);
  78.     console.log("Numero de inclusoes: " + total_inclusion);
  79.  
  80. }
  81.  
  82. function intersectUtil()
  83. {
  84.     var i = arguments[1], j=0;
  85.     var x1 = arguments[2], y1 = arguments[3], w1 = arguments[4], h1 = arguments[5];
  86.     var x2,y2,w2,h2;
  87.     var total_intersect=0,total_inclusion=0;
  88.     var total=[];
  89.     for(j=(i+1); j<arguments[0].length; j++){
  90.         /*Circulo*/
  91.         if(typeof arguments[0][j].w == 'undefined'){
  92.             x2 = arguments[0][j].x - arguments[0][j].r;
  93.             y2 = arguments[0][j].y - arguments[0][j].r;
  94.             w2 = 2*arguments[0][j].r;
  95.             h2 = w2;
  96.         }
  97.         /*Não é circulo*/
  98.         else{
  99.             x2 = arguments[0][j].x;
  100.             y2 = arguments[0][j].y;
  101.             w2 = arguments[0][j].w;
  102.             h2 = arguments[0][j].h;
  103.         }
  104.  
  105.         if(((x1>x2) && (y1>y2) && (x1+w1<x2+w2) && (y1+h1<y2+h2))){
  106.             total_inclusion++;
  107.         }
  108.         /*Para verificar a inclusao das duas figuras*/
  109.         else if(((x2>x1) && (y2>y1) && (x2+w2<x1+w1) && (y2+h2<y1+h1))){
  110.             total_inclusion++;
  111.         }
  112.         /*Não estão incluidas, mas ainda podem intersetar*/
  113.         else if(!((x1>x2+w2) || (y1>y2+h2) || (x1+w1<x2) || (y1+h1<y2))){
  114.             total_intersect++;
  115.         }
  116.     }
  117.     total.push(total_intersect);
  118.     total.push(total_inclusion);
  119.     return total;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement