My 2d ray tracer has worked fine up until the point where I sorted the calculated rays by angle (radian to be specific). I assume it has to do with the way tan is acting but I am not sure. What is the best way to sort the angles with a known x,y for the collision and for the originating point? I have been working on the same problem on and off for 2 weeks and have tried almost everything.
I can upload pictures now here: Here is the guilty code if you want to fiddle with it:
function sortByAngle(pos){
for (var i = viewFeild.length - 1; i >= 0; i --) {
viewFeild[i][3] = Math.atan((viewFeild[i][4]-pos.y)/(viewFeild[i][0]-pos.x));
if(viewFeild[i][5]<pos.y)
viewFeild[i][6] = viewFeild[i][7]*-1-4;
if (viewFeild[i][8]<0) {viewFeild[i][9]+=2};
};
viewFeild.sort(function(a,b){return a[2]-b[2]});
}
function fillView(pos) {
for (var i = viewFeild.length - 1; i >= 0; i--) {
//console.log(i+" "+viewFeild[i][10] + " " + viewFeild[(i+1)%viewFeild.length][11])
//console.log(viewFeild.length)
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
ctx.lineTo(viewFeild[i][0]+pos.x, viewFeild[i][12]+pos.y);
ctx.lineTo(viewFeild[(i+1)%viewFeild.length][0]+pos.x, viewFeild[(i+1)%viewFeild.length][13]+pos.y);
ctx.closePath();
ctx.fillStyle = "rgba(100, " + 35*i + ", 100, .6)";
ctx.fill();
};
}
Here is google doc with the entire js code and html (html is after the js) https://docs.google.com/document/d/12chxLiaj9gz-irlM0VdZs-BNoNqoMbz5AS0Dm0CpXfI/edit?usp=sharing
First thing to do is to clarify your code.
1) you don't need to fill the array in reverse order.
2) use atan2 - i didn't get your way of dealing with radians...
3) cache the array element that you will re-use.
4) do not create one sort function on each sort.
5) if you sort in the right order, you don't need to display in reverse order.
Once situation is clearer, i find strange that you are using fields 3, 5 or 6 for the y of your point. I would say just one offset for the y data is enough ;-)
function sortByAngle(center) {
for (var i = 0 ; i<viewFeild.length ; i++) {
var thisField = viewFeild[i] ;
thisField[2] = Math.atan2( thisField[3] - center.y)
, (thisField[0] - center.x));
};
viewFeild.sort(sortOnSecondItem);
}
function fillView(pos) {
for (var i = 0 ; i<viewFeild.length ; i++) {
var thisField = viewFeild[i] ;
var nextField = (i==viewFeild.length-1) ?
viewFeild[0]
: viewFeild[i+1] ;
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
ctx.lineTo(thisField[0] + pos.x, thisField[5] + pos.y);
ctx.lineTo(nextField[0] + pos.x, nextField[6] + pos.y);
ctx.closePath();
ctx.fillStyle = "rgba(100, " + 35 * i + ", 100, .6)";
ctx.fill();
};
}
function sortOnSecondItem(a,b) { return a[2] - b[2] }