Beispiel #1
0
static void split_into(
	word r, word g, word b,
	int n, byte *inxs
	)
	{
	byte inx;

	if ( n >= 9 )
		{
		inx = takefrom(div9(r), div9(g), div9(b));
		b -= gbmrgb_vga[inx].b;
		g -= gbmrgb_vga[inx].g;
		r -= gbmrgb_vga[inx].r;
		*inxs++ = inx;

		inx = takefrom(div8(r), div8(g), div8(b));
		b -= gbmrgb_vga[inx].b;
		g -= gbmrgb_vga[inx].g;
		r -= gbmrgb_vga[inx].r;
		*inxs++ = inx;

		inx = takefrom(div7(r), div7(g), div7(b));
		b -= gbmrgb_vga[inx].b;
		g -= gbmrgb_vga[inx].g;
		r -= gbmrgb_vga[inx].r;
		*inxs++ = inx;
		}

	if ( n >= 6 )
		{
		inx = takefrom(div6(r), div6(g), div6(b));
		b -= gbmrgb_vga[inx].b;
		g -= gbmrgb_vga[inx].g;
		r -= gbmrgb_vga[inx].r;
		*inxs++ = inx;

		inx = takefrom(div5(r), div5(g), div5(b));
		b -= gbmrgb_vga[inx].b;
		g -= gbmrgb_vga[inx].g;
		r -= gbmrgb_vga[inx].r;
		*inxs++ = inx;
		}

	if ( n >= 4 )
		{
		inx = takefrom(div4(r), div4(g), div4(b));
		b -= gbmrgb_vga[inx].b;
		g -= gbmrgb_vga[inx].g;
		r -= gbmrgb_vga[inx].r;
		*inxs++ = inx;
		}

	if ( n >= 3 )
		{
		inx = takefrom(div3(r), div3(g), div3(b));
		b -= gbmrgb_vga[inx].b;
		g -= gbmrgb_vga[inx].g;
		r -= gbmrgb_vga[inx].r;
		*inxs++ = inx;
		}

	inx = takefrom(div2(r), div2(g), div2(b));
	b -= gbmrgb_vga[inx].b;
	g -= gbmrgb_vga[inx].g;
	r -= gbmrgb_vga[inx].r;
	*inxs++ = inx;

	*inxs = takefrom(r, g, b);
	}
Beispiel #2
0
static int
find_roots(double a1, double a2, double a3, double *x1, double *x2, double *x3)
{
    double x, t;
    double p, q;

    q = (a1*a1-3.0*a2) / 9.0;
    p = (2.0*a1*a1*a1-9.0*a1*a2+27.0*a3) / 54.0;
    t = q*q*q - p*p;
    if (t >= 0.0) {
        t = acos(p /(q * sqrt(q)));
        x = -2.0*sqrt(q)*cos(t / 3.0) - a1/3.0;
    } else {
        if (p > 0.0) {
            t = pow(sqrt(-t)+p, 1.0 / 3.0);
            x = -(t + q / t) - a1/3.0;
        } else if (p == 0.0) {
            x = -a1/3.0;
        } else {
            t = pow(sqrt(-t)-p, 1.0 / 3.0);
            x = (t + q / t) - a1/3.0;
        }
    }
    {
        double x_backup = x;
        int i = 0;
        for (t = root3(a1, a2, a3, x); ABS(t-x) > 5.0e-4;
                t = root3(a1, a2, a3, x))
            if (++i == 32) {
                x = x_backup;
                break;
            } else
                x = t;
    }
    /*
    x = a1;
    i = 0;
    j = 0;
    for (t = root3(a1, a2, a3, x); ABS(t-x) > epsi;
    		    t = root3(a1, a2, a3, x)) {
       x = t;
       i++;
       if (i > 1000) {
     x = 0.5 * (x + root3(a1, a2, a3, x));
     j++;
     if (j == 3)
        break;
     i = 0;
       }
    }
     */

    *x1 = x;
    div3(a1, a2, a3, x, &a1, &a2);

    t = a1 * a1 - 4.0 * a2;
    if (t < 0) {
        printf("***** Two Imaginary Roots in Characteristic Admittance.\n");
        controlled_exit(EXIT_FAILURE);
    }

    t *= 1.0e-18;
    t = sqrt(t) * 1.0e9;
    if (a1 >= 0.0)
        *x2 = t = -0.5 * (a1 + t);
    else
        *x2 = t = -0.5 * (a1 - t);
    *x3 = a2 / t;
    /*
    *x2 = 0.5 * (-a1 + t);
    *x3 = 0.5 * (-a1 - t);
     */
    return(1);
}
Beispiel #3
0
static int
exp_find_roots(double a1, double a2, double a3, double *ex1, double *ex2, double *ex3)
{
    double x, t;
    double p, q;

    q = (a1*a1-3.0*a2) / 9.0;
    p = (2.0*a1*a1*a1-9.0*a1*a2+27.0*a3) / 54.0;
    t = q*q*q - p*p;
    if (t >= 0.0) {
        t = acos(p /(q * sqrt(q)));
        x = -2.0*sqrt(q)*cos(t / 3.0) - a1/3.0;
    } else {
        if (p > 0.0) {
            t = pow(sqrt(-t)+p, 1.0 / 3.0);
            x = -(t + q / t) - a1/3.0;
        } else if (p == 0.0) {
            x = -a1/3.0;
        } else {
            t = pow(sqrt(-t)-p, 1.0 / 3.0);
            x = (t + q / t) - a1/3.0;
        }
    }
    {
        double ex1;
        int i = 0;
        ex1 = x;
        for (t = root3(a1, a2, a3, x); ABS(t-x) > 5.0e-4;
                t = root3(a1, a2, a3, x))
            if (++i == 32) {
                x = ex1;
                break;
            } else
                x = t;
    }
    /***
    x = a1;
    for (t = root3(a1, a2, a3, x); ABS(t-x) > epsi2;
    		    t = root3(a1, a2, a3, x)) {
       x = t;
       i++;
       if (i > 1000) {
          x = 0.5 * (x + root3(a1, a2, a3, x));
          j++;
          if (j == 3)
             break;
          i = 0;
       }
    }
     ***/
    *ex1 = x;
    div3(a1, a2, a3, x, &a1, &a2);

    t = a1 * a1 - 4.0 * a2;
    if (t < 0) {
        ifImg = 1;
        printf("***** Two Imaginary Roots.\n");
        *ex3 = 0.5 * sqrt(-t);
        *ex2 = -0.5 * a1;
    } else {
        ifImg = 0;
        t *= 1.0e-16;
        t = sqrt(t)*1.0e8;
        if (a1 >= 0.0)
            *ex2 = t = -0.5 * (a1 + t);
        else
            *ex2 = t = -0.5 * (a1 - t);
        *ex3 = a2 / t;
        /*
        *ex2 = 0.5 * (-a1 + t);
        *ex3 = 0.5 * (-a1 - t);
         */
    }

    return(1);
}
Beispiel #4
0
inline void normalized3(vec3 * r, vec3 v) {
	scalar l = length3( v );
	div3( r, v, l );
}