[Freeciv-Dev] Re: (PR#7350) Map radius change with city size
[Top] [All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
<URL: http://rt.freeciv.org/Ticket/Display.html?id=7350 >
This is an attrocious algorithm and the excuses should
be gut wrenching enough to force one to use a little
brain power to fix it ...
Why people think looping over every world possibiity to
do a test in an inner loop for something that can be evaluated
by a little pre-thought to use a *slightly* more efficient
order of evaluation, is some sort of "good" programming
style, and *never* losing a chance to try and slow down
the code, is something I will never understand.
Use brainpower to reorder the loops and document the process.
Leave this behind as a comment if you want to show off poor
programming skills that were avoided or whatever your rationale
for this code is.
Cheers
RossW
=====
Jason Short wrote:
> <URL: http://rt.freeciv.org/Ticket/Display.html?id=7350 >
>
> I think the generation of the indices is more complicated than it needs
> to be.
>
> I think this implementation should work:
>
> int dist, count = 0;
>
> for (dist = 0; dist <= CITY_MAP_RADIUS * CITY_MAP_RADIUS + 2; dist++) {
> for (x = 0; x < CITY_MAP_SIZE; x++) {
> for (y = 0; y < CITY_MAP_SIZE; y++) {
> int dx = (x - CITY_MAP_RADIUS);
> int dy = (y - CITY_MAP_RADIUS);
>
> if (dx * dx + dy * dy == dist) {
> indices[count].x = x;
> indices[count].y = y;
> count++;
> }
> }
> }
> }
>
> This is O(R^4), where R = CITY_MAP_RADIUS (or max city radius). This is
> quite acceptable as a one-time cost.
>
> jason
- [Freeciv-Dev] Re: (PR#7350) Map radius change with city size,
rwetmore@xxxxxxxxxxxx <=
|
|