r/AskProgramming • • Apr 13 '26

Is my new pathfinding algorithm any good?

0 Upvotes

Could not find a better reddit, so I'll turn this into a question asking for opinions.

had an idea for an algorithm to pathfind. It is not about finding the literal shortest nor cheapest path but makes the assumption that a straight line is the best option.

The algorithm starts off with a line of sight check (pick your favorite) but clear space is of course about traversability.

An overly simplistic case for demonstration. Imagine a short wall between the source and destination. The algorithm follows a straight line to the destination. When this line hits a wall, it start follows the wall in both directions until a corner is found. The algorithm then makes two line of sight checks. One to check line of sight from the source to the corner, and another from the corner to the destination. If both are clear, it is kept as a valid path. Otherwise, the algorithm is performed recursively, this time on two paths, from the source to the corner, and from the corner to the destination.

Once one valid path is found, any others in progress will be checked periodically and those of longer distance cancelled and ignored, keep checking until all paths either reach completion or get culled, leaving one shortest path.

Obviously a few details need to be taken care of, such as adding a little padding around the corners to make sure the object to follow the path can actually fit (proper handling of the padding can treat bars as a straight flat barrier) as well as not flowing through two obstacles that only meet at a point, and also covering a case where the source is completely surrounded or otherwise unable to find a valid path.

Is this algorithm any good? Does it exist already?


r/AskProgramming • • Apr 12 '26

C/C++ Which approach is faster? "If" statements with range comparisons or a long "switch" block?

0 Upvotes

Hello, I am working on a really tight CPU budget so I came up with an idea to speed up this function with arguably bad programming choices.

I tried to optimize the function below with a big "switch" statement but the compiler outputted code that is actually a little slower.

Do you think this is a good idea or should I go back to the old approach?

This is the original function

uint8_t read(uint32_t addr)

{

`if (addr >= 0x002100 && addr <= 0x1FFFFF)`

	`return rom_bin[(addr/*-0x2100*/)&0x1FFFFF];`

`else`

`if (addr >= 0x001000 && addr <= 0x001FFF)`

	`return ram[(addr-0x1000)&0x0FFF];`

`else`

`if (addr >= 0x000000 && addr <= 0x000FFF)`

	`return bios[addr&0x0FFF];`

`else`

`if (addr >= 0x002000 && addr <= 0x0020FF)`

	`return read_reg(addr&0x0000FF);`

`else`

`if ((addr>>20) >= 2) //Cart ROM mirror`

	`return rom[addr&0x1FFFFF];`

`else`

	`return 0x00;`

}

This is the new "optimized" version

uint8_t read(uint32_t addr)

{

`switch (addr>>8)`

`{`

	`case 0x00: case 0x01: case 0x02: case 0x03:`

	`case 0x04: case 0x05: case 0x06: case 0x07:`

	`case 0x08: case 0x09: case 0x0A: case 0x0B:`

	`case 0x0C: case 0x0D: case 0x0E: case 0x0F:`

		`//BIOS (read-only)`

		`return bios[addr&0x0FFF];`

	`case 0x10: case 0x11: case 0x12: case 0x13:`

	`case 0x14: case 0x15: case 0x16: case 0x17:`

	`case 0x18: case 0x19: case 0x1A: case 0x1B:`

	`case 0x1C: case 0x1D: case 0x1E: case 0x1F:`

		`//Work RAM (general purpose area)`

		`return ram[addr&0x0FFF];`

	`case 0x20:`

		`//Hardware registers`

		`return read_reg(addr&0x0000FF);`

	`default:`

		`//ROM (read-only)`

		`return rom[addr&0x1FFFFF];`

`}`

}