Reputation: 45
public static ArrayList<Double> Cast(double angle,double step,ArrayList<Double> list){
double x = px + Math.sin(angle/180*Math.PI)*step;
double z = pz + Math.cos(angle/180*Math.PI)*step;
if((int)x<0||(int)x>mapWidth-1||(int)z<0||(int)z>mapHeight-1){
return list;
}else{
step+=quality;
list.add(getHeight(x,z));
return Cast(angle,step,list);
}
}
I am making a raycaster and this function return height of (direction,distance). As I need to cast almost fov/screenWidth times, this is very expensive for my application. How can I make dynamic array faster with recursion function?
Upvotes: 0
Views: 95
Reputation: 1678
Recursion functions aren't efficient, althought they resolve difficult problems in an easy way.
When you need to improve performance of a recursion function you'll probably get the best result implementing it iteratively (if possible).
Hope this piece of code helps you!
public static List<Double> cast(double angle, double step, List<Double> list) {
while(true) {
double x = px + Math.sin(angle / 180 * Math.PI) * step;
double z = pz + Math.cos(angle / 180 * Math.PI) * step;
if ((int) x < 0 || (int) x > mapWidth - 1 || (int) z < 0 || (int) z > mapHeight - 1) {
break;
}
list.add(getHeight(x,z));
step += quality;
}
return list;
}
Upvotes: 2