let p=[0,0,15],d=[0,0],sc=4,walls=[[20,0,30,10,10,-10,[0,100,255]],[20,0,30,-10,10,-10,[0,100,255]],[40,0,30,10,10,-10,[0,100,255]],[40,0,30,-10,10,-10,[0,100,255]],
[100,-80,100,80,10,-10,[0,255,0]]]
for(let i=0;i<width/sc;i++){
let eye=[Math.cos(d[0])-(i-width/sc/2)/(width/sc/2)*Math.cos(d[0]-Math.PI/2),Math.sin(d[0])-(i-width/sc/2)/(width/sc/2)*Math.sin(d[0]-Math.PI/2)]
for(let j=0;j<walls.length;j++){
let intersect=line2(walls[j],[p[0],p[1],p[0]+eye[0]*10000,p[1]+eye[1]*10000])
let r=1/Math.sqrt(Math.pow(p[0]-intersect[0],2)+Math.pow(p[1]-intersect[1],2))/Math.cos(Math.atan2(i-width/sc/2,width/sc/2))
let angle1=width/2*(p[2]-walls[j][4])*r
let angle2=width/2*(p[2]-walls[j][5])*r
let eye1=[Math.cos(d[0])-(i-1-width/sc/2)/(width/sc/2)*Math.cos(d[0]-Math.PI/2),Math.sin(d[0])-(i-1-width/sc/2)/(width/sc/2)*Math.sin(d[0]-Math.PI/2)],
eye2=[Math.cos(d[0])-(i+1-width/sc/2)/(width/sc/2)*Math.cos(d[0]-Math.PI/2),Math.sin(d[0])-(i+1-width/sc/2)/(width/sc/2)*Math.sin(d[0]-Math.PI/2)],
if(!(line2(walls[j],[p[0],p[1],p[0]+eye1[0]*10000,p[1]+eye1[1]*10000])&&line2(walls[j],[p[0],p[1],p[0]+eye2[0]*10000,p[1]+eye2[1]*10000])))fl=1
bfr.push([Math.sqrt(Math.pow(p[0]-intersect[0],2)+Math.pow(p[1]-intersect[1],2)),angle1,angle2,fl,walls[j][6]])
bfr.sort((a,b)=>b[0]-a[0])
for(let j=0;j<bfr.length;j++){
fill(...bfr[j][4]);if(bfr[j][3])fill(255)
drawSector(i,height/2+bfr[j][1],height/2+bfr[j][2])
drawSector(i,height/2+bfr[j][1],height/2+bfr[j][1]+sc)
drawSector(i,height/2+bfr[j][2]-sc,height/2+bfr[j][2])
noFill();stroke(255);strokeWeight(1);rect(0,0,width,height)
fill(255);noStroke();textSize(15);textAlign(LEFT,TOP);text(fps,10,10)
if(frameCount%5==0)fps=Math.floor(frameRate())
if(keyIsDown(87)){p[0]+=Math.cos(d[0]);p[1]+=Math.sin(d[0])}
if(keyIsDown(83)){p[0]-=Math.cos(d[0]);p[1]-=Math.sin(d[0])}
if(keyIsDown(65)){p[0]-=Math.cos(d[0]+Math.PI/2);p[1]-=Math.sin(d[0]+Math.PI/2)}
if(keyIsDown(68)){p[0]+=Math.cos(d[0]+Math.PI/2);p[1]+=Math.sin(d[0]+Math.PI/2)}
d[0]+=(mouseX-pmouseX)/100
rect(x*sc,Math.round(y1/sc)*sc,sc,Math.round(y2/sc)*sc-Math.round(y1/sc)*sc)