-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.js
123 lines (106 loc) · 3.86 KB
/
grid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const container = document.getElementById("container");
const width = 19;
const height = 9;
function makeRows(rows, cols) {
container.style.setProperty('--grid-rows', rows);
container.style.setProperty('--grid-cols', cols);
for (c = 0; c < (rows * cols); c++) {
let cell = document.createElement("div");
cell.gridval = 0
cell.onclick = fillCell
container.appendChild(cell).className = "grid-item";
}
}
function fillCell() {
this.gridval = (this.gridval == 0 ? 1 : 0);
this.style.backgroundColor = (this.style.backgroundColor == "black" ? "white": "black");
console.log(this.gridval);
}
function getGrid(){
let cells = document.querySelectorAll('.grid-item');
return Array.prototype.map.call(cells, function(cell) {
return cell.gridval;
})
}
function readGrid(){
let grid = getGrid();
console.log(grid);
//turn grid into binary numbers
// do it old school
let vals = []
for(let i=0; i<grid.length; i+=width){
vals.push(parseInt(grid.slice(i, i+width).join(''), 2));
}
//this is terrible, but it's a formatting hack
//TODO find a better solution
vals.splice(3,0,'\n ')
console.log(vals);
return 'G[]={' + vals.join() + '}'
}
function updateCard(config){
let template = `
#include <stdlib.h> // card > aek.ppm
#include <stdio.h>
#include <math.h>
typedef int i;typedef float f;struct v{
f x,y,z;v operator+(v r){return v(x+r.x
,y+r.y,z+r.z);}v operator*(f r){return
v(x*r,y*r,z*r);}f operator%(v r){return
x*r.x+y*r.y+z*r.z;}v(){}v operator^(v r
){return v(y*r.z-z*r.y,z*r.x-x*r.z,x*r.
y-y*r.x);}v(f a,f b,f c){x=a;y=b;z=c;}v
operator!(){return*this*(1/sqrt(*this%*
this));}};i G[]={247570,280596,280600,
249748,18578,18577,231184,16,16};f R(){
return(f)rand()/RAND_MAX;}i T(v o,v d,f
&t,v&n){t=1e9;i m=0;f p=-o.z/d.z;if(.01
<p)t=p,n=v(0,0,1),m=1;for(i k=19;k--;)
for(i j=9;j--;)if(G[j]&1<<k){v p=o+v(-k
,0,-j-4);f b=p%d,c=p%p-1,q=b*b-c;if(q>0
){f s=-b-sqrt(q);if(s<t&&s>.01)t=s,n=!(
p+d*t),m=2;}}return m;}v S(v o,v d){f t
;v n;i m=T(o,d,t,n);if(!m)return v(.7,
.6,1)*pow(1-d.z,4);v h=o+d*t,l=!(v(9+R(
),9+R(),16)+h*-1),r=d+n*(n%d*-2);f b=l%
n;if(b<0||T(h,l,t,n))b=0;f p=pow(l%r*(b
>0),99);if(m&1){h=h*.2;return((i)(ceil(
h.x)+ceil(h.y))&1?v(3,1,1):v(3,3,3))*(b
*.2+.1);}return v(p,p,p)+S(h,r)*.5;}i
main(){printf("P6 512 512 255 ");v g=!v
(-6,-16,0),a=!(v(0,0,1)^g)*.002,b=!(g^a
)*.002,c=(a+b)*-256+g;for(i y=512;y--;)
for(i x=512;x--;){v p(13,13,13);for(i r
=64;r--;){v t=a*(R()-.5)*99+b*(R()-.5)*
99;p=S(v(17,16,8)+t,!(t*-1+(a*(R()+x)+b
*(y+R())+c)*16))*3.5+p;}printf("%c%c%c"
,(i)p.x,(i)p.y,(i)p.z);}}`
configPattern = /G\[]={.*\n.*}/gm
console.log(template.search(configPattern))
modified = template.replace(/G\[]={.*\n.*}/gm, config);
raytracer = document.getElementById("raytracer");
raytracer.innerHTML = modified;
}
function copyToClipboard(){
var copyText = document.querySelector("#raytracer code").innerText;
navigator.clipboard.writeText(copyText).then(function() {
// alert('Code copied to clipboard!');
}).catch(function(error) {
// alert('Error copying code: ' + error);
});
}
function clearGrid() {
let cells = document.querySelectorAll('.grid-item');
cells.forEach(cell => {
cell.style.backgroundColor = "white"; // Resetting cell color
cell.gridval = 0; // Resetting grid value
});
}
function raytrace(){
}
function main(){
config = readGrid();
console.log(config);
updateCard(config);
raytrace();
}
makeRows(height, width);