static void ellipse_vignette_proc( int y, int left, int right, LPTR lpDst, LPTR lpSrc, int depth, LPGRADIENT_DATA lpGrad ) { int xn, index; LPLONG lptld, lptlD; long dx, dy, d, dx2, dy2; dy = y - lpGrad->yc; lptld = lpGrad->lpld; lptlD = lpGrad->lplD; for (xn = left; xn <= right; ++xn) { dx = xn - lpGrad->xc; dx2 = dx*dx; dy2 = dy*dy; d = dx2 + dy2; *lptld++ = lsqrt(d); if (d > 0) { if (lpGrad->ea > lpGrad->eb) index = ((dx2 * (long)TSIZE)+(d/2)) / d; else index = ((dy2 * (long)TSIZE)+(d/2)) / d; index = bound(abs(index), 0, TSIZE); *lptlD++ = lpGrad->lpD[index]; } else *lptlD++ = 1; } (*lpGrad->lpProcessProc)(lpSrc, right-left+1, lpGrad); }
int main () { unsigned long j; for ( j = 1; j < 2000000000; j += (j / 2) + 1 ) { printf( "%8lu : %8lu\n", j, lsqrt(j) ); } j = 0x7FFFFFFFL; printf( "%8lu : %8lu\n", j, lsqrt(j) ); j++; printf( "%8lu : %8lu\n", j, lsqrt(j) ); j = 0xFFFFFFFFL - 1; printf( "%8lu : %8lu\n", j, lsqrt(j) ); j ++; printf( "%8lu : %8lu\n", j, lsqrt(j) ); return 0; }
void TShearX( LPTFORM lpTForm, int height, int dx ) /***********************************************************************/ { LFIXED b; unsigned int h; h = lsqrt(((long)height * (long)height) + ((long)dx * (long)dx)); b = FGET(dx, h); lpTForm->ax += FIXMUL( lpTForm->ay, b ); lpTForm->bx += FIXMUL( lpTForm->by, b ); lpTForm->cx += FIXMUL( lpTForm->cy, b ); }
void TShearY( LPTFORM lpTForm, int width, int dy ) /***********************************************************************/ { LFIXED a; unsigned int h; h = lsqrt(((long)width * (long)width) + ((long)dy * (long)dy)); a = FGET(dy, h); lpTForm->ay += FIXMUL( lpTForm->ax, a ); lpTForm->by += FIXMUL( lpTForm->bx, a ); lpTForm->cy += FIXMUL( lpTForm->cx, a ); }
bool i_navigate_sqrt(arg_t a) { long n = (long) a; unsigned long q = lsqrt(filecnt); if (q > 0) n *= q; if (mode == MODE_IMAGE) { if (prefix > 0) n *= prefix; n += fileidx; if (n < 0) n = 0; if (n >= filecnt) n = filecnt - 1; if (n != fileidx) { load_image(n); return true; } } return false; }
static void radial_vignette_proc( int y, int left, int right, LPTR lpDst, LPTR lpSrc, int depth, LPGRADIENT_DATA lpGrad ) { int xn, tdx, tdy; LPLONG lptld, lptlD; long D; tdy = y - lpGrad->y1; lptld = lpGrad->lpld; lptlD = lpGrad->lplD; D = lpGrad->D; for (xn = left; xn <= right; ++xn, ++lptld, ++lptlD) { tdx = xn - lpGrad->x1; *lptld = lsqrt(((long)tdx*(long)tdx)+((long)tdy*(long)tdy)); *lptlD = D; } (*lpGrad->lpProcessProc)(lpSrc, right-left+1, lpGrad); }
void CreateWeaponDataText(String* text, long whichWeapon, const StringSlice& weaponName) { baseObjectType *weaponObject, *missileObject; long mostDamage, actionNum; objectActionType *action; bool isGuided = false; if (whichWeapon == kNoShip) { return; } weaponObject = gBaseObjectData.get() + whichWeapon; // TODO(sfiera): catch exception. Resource rsrc("text", "txt", kWeaponDataTextID); String data(macroman::decode(rsrc.data())); // damage; this is tricky--we have to guess by walking through activate actions, // and for all the createObject actions, see which creates the most damaging // object. We calc this first so we can use isGuided mostDamage = 0; isGuided = false; if ( weaponObject->activateActionNum > 0) { action = gObjectActionData.get() + weaponObject->activateAction; for ( actionNum = 0; actionNum < weaponObject->activateActionNum; actionNum++) { if (( action->verb == kCreateObject) || ( action->verb == kCreateObjectSetDest)) { missileObject = gBaseObjectData.get() + action->argument.createObject.whichBaseType; if ( missileObject->attributes & kIsGuided) isGuided = true; if ( missileObject->damage > mostDamage) mostDamage = missileObject->damage; } action++; } } StringList keys(kShipDataKeyStringID); StringList values(kShipDataNameID); // weapon name # find_replace(data, 0, keys.at(kWeaponNumberStringNum), weaponName); // weapon name { StringList names(5000); const StringSlice& name = names.at(whichWeapon); find_replace(data, 0, keys.at(kWeaponNameStringNum), name); } const StringSlice& yes = values.at(kShipDataYesStringNum); const StringSlice& no = values.at(kShipDataNoStringNum); const StringSlice& dash = values.at(kShipDataDashStringNum); // is guided if (isGuided) { find_replace(data, 0, keys.at(kWeaponGuidedStringNum), yes); } else { find_replace(data, 0, keys.at(kWeaponGuidedStringNum), no); } // is autotarget if (weaponObject->attributes & kAutoTarget) { find_replace(data, 0, keys.at(kWeaponAutoTargetStringNum), yes); } else { find_replace(data, 0, keys.at(kWeaponAutoTargetStringNum), no); } // range find_replace(data, 0, keys.at(kWeaponRangeStringNum), lsqrt(weaponObject->frame.weapon.range)); if (mostDamage > 0) { find_replace(data, 0, keys.at(kWeaponDamageStringNum), mostDamage); } else { find_replace(data, 0, keys.at(kWeaponDamageStringNum), dash); } print(*text, data); }
inline Fixed lsqrt(Fixed n) { return Fixed::from_val(lsqrt(n.val())); }
void mPlayDistanceSound( long mvolume, spaceObjectType* mobjectptr, long msoundid, long msoundpersistence, soundPriorityType msoundpriority) { if (mobjectptr->distanceFromPlayer < kMaximumRelevantDistanceSquared) { int32_t mdistance = mobjectptr->distanceFromPlayer; uint32_t mul1; uint32_t mul2; spaceObjectType* mplayerobjectptr; if (mdistance == 0) { if (globals()->gPlayerShipNumber >= 0) { mplayerobjectptr = gSpaceObjectData.get() + globals()->gPlayerShipNumber; } else { mplayerobjectptr = NULL; } if ((mplayerobjectptr != NULL) && (mplayerobjectptr->active)) { mul1 = ABS<int>(mplayerobjectptr->location.h - mobjectptr->location.h); mul2 = mul1; mul1 = ABS<int>(mplayerobjectptr->location.v - mobjectptr->location.v); mdistance = mul1; if ((mul2 < kMaximumRelevantDistance) && (mdistance < kMaximumRelevantDistance)) { mdistance = mdistance * mdistance + mul2 * mul2; } else { mdistance = kMaximumRelevantDistanceSquared; } mdistance = lsqrt(mdistance); if (mdistance > 480) { mdistance -= 480; if (mdistance > 1920) { mvolume = 0; } else { mvolume = ((1920 - mdistance) * mvolume) / 1920; } } if (mvolume > 0) { PlayLocalizedSound( mplayerobjectptr->location.h, mplayerobjectptr->location.v, mobjectptr->location.h, mobjectptr->location.v, mplayerobjectptr->velocity.h - mobjectptr->velocity.h, mplayerobjectptr->velocity.v - mobjectptr->velocity.v, msoundid, mvolume, msoundpersistence, msoundpriority); } } else { mul1 = ABS<int>(gGlobalCorner.h - mobjectptr->location.h); mul2 = mul1; mul1 = ABS<int>(gGlobalCorner.v - mobjectptr->location.v); mdistance = mul1; if ((mul2 < kMaximumRelevantDistance) && (mdistance < kMaximumRelevantDistance)) { mdistance = mdistance * mdistance + mul2 * mul2; } else { mdistance = kMaximumRelevantDistanceSquared; } mdistance = lsqrt(mdistance); if (mdistance > 480) { mdistance -= 480; if (mdistance > 1920) { mvolume = 0; } else { mvolume = ((1920 - mdistance) * mvolume) / 1920; } } if (mvolume > 0) { PlayLocalizedSound( gGlobalCorner.h, gGlobalCorner.v, mobjectptr->location.h, mobjectptr->location.v, mobjectptr->velocity.h, mobjectptr->velocity.v, msoundid, mvolume, msoundpersistence, msoundpriority); } } } else { mdistance = lsqrt(mdistance); if (mdistance > 480) { mdistance -= 480; if (mdistance > 1920) { mvolume = 0; } else { mvolume = ((1920 - mdistance) * mvolume) / 1920; } } if (globals()->gPlayerShipNumber >= 0) { mplayerobjectptr = gSpaceObjectData.get() + globals()->gPlayerShipNumber; } else { mplayerobjectptr = NULL; } if ((mplayerobjectptr != NULL) && (mplayerobjectptr->active)) { if (mvolume > 0) { PlayLocalizedSound( mplayerobjectptr->location.h, mplayerobjectptr->location.v, mobjectptr->location.h, mobjectptr->location.v, mplayerobjectptr->velocity.h - mobjectptr->velocity.h, mplayerobjectptr->velocity.v - mobjectptr->velocity.v, msoundid, mvolume, msoundpersistence, msoundpriority); } } else { if (mvolume > 0) { PlayLocalizedSound( gGlobalCorner.h, gGlobalCorner.v, mobjectptr->location.h, mobjectptr->location.v, mobjectptr->velocity.h, mobjectptr->velocity.v, msoundid, mvolume, msoundpersistence, msoundpriority); } } } } }
/* * Find the sum of "n" squares that equal "d1" and display if found. * * Return false if not found. */ int sumsq(long d1, int n) { int i = 0; long d2; long save = 0; int found_one = false; d2 = d1; try_again: for (;;) { if (i == 2) { save = d2; } squares[i] = lsqrt(d2); assert(squares[i] >= 0); d2 -= squared(squares[i]); i++; if (i >= n) { break; } } if (d2 == 0) { for (i = 0; i < n; i++) { d2 += squared(squares[i]); } if (d2 != d1) { fprintf(stderr, "Error: Result doesn't compare identical to original number!\n"); exit(EXIT_FAILURE); } for (i = 0; i < (n - 1); i++) { if (squares[i] < squares[i+1]) { goto skip_print; } } found_one = true; printf("%ld = %ld^2", d1, squares[0]); for (i = 1; i < n; i++) { if (squares[i] != 0) printf(" + %ld^2", squares[i]); } printf("\n"); skip_print: assert(found_one); if (n < 2 || n > multi) { return found_one; } } switch (n) { case 4: if (squares[2] > squares[n-1]) { squares[2] -= 1; d2 = save - squared(squares[2]); i = 3; goto try_again; } case 3: if (squares[1] > squares[n-1]) { squares[1] -= 1; d2 = d1 - squared(squares[0]) - squared(squares[1]); i = 2; goto try_again; } case 2: if (squares[0] > squares[n-1]) { squares[0] -= 1; d2 = d1 - squared(squares[0]); i = 1; goto try_again; } } return found_one; }
BOOL GradientImage( LPIMAGE lpImage, LPGRADIENT_PARMS lpParms) { LPFRAME lpFrame; RECT rMask; LPINT lpD; FRMTYPEINFO TypeInfo; int dx, dy, iCount; int index, prev, next, pi, ni; long ldx, ldy, x, y, xs, xe, ys, ye, asqrd, bsqrd, r; LPDATAPROC lpVignetteProc; ENGINE Engine; BOOL DoHSL; GRADIENT_DATA data; int res; FRMDATATYPE type; ImgGetInfo(lpImage, NULL, NULL, NULL, &type); if (type == FDT_LINEART) return(FALSE); if (!(lpFrame = ImgGetBaseEditFrame(lpImage))) return(FALSE); res = FrameResolution(lpFrame); data.x1 = lpParms->x1; data.y1 = lpParms->y1; data.x2 = lpParms->x2; data.y2 = lpParms->y2; ResConvertUL(lpParms->iBaseRes, res, &data.x1, &data.y1); ResConvertLR(lpParms->iBaseRes, res, &data.x2, &data.y2); dx = data.x2 - data.x1; dy = data.y2 - data.y1; data.SoftTransition = lpParms->SoftTransition; data.lpPaletteLUT = NULL; if (lpParms->Gradient == IDC_VIGLINEAR || lpParms->Gradient == IDC_VIGRADIAL) { if (abs(dx) <= 3 && abs(dy) <= 3) return(FALSE); } else { if (abs(dx) <= 3 || abs(dy) <= 3) return(FALSE); } data.xc = (data.x1 + data.x2) / 2; data.yc = (data.y1 + data.y2) / 2; if ( (data.nRepetitions = lpParms->RepeatCount) <= 0 ) data.nRepetitions = 1; FrameGetTypeInfo(lpFrame, &TypeInfo); DoHSL = (lpParms->VigColorModel+IDC_FIRST_MODEL) != IDC_MODEL_RGB && (TypeInfo.DataType > FDT_GRAYSCALE); switch (lpParms->Gradient) { case IDC_VIGLINEAR: data.D = lsqrt(((long)dx*(long)dx)+((long)dy*(long)dy)); data.sine = FGET(-dy, data.D); data.cosine = FGET(dx, data.D); data.xr = WHOLE(( rotx(data.x2, data.y2, data.x1, data.y1, data.cosine, data.sine) )); lpVignetteProc = (LPDATAPROC)linear_vignette_proc; break; case IDC_VIGRADIAL: data.D = lsqrt(((long)dx*(long)dx)+((long)dy*(long)dy)); lpVignetteProc = (LPDATAPROC)radial_vignette_proc; break; case IDC_VIGCIRCLE: data.x1 = data.xc; data.y1 = data.yc; data.y2 = data.yc; dx = data.x2 - data.x1; dy = data.y2 - data.y1; if (!dx && !dy) return(FALSE); data.D = lsqrt(((long)dx*(long)dx)+((long)dy*(long)dy)); lpVignetteProc = (LPDATAPROC)radial_vignette_proc; break; case IDC_VIGSQUARE: case IDC_VIGRECTANGLE: data.ymin = min(data.y1, data.y2); data.ymax = max(data.y1, data.y2); data.xmin = min(data.x1, data.x2); data.xmax = max(data.x1, data.x2); ldx = data.xmin-data.xc; // upper left ldy = data.ymin-data.yc; data.m1 = (256L * ldy) / ldx; data.b1 = data.ymin - ((data.m1 * data.xmin)/256L); data.D1 = lsqrt((ldx*ldx)+(ldy*ldy)); ldx = data.xmax-data.xc; // upper right ldy = data.ymin-data.yc; data.m2 = (256L * ldy) / ldx; data.b2 = data.ymin - ((data.m2 * data.xmax)/256L); data.D2 = lsqrt((ldx*ldx)+(ldy*ldy)); ldx = data.xmax-data.xc; // lower right ldy = data.ymax-data.yc; data.m3 = (256L * ldy) / ldx; data.b3 = data.ymax - ((data.m3 * data.xmax)/256L); data.D3 = lsqrt((ldx*ldx)+(ldy*ldy)); ldx = data.xmin-data.xc; // lower left ldy = data.ymax-data.yc; data.m4 = (256L * ldy) / ldx; data.b4 = data.ymax - ((data.m4 * data.xmin)/256L); data.D4 = lsqrt((ldx*ldx)+(ldy*ldy)); lpVignetteProc = (LPDATAPROC)rectangle_vignette_proc; break; case IDC_VIGELLIPSE: if ( !(data.lpD = (LPINT)Alloc((long)sizeof(int)*(TSIZE+1))) ) { Message(IDS_EMEMALLOC); return(FALSE); } iCount = TSIZE+1; lpD = data.lpD; while (--iCount >= 0) *lpD++ = -1; data.ymin = min(data.y1, data.y2); data.ymax = max(data.y1, data.y2); data.xmin = min(data.x1, data.x2); data.xmax = max(data.x1, data.x2); data.ea = dx/2; data.eb = dy/2; if (!data.ea || !data.eb) { FreeUp((LPTR)data.lpD); return(FALSE); } asqrd = data.ea*data.ea; bsqrd = data.eb*data.eb; // fill in a table with radius information for the // ellipse. The radius for a given point would be // starting from the center of the ellipse, going // through to point, and where it intersects the // edge of the ellipse. We need the radius for // the D value used in the ellipse_proc, which is // the maximum distance used for determining how to // calculate the gradient, which is d/D. d is the // distance of the point from the center, D is extracted // from the table built below. The index of the table is // formed from the ratio of sides of the triangle formed. // This is like looking up the angle to see where the // point would intersect the circle. But we calculate // the radii ahead of time to speed things up. if (data.ea > data.eb) // step in x { xs = data.xc - data.xc; xe = data.xmax - data.xc; for (x = xs; x <= xe; ++x) { y = ((data.eb*(long)lsqrt(asqrd - (x*x)))+(data.ea/2))/data.ea; r = (x*x)+(y*y); if (r <= 0) r = 1; index = ((x * x * (long)TSIZE)+(r/2)) / r; index = bound(abs(index), 0, TSIZE); data.lpD[index] = lsqrt(r); } } else // step in y { ys = data.yc - data.yc; ye = data.ymax - data.yc; for (y = ys; y <= ye; ++y) { x = ((data.ea*(long)lsqrt(bsqrd - (y*y)))+(data.eb/2))/data.eb; r = (x*x)+(y*y); if (r <= 0) r = 1; index = ((y * y * (long)TSIZE)+(r/2)) / r; index = bound(abs(index), 0, TSIZE); data.lpD[index] = lsqrt(r); } } // find the first valid entry in our table for (index = 0; index <= TSIZE && data.lpD[index] < 0; ++index) ; // see if we have any entries if (index > TSIZE) { FreeUp((LPTR)data.lpD); return(FALSE); } // fill in all entries before first with value of first while (--index >= 0) data.lpD[index] = data.lpD[index+1]; // find last valid entry in table for (index = TSIZE; index >= 0 && data.lpD[index] < 0; --index) ; // see if we have any entries if (index < 0) { FreeUp((LPTR)data.lpD); return(FALSE); } // fill in all entries after last with value of last while (++index <= TSIZE) data.lpD[index] = data.lpD[index-1]; // interpolate values of all empty cells for (index = 0; index <= TSIZE; ++index) { if (data.lpD[index] < 0) { pi = index - 1; prev = data.lpD[pi]; ni = index; while (data.lpD[ni] < 0) ++ni; next = data.lpD[ni]; // remember here that (index-pi) == 1 data.lpD[index] = prev + ((next-prev)/(ni-pi)); } } lpVignetteProc = (LPDATAPROC)ellipse_vignette_proc; break; default: return(FALSE); break; } switch(TypeInfo.DataType) { case FDT_LINEART : case FDT_GRAYSCALE : data.lpProcessProc = (LPVIGPROC)ProcessVignette8; break; case FDT_PALETTECOLOR: data.lpProcessProc = (LPVIGPROC)ProcessVignette8P; data.lpPaletteLUT = CreatePaletteLut15(TypeInfo.ColorMap->RGBData, TypeInfo.ColorMap->NumEntries, NULL, NULL); break; case FDT_RGBCOLOR : data.lpProcessProc = (LPVIGPROC)ProcessVignette24; break; case FDT_CMYKCOLOR : data.lpProcessProc = (LPVIGPROC)ProcessVignette32; break; } data.lpMidpointTable = BuildMidPointTable( DoHSL, TypeInfo.DataType, lpParms->Midpoint, &lpParms->StartColor, &lpParms->EndColor, &data ); ImgGetMaskRect( lpImage, &rMask ); data.lplD = (LPLONG)Alloc((long)sizeof(long)*(long)RectWidth(&rMask)); data.lpld = (LPLONG)Alloc((long)sizeof(long)*(long)RectWidth(&rMask)); if (!data.lpld || !data.lplD || !data.lpMidpointTable || (TypeInfo.DataType == FDT_PALETTECOLOR && !data.lpPaletteLUT)) { if (lpParms->Gradient == IDC_VIGELLIPSE) FreeUp((LPTR)data.lpD); if (data.lplD) FreeUp((LPTR)data.lplD); if (data.lpld) FreeUp((LPTR)data.lpld); if (data.lpMidpointTable) FreeUp((LPTR)data.lpMidpointTable); if (data.lpPaletteLUT) FreeUp(data.lpPaletteLUT); return(FALSE); } SetEngineDraw(&Engine,lpVignetteProc,lpParms->VigOpacity,lpParms->VigMergeMode); Engine.lpParam = &data; Engine.fThread = NO; lpParms->Common.StatusCode = LineEngineSelObj(lpImage,&Engine,lpParms->Common.idDirty); if (!AstralIsRectEmpty(&Engine.rUpdate)) { lpParms->Common.UpdateType = UT_AREA; lpParms->Common.rUpdateArea = Engine.rUpdate; } FreeUp((LPTR)data.lpld); FreeUp((LPTR)data.lplD); FreeUp((LPTR)data.lpMidpointTable); if (data.lpPaletteLUT) FreeUp(data.lpPaletteLUT); if (lpParms->Gradient == IDC_VIGELLIPSE) FreeUp((LPTR)data.lpD); return(lpParms->Common.StatusCode == SC_SUCCESS); }
static void rectangle_vignette_proc( int the_y, int left, int right, LPTR lpDst, LPTR lpSrc, int depth, LPGRADIENT_DATA lpGrad ) { int xn, px, py, the_x, y; long xc2, yc2, dy, m, ldx, ldy; LPLONG lptld, lptlD; xc2 = 2L * lpGrad->xc; yc2 = 2L * lpGrad->yc; if (the_y > lpGrad->yc) // bottom side y = yc2 - the_y; // flip y over to top side else y = the_y; dy = y - lpGrad->yc; lptld = lpGrad->lpld; lptlD = lpGrad->lplD; for (the_x = left; the_x <= right; ++the_x) { if (the_x > lpGrad->xc) // right side xn = xc2 - the_x; // flip x over to left side else xn = the_x; if (xn < lpGrad->xmin || xn > lpGrad->xmax || y < lpGrad->ymin || y > lpGrad->ymax) { *lptld++ = 2; *lptlD++ = 1; continue; } if (xn > lpGrad->xc) // right side { m = (256L * dy) / (xn-lpGrad->xc); if (y > lpGrad->yc) // bottom-right corner { *lptlD++ = lpGrad->D3; if (m < lpGrad->m3) { px = xn; py = (((lpGrad->m3 * px)+128L)/256L)+lpGrad->b3; } else { py = y; px = (((py-lpGrad->b3)*256L)+128L)/lpGrad->m3; } } else // top-right corner { *lptlD++ = lpGrad->D2; if (m > lpGrad->m2) { px = xn; py = (((lpGrad->m2 * px)+128L)/256L)+lpGrad->b2; } else { py = y; px = (((py-lpGrad->b2)*256L)+128L)/lpGrad->m2; } } } else if (xn < lpGrad->xc) // left side { m = (256L * dy) / (xn-lpGrad->xc); if (y > lpGrad->yc) // bottom-left corner { *lptlD++ = lpGrad->D4; if (m > lpGrad->m4) { px = xn; py = (((lpGrad->m4 * px)+128L)/256L)+lpGrad->b4; } else { py = y; px = (((py-lpGrad->b4)*256L)+128L)/lpGrad->m4; } } else // top-left corner { *lptlD++ = lpGrad->D1; if (m < lpGrad->m1) { px = xn; py = (((lpGrad->m1 * px)+128L)/256L)+lpGrad->b1; } else { py = y; px = (((py-lpGrad->b1)*256L)+128L)/lpGrad->m1; } } } else // x = lpGrad->xc - infinite slope { if (y > lpGrad->yc) { *lptlD++ = lpGrad->D3; py = y; px = (((py-lpGrad->b3)*256L)+128L)/lpGrad->m3; } else { *lptlD++ = lpGrad->D1; py = y; px = (((py-lpGrad->b1)*256L)+128L)/lpGrad->m1; } } ldx = px - lpGrad->xc; ldy = py - lpGrad->yc; *lptld++ = lsqrt((ldx*ldx)+(ldy*ldy)); } (*lpGrad->lpProcessProc)(lpSrc, right-left+1, lpGrad); }