Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- (function()
- {
- //automatically called as soon as the javascript is loaded
- window.addEventListener("load", main);
- }());
- function main()
- {
- var canvas = document.getElementById("myCanvas");
- var ctx = canvas.getContext("2d");
- var cw = canvas.width;
- var ch = canvas.height;
- /* add your code */
- var i, n=15;
- var lista = [];
- for( i=0; i<n; i++){
- var radius = Math.floor((Math.random()*50)+5);
- var shape = Math.floor((Math.random()*10) + 1)
- /*console.log(shape);*/
- if(shape<=5){
- /*Rectangulo*/
- var w = Math.floor((Math.random()*75) + 1);
- var h = Math.floor((Math.random()*75) + 1);
- var x = Math.floor((Math.random()* (cw-w)) + 1);
- var y = Math.floor((Math.random()*(ch-h)) + 1);
- var rect = new Rectangle(x,y,w,h);
- rect.draw(ctx);
- lista.push(rect);
- }
- else{
- var x = Math.floor((Math.random()*(cw-radius)) + radius);
- var y = Math.floor((Math.random()*(ch-radius)) + radius);
- if(y+radius>ch) y = y-radius;
- if(x+radius>cw) x = x-radius;
- var circle = new Circle(x,y,radius);
- circle.draw(ctx);
- lista.push(circle);
- }
- }
- intersect(lista);
- }
- function intersect()
- {
- var i,j;
- var x1,y1,x2,y2;
- var w1,w2,h1,h2;
- var temp = [];
- var total_intersect = 0, total_inclusion=0;
- for(i=0; i<arguments[0].length; i++){
- /*Circulo*/
- if(typeof arguments[0][i].w == 'undefined'){
- x1 = arguments[0][i].x - arguments[0][i].r;
- y1 = arguments[0][i].y - arguments[0][i].r;
- w1 = 2*arguments[0][i].r;
- h1 = w1;
- temp = intersectUtil(arguments[0],i,x1,y1,w1,h1);
- total_intersect+=temp[0];
- total_inclusion+=temp[1];
- }
- /*Não é circulo*/
- else{
- x1 = arguments[0][i].x;
- y1 = arguments[0][i].y;
- w1 = arguments[0][i].w;
- h1 = arguments[0][i].h;
- temp = intersectUtil(arguments[0],i,x1,y1,w1,h1);
- total_intersect+=temp[0];
- total_inclusion+=temp[1];
- }
- }
- console.log("Numero de interseçoes: " + total_intersect);
- console.log("Numero de inclusoes: " + total_inclusion);
- }
- function intersectUtil()
- {
- var i = arguments[1], j=0;
- var x1 = arguments[2], y1 = arguments[3], w1 = arguments[4], h1 = arguments[5];
- var x2,y2,w2,h2;
- var total_intersect=0,total_inclusion=0;
- var total=[];
- for(j=(i+1); j<arguments[0].length; j++){
- /*Circulo*/
- if(typeof arguments[0][j].w == 'undefined'){
- x2 = arguments[0][j].x - arguments[0][j].r;
- y2 = arguments[0][j].y - arguments[0][j].r;
- w2 = 2*arguments[0][j].r;
- h2 = w2;
- }
- /*Não é circulo*/
- else{
- x2 = arguments[0][j].x;
- y2 = arguments[0][j].y;
- w2 = arguments[0][j].w;
- h2 = arguments[0][j].h;
- }
- if(((x1>x2) && (y1>y2) && (x1+w1<x2+w2) && (y1+h1<y2+h2))){
- total_inclusion++;
- }
- /*Para verificar a inclusao das duas figuras*/
- else if(((x2>x1) && (y2>y1) && (x2+w2<x1+w1) && (y2+h2<y1+h1))){
- total_inclusion++;
- }
- /*Não estão incluidas, mas ainda podem intersetar*/
- else if(!((x1>x2+w2) || (y1>y2+h2) || (x1+w1<x2) || (y1+h1<y2))){
- total_intersect++;
- }
- }
- total.push(total_intersect);
- total.push(total_inclusion);
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement