⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vec_h.c

📁 [Game.Programming].Academic - Graphics Gems (6 books source code)
💻 C
📖 第 1 页 / 共 2 页
字号:
char *getsuffix(s)char *s;{    static char buf[100];    int i;    for (i = 0; !strchr(" ,)", s[i]); ++i)	buf[i] = s[i];    buf[i] = 0;    return buf;}/* * Print the string s with the following substitutions: * 	%d turns into d *	%i turns into i *	%- turns into "-" if i is odd, " " otherwise *	%-- turns into "-" if i is even, " " otherwise *	%_ turns into "_" if i is odd, " " otherwise *	%-%- turns into "-" if d+i is even, " " otherwise (sorry this is stupid) *	%i+1 turns into (i+1)%d etc. *	%d-1 turns into d-1 etc. *	%% turns into % *	blah%0..d-1bleh turns into blah0bleh,blah1bleh,...,blahd-1bleh *	blah%~ibleh  is same as blah%0..d-1bleh but excluding i *	%XV turns into VXVX...XV with d-1 V's (or XV if d==2) *	%VXV turns into VXVX...XV with d V's  */void printformatted_to(s, tochar, d, i)char *s;int tochar, d, i;{    char *prefix, *suffix;    int num, j, lastj;    for (; *s != tochar; s++) {	if (*s == '%') {	    ++s;	    prefix = NULL;	    if (isdigit(*s) && !strncmp(s+1, "..d-1", 5)) {		prefix = getprefix(s-2);	/* one char before the % */		suffix = getsuffix(s+6);	    } else if (!strncmp(s, "~i", 2)) {		prefix = getprefix(s-2);	/* one char before the % */		suffix = getsuffix(s+2);	    }	    if (prefix) {		lastj = d-1 - (*s == '~');		for (j = atoi(s); j <= lastj; ++j) {		    printf("%d", j + (*s == '~' && j >= i));		    printf("%s", suffix);		    if (j < lastj)			printf(",%s", prefix);		}		s += (*s == '~' ? 2 : 6) + strlen(suffix) - 1;	    } else {		switch(*s) {		    case '-': case '_':			if (!strncmp(s, "-%-", 3)) {			    printf("%c", (i+d)%2==0 ? *s : ' ');			    s += 2;			} else if (!strncmp(s, "--", 2)) {			    printf("%c", i%2==0 ? *s : ' ');			    s++;			} else {			    printf("%c", i%2==1 ? *s : ' ');			}			break;		    case 'i': case 'd':			num = (*s == 'i' ? i : d);			if (strchr("+-", s[1])) {			    num += (s[1] == '-' ? -1 : 1) * atoi(s+2);			    if (*s == 'i')				num = (num+d) % d;			    s += 2;			    /* s is now pointing to the first digit */			    while (isdigit(s[1]))				s++;			    /* s is now pointing to the last digit */			}			printf("%d", num);			break;		    case 'X': case 'V':			if (*s == 'X' && d == 2)			    printf("X");			for (j = 0; j < d-2; ++j)			    printf("VX");			if (*s == 'V')			    printf("V");			break;		    case '%':			    printf("%%");			break;		    default:			assert(0);		}	    }	} else	    putchar(*s);    }}void printformatted(s, d, i)char *s;int d, i;{    printformatted_to(s, '\0', d, i);}void define(d, name_and_args, all_but_last, sep, last)int d;char *name_and_args, *all_but_last, *sep, *last;{    int i;    printf("#define ");    printformatted(name_and_args, d, 0);    printf("\t\\\n\t\t");    printf("(");	if (sep)	    for (i = 0; i < d-1; ++i) {		/* loop for all but last */		printformatted(all_but_last, d, i);		printformatted(sep, d, i);		printf(" \\\n\t\t ");	    }	printformatted(last, d, i);    printf(")\n");}int is_substring(a,b)char *a, *b;{    for (; *b; b++)	if (strncmp(a, b, strlen(a)) == 0)	    return 1;    return 0;}/* * Hack utility routines... */void print_name_and_args_with_stuff_inserted(name_and_arg, suff, arg0, d)char *name_and_arg, *suff, *arg0;int d;{    printformatted_to(name_and_arg, '(', d, 0);    printformatted(suff, d, 0);    printf("(");    if (arg0) {	printformatted(arg0, d, 0);	printf(",");    }    printformatted(strchr(name_and_arg, '(' /*)*/ ) + 1, d, 0);}void print_name_and_args_with_first_arg_changed(name_and_arg, arg0, d)char *name_and_arg, *arg0;int d;{    printformatted_to(name_and_arg, '(', d, 0);    printf("(");    printformatted(arg0, d, 0);    printformatted(strchr(name_and_arg, ','), d, 0);}void print_first_arg(name_and_arg, d)char *name_and_arg;int d;{    printformatted_to(strchr(name_and_arg, '(' /*)*/ ) + 1, ',', d, 0);}#define MINDIM 2main(argc, argv)char **argv;{    int i, d, maxdim;    if (argc != 2 || ! (maxdim = atoi(argv[argc-1])))	fprintf(stderr, "Usage: %s [<maxdim>]\n", argv[0]), exit(1);    printf("/*\n");    for (i  = 0; intro[i]; ++i) {	printf(" *");	printformatted(intro[i], maxdim, 0);	printf("\n");    }    printf(" */\n\n");    printf("#ifndef VEC_H\n");    printf("#define VEC_H %d\n", maxdim);    printf("#include <math.h>	/* for definition of floor() */\n");    for (d = MINDIM; d <= maxdim; ++d)	for (i = 0; i < numberof(defs); ++i) {	    if (is_substring("%d-1", defs[i].name_and_args) && d-1 < MINDIM	     || is_substring("%d+1", defs[i].name_and_args) && d+1 > maxdim)		continue;	    if (!is_substring("%d", defs[i].name_and_args) && d != MINDIM)		continue;	/* don't redefine if it's the same */	    define(d, defs[i].name_and_args,		      defs[i].all_but_last,		      defs[i].sep,		      defs[i].last ? defs[i].last : defs[i].all_but_last);	}    for (d = MINDIM; d <= maxdim; ++d)	for (i = 0; i < numberof(defs); ++i) {	    if (is_substring("%d-1", defs[i].name_and_args) && d-1 < MINDIM	     || is_substring("%d+1", defs[i].name_and_args) && d+1 > maxdim)		continue;	    if (!is_substring("%d", defs[i].name_and_args) && d != MINDIM)		continue;	/* don't redefine if it's the same */	    if (defs[i].safety > 0) {		int t;		printf("#define ");		print_name_and_args_with_stuff_inserted(defs[i].name_and_args,							    "safe", "type", d);		printf(" \\\n\t\t");		printformatted("do {type _vec_h_temp_[%d]", d, 0);		if (defs[i].safety > 1)		    printformatted("[%d]", d, 0);		printf("; \\\n\t\t    ");		print_name_and_args_with_first_arg_changed(			defs[i].name_and_args, "_vec_h_temp_", d);		printf("; \\\n\t\t    ");		if (defs[i].safety > 1)		    printformatted("SETMAT%d(", d, 0);		else		    printformatted("SET%d(", d, 0);		print_first_arg(defs[i].name_and_args, d);		printf(", _vec_h_temp_); \\\n\t\t} while (0)\n");		for (t = 0; t < numberof(types); ++t) {		    printf("#define ");		    print_name_and_args_with_stuff_inserted(			defs[i].name_and_args, types[t].abbr, (char *)NULL, d);		    printf(" ");		    print_name_and_args_with_stuff_inserted(			defs[i].name_and_args, "safe", types[t].full, d);		    printf("\n");		}	    }	}    printf("#endif /* VEC_H */\n");    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -