D demoscene compo

bearophile bearophileHUGS at lycos.com
Sun Mar 22 17:38:15 PDT 2009


ponce:
> Source is released.

I have found this only now, thanks to http://planet.dsource.org/

I have seem there's lot of code like this one:
vec3!(T) xxx() { return vec3!(T)(x, x, x); }
vec3!(T) xxy() { return vec3!(T)(x, x, y); }
vec3!(T) xxz() { return vec3!(T)(x, x, z); }
etc etc ...

D1 allows you to do such things with less code (but less easy to understand):

import std.metastrings: Format;
import d.templates: Range;

string product3(char[3] s)() {
    string result;
    foreach (i; Range!(3))
        foreach (j; Range!(3))
            foreach (k; Range!(3))
                result ~= Format!("vec3!(T) %s() { return vec3!(T)(%s, %s, %s); }\n",
                                  ""~s[i]~s[j]~s[k], s[i], s[j], s[k]);
    return result;
}
pragma(msg, product3!("xyz")());
void main() {}

That can also be generalized in a compile-time general product, but it may be overkill.

Regarding the vec2D, vectors 3D, quaternions, segment-point distance, point-in-polygon, etc: most of such functions/classes/structs are present in most demos and games I have seen written in D, so I think such stuff deserves to be in the std libraries (Phobos and Tango), (part of that stuff is already present in my d libs, but this means little), avoiding people to re-implement them all the time. If you want you can polish such geometric code of yours, and offer it as a single module to Walter (with a license fitting for Phobos).

Bye,
bearophile


More information about the Digitalmars-d-bugs mailing list