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?
Mof the size of the array prior to recursion, aLinkedListwill give you O(1) append versus amortized O(1) for anArrayList. But with a linked list, you will not benefit of O(1) for accessing a random element in the list. If you can provide such an estimateM, simply pass it as theinitialCapacityto the constructor ofArrayList.