// Convert degrees to radians. var RAD2DEG = 180 / Math.PI var DEG2RAD = Math.PI / 180 // Simple 2D Vector Math function subtract(a, b){ return [a[0]-b[0], a[1]-b[1]]; } function dotProduct(a, b){ return a[0] * b[0] + a[1] * b[1]; } function crossProduct(a, b){ return a[0] * b[1] - b[0] * a[1] } // Check for the intersection of ray and a line in 2D. function rayLineIntersect(rayOrigin, rayDirection, point1, point2){ v1 = subtract(rayOrigin, point1); v2 = subtract(point2, point1); v3 = [-rayDirection[1], rayDirection[0]]; dot = dotProduct(v2, v3); if (Math.abs(dot) < 0.000001) { return false; } t1 = (crossProduct(v2, v1)) / dot; t2 = dotProduct(v1,v3) / dot; if (t1 >= 0.0 && (t2 >= 0.0 && t2 <= 1.0)){ return true; } return false; } // Perform intersection calculation for a grid of points and set of extrusions in three dimensions. function threeDIntersect(sunVectors, pointGrid, windowExtrusions, shadeExtrusions, timeinterval = 1){ // Create an empty list of results for the point grid. var results = [] for(var i = 0; i < pointGrid.length; i++) { results.push(0) } // Loop through all of the vectors and extrusions to find which points see the sun for (j = 0; j < sunVectors.length; j++) { // Convert the sun vector into a series of 2D rays sunVec = sunVectors[j] xySunVec = [sunVec.x, sunVec.y] yzSunVec = [sunVec.y, sunVec.z] xzSunVec = [sunVec.x, sunVec.z] for (var i = 0; i < pointGrid.length; i++) { rayOriginxy = [pointGrid[i].x,pointGrid[i].y] rayOriginyz = [pointGrid[i].y,pointGrid[i].z] rayOriginxz = [pointGrid[i].x,pointGrid[i].z] for (var k = 0; k < windowExtrusions.length; k++) { xyIntersect = rayLineIntersect(rayOriginxy, xySunVec, windowExtrusions[k].xy[0], windowExtrusions[k].xy[1]) yzIntersect = rayLineIntersect(rayOriginyz, yzSunVec, windowExtrusions[k].yz[0], windowExtrusions[k].yz[1]) if (xyIntersect == true && yzIntersect == true){ results[i] = results[i] + timeinterval } } } } return results }