sqrt and normalize builtins

This commit is contained in:
Wolfgang Bumiller 2013-01-08 19:21:24 +01:00
parent d6809ed331
commit 6ece523552
2 changed files with 54 additions and 11 deletions

View file

@ -123,6 +123,16 @@ Concatenate two strings, returning a tempstring.
.in +8
Compare two strings. Returns the same as the corresponding C functions.
.in
.RI "12) " vector " normalize (" vector ") = " "#12" ;
.in +8
Normalize a vector so its length is 1.
.in
.RI "13) " float " sqrt (" float ") = " "#13" ;
.in +8
Get a value's square root.
.in
.SH BUGS
Please report bugs on <http://github.com/graphitemaster/gmqcc/issues>,
or see <http://graphitemaster.github.com/gmqcc> on how to contact us.

55
exec.c
View file

@ -733,6 +733,16 @@ static int qc_kill(qc_program *prog)
return 0;
}
static int qc_sqrt(qc_program *prog)
{
qcany *num, out;
CheckArgs(1);
num = GetArg(0);
out._float = sqrt(num->_float);
Return(out);
return 0;
}
static int qc_vlen(qc_program *prog)
{
qcany *vec, len;
@ -745,6 +755,27 @@ static int qc_vlen(qc_program *prog)
return 0;
}
static int qc_normalize(qc_program *prog)
{
double len;
qcany *vec;
qcany out;
CheckArgs(1);
vec = GetArg(0);
len = sqrt(vec->vector[0] * vec->vector[0] +
vec->vector[1] * vec->vector[1] +
vec->vector[2] * vec->vector[2]);
if (len)
len = 1.0 / len;
else
len = 0;
out.vector[0] = len * vec->vector[0];
out.vector[1] = len * vec->vector[1];
out.vector[2] = len * vec->vector[2];
Return(out);
return 0;
}
static int qc_strcat(qc_program *prog)
{
char *buffer;
@ -795,17 +826,19 @@ static int qc_strcmp(qc_program *prog)
static prog_builtin qc_builtins[] = {
NULL,
&qc_print, /* 1 */
&qc_ftos, /* 2 */
&qc_spawn, /* 3 */
&qc_kill, /* 4 */
&qc_vtos, /* 5 */
&qc_error, /* 6 */
&qc_vlen, /* 7 */
&qc_etos, /* 8 */
&qc_stof, /* 9 */
&qc_strcat, /* 10 */
&qc_strcmp /* 11 */
&qc_print, /* 1 */
&qc_ftos, /* 2 */
&qc_spawn, /* 3 */
&qc_kill, /* 4 */
&qc_vtos, /* 5 */
&qc_error, /* 6 */
&qc_vlen, /* 7 */
&qc_etos, /* 8 */
&qc_stof, /* 9 */
&qc_strcat, /* 10 */
&qc_strcmp, /* 11 */
&qc_normalize, /* 12 */
&qc_sqrt /* 13 */
};
static size_t qc_builtins_count = sizeof(qc_builtins) / sizeof(qc_builtins[0]);