int main(void){ float f1 = -15.5, f2 = 20.0, f3 = -5.0; int i1 = -716; float result; result = absoluteValue(f1); printf("result = %.2f\n", result); printf("f1 = %.2f\n", result); result = absoluteValue(f2) + absoluteValue(f3); printf("result = %.2f\n", result); result = absoluteValue((float) i1); printf("result = %.2f\n", result); result = absoluteValue(i1); printf("result = %.2f\n", result); return 0; }
void distanceDataForNode(FocusDirection direction, const FocusCandidate& current, FocusCandidate& candidate) { if (areElementsOnSameLine(current, candidate)) { if ((direction == FocusDirectionUp && current.rect.y() > candidate.rect.y()) || (direction == FocusDirectionDown && candidate.rect.y() > current.rect.y())) { candidate.distance = 0; candidate.alignment = Full; return; } } LayoutRect nodeRect = candidate.rect; LayoutRect currentRect = current.rect; deflateIfOverlapped(currentRect, nodeRect); if (!isRectInDirection(direction, currentRect, nodeRect)) return; LayoutPoint exitPoint; LayoutPoint entryPoint; LayoutUnit sameAxisDistance = 0; LayoutUnit otherAxisDistance = 0; entryAndExitPointsForDirection(direction, currentRect, nodeRect, exitPoint, entryPoint); switch (direction) { case FocusDirectionLeft: sameAxisDistance = exitPoint.x() - entryPoint.x(); otherAxisDistance = absoluteValue(exitPoint.y() - entryPoint.y()); break; case FocusDirectionUp: sameAxisDistance = exitPoint.y() - entryPoint.y(); otherAxisDistance = absoluteValue(exitPoint.x() - entryPoint.x()); break; case FocusDirectionRight: sameAxisDistance = entryPoint.x() - exitPoint.x(); otherAxisDistance = absoluteValue(entryPoint.y() - exitPoint.y()); break; case FocusDirectionDown: sameAxisDistance = entryPoint.y() - exitPoint.y(); otherAxisDistance = absoluteValue(entryPoint.x() - exitPoint.x()); break; default: ASSERT_NOT_REACHED(); return; } float x = (entryPoint.x() - exitPoint.x()) * (entryPoint.x() - exitPoint.x()); float y = (entryPoint.y() - exitPoint.y()) * (entryPoint.y() - exitPoint.y()); float euclidianDistance = sqrt(x + y); // Loosely based on http://www.w3.org/TR/WICD/#focus-handling // df = dotDist + dx + dy + 2 * (xdisplacement + ydisplacement) - sqrt(Overlap) float distance = euclidianDistance + sameAxisDistance + 2 * otherAxisDistance; candidate.distance = roundf(distance); LayoutSize viewSize = candidate.visibleNode->document().page()->mainFrame().view()->visibleContentRect().size(); candidate.alignment = alignmentForRects(direction, currentRect, nodeRect, viewSize); }
void checkArray(int n, int oneZero[], int start[], int *dif, int resultArray1[], int resultArray2[]){ int difference = 0, *interim1 = calloc(n,sizeof(int)), *interim2 = calloc(n,sizeof(int)); int i = 0, j = 0, k = 0; for (i = 0, j = 0, k = 0; i<n; i++){ //splits array in two according to oneZero if(oneZero[i] == 1){ interim1[j] = start[i]; j++; } else if (oneZero[i] == 0) { interim2[k] = start[i]; k++; } } //assigns difference of two arrays and makes it absolute value difference = sumArray(n,interim1) - sumArray(n,interim2); difference = absoluteValue(difference); if (difference < *dif){ //checks new difference with old one *dif = difference; //if smaller assigns smaller value copyArray(n,resultArray1,interim1); //and new arrays copyArray(n,resultArray2,interim2); } free(interim1); free(interim2); }//checkArray
// FIXME: This should take a RenderBoxModelObject&. bool RenderLineBoxList::rangeIntersectsRect(RenderBoxModelObject* renderer, LayoutUnit logicalTop, LayoutUnit logicalBottom, const LayoutRect& rect, const LayoutPoint& offset) const { LayoutUnit physicalStart = logicalTop; LayoutUnit physicalEnd = logicalBottom; if (renderer->view().frameView().hasFlippedBlockRenderers()) { RenderBox* block; if (is<RenderBox>(*renderer)) block = downcast<RenderBox>(renderer); else block = renderer->containingBlock(); physicalStart = block->flipForWritingMode(logicalTop); physicalEnd = block->flipForWritingMode(logicalBottom); } LayoutUnit physicalExtent = absoluteValue(physicalEnd - physicalStart); physicalStart = std::min(physicalStart, physicalEnd); if (renderer->style().isHorizontalWritingMode()) { physicalStart += offset.y(); if (physicalStart >= rect.maxY() || physicalStart + physicalExtent <= rect.y()) return false; } else { physicalStart += offset.x(); if (physicalStart >= rect.maxX() || physicalStart + physicalExtent <= rect.x()) return false; } return true; }
/* ** Fill the tab with malus for each direction which is less than 10 cases from an ennemy */ void Legend::getEnnemiesDirections(int directions[4], bool const checkIfSameLine) const { size_t i = 0; int distanceX; int distanceY; int bonusHits = 0; size_t const x = _character->getX(); size_t const y = _character->getY(); while (i < 4) { directions[i] = 0; i += 1; } i = 0; while (i < _ennemies.size()) { distanceX = x - _ennemies[i]->getX(); distanceY = y - _ennemies[i]->getY(); if (distanceX || distanceY) { if (checkIfSameLine && (!distanceX || !distanceY)) bonusHits += DISTANCE_ENNEMY_MAX; if (absoluteValue(distanceX) + absoluteValue(distanceY) < DISTANCE_ENNEMY_MAX) { if (distanceX > 0 && DISTANCE_ENNEMY_MAX - distanceX > directions[LEFT]) directions[LEFT] = DISTANCE_ENNEMY_MAX - distanceX; if (distanceX < 0 && DISTANCE_ENNEMY_MAX + distanceX > directions[RIGHT]) directions[RIGHT] = DISTANCE_ENNEMY_MAX + distanceX; if (distanceY > 0 && DISTANCE_ENNEMY_MAX - distanceY > directions[UP]) directions[UP] = DISTANCE_ENNEMY_MAX - distanceY; if (distanceY < 0 && DISTANCE_ENNEMY_MAX + distanceY > directions[DOWN]) directions[DOWN] = DISTANCE_ENNEMY_MAX + distanceY; } } i += 1; } i = 0; if (!checkIfSameLine) while (i < 4) { if (directions[i]) directions[i] += MALUS_NEAR_ENNEMY; i += 1; } directions[0] += bonusHits; }
float squareRoot (float x) { const float epsilon = .00001; float guess = 1.0; while ( absoluteValue (guess * guess - x) >= epsilon ) guess = ( x / guess + guess ) / 2.0; return guess; }
void motorX(double xAccel) { if(absoluteValue(xAccel) < ACCEL_SENSITIVITY_BUFFER) { return; } //zeroing out hover speed when quad copter is stable else if((int)xAccel == 0) { //xChange_i = 0; if(x_hover_adjust > 0) x_hover_adjust--; else if(x_hover_adjust < 0) x_hover_adjust++; } else if(xAccel > 0) { x_hover_adjust++; } else if (xAccel < 0) { x_hover_adjust--; } //Hover speed adjust //Determines different motor power distrubution. Some motors are weaker than others. if(x_hover_adjust > motorxy_hoveradjust_thresh) { motorSpeedHover[0]+= motorxy_hover_increment; motorSpeedHover[1]-= motorxy_hover_increment; //digitalWrite(ledpin, HIGH); // turn ON the LED x_hover_adjust = 0; }else if(x_hover_adjust < (-motorxy_hoveradjust_thresh)) { motorSpeedHover[0]-= motorxy_hover_increment; motorSpeedHover[1]+= motorxy_hover_increment; //digitalWrite(ledpin, HIGH); // turn ON the LED x_hover_adjust = 0; } xChange_i += xAccel; //increase integral portion //xChange_i = xChange_d+xAccel; //Calculating derivative Current acc-last double xDiff = xAccel - xChange_d; xChange_d = xAccel; //remember this accel value for next iteration double propTemp = kp*xAccel; double intTemp = ki* xChange_i; double dervTemp = kd*xDiff; double totalAdjustment = (propTemp+intTemp+dervTemp); // double offset = 0;//(motorSpeedHover[0]-motorSpeedHover[1])/2; //At the end of every control/stabalize loop motorSpeedCorrection is set to motorSpeedHover. This way when control is called. motorspeedcorrection is changed and then stabalize makes necessary adjustments prior to turning copter. motorSpeedCorrection[0] = motorSpeedCorrection[0] + (totalAdjustment + offset); motorSpeedCorrection[1] = motorSpeedCorrection[1] - (totalAdjustment + offset); return; }
float squareRoot(float x, float epsilon) { float guess = 1.0; while (absoluteValue(guess * guess - x) >= epsilon) guess = (x / guess + guess) / 2.0; return guess; }
void motorY(double yAccel) { if(absoluteValue(yAccel) < ACCEL_SENSITIVITY_BUFFER) { return; } //zeroing out hover speed when quad copter is stable else if((int)yAccel == 0) { //yChange_i = 0; if(y_hover_adjust > 0) y_hover_adjust--; else if(y_hover_adjust<0) y_hover_adjust++; } else if(yAccel>0) { y_hover_adjust++; } else if (yAccel<0) { y_hover_adjust--; } //Hover speed adjust if(y_hover_adjust > motorxy_hoveradjust_thresh) { motorSpeedHover[2]+= motorxy_hover_increment; motorSpeedHover[3]-= motorxy_hover_increment; //digitalWrite(ledpin, HIGH); // turn ON the LED y_hover_adjust = 0; }else if(y_hover_adjust < (-motorxy_hoveradjust_thresh)) { motorSpeedHover[2]-= motorxy_hover_increment; motorSpeedHover[3]+= motorxy_hover_increment; //digitalWrite(ledpin, LOW); // turn ON the LED y_hover_adjust = 0; } yChange_i += yAccel; //increase integral portion //yChange_i = yChange_d + yAccel; //Calculating derivative Current acc-last double yDiff = yAccel - yChange_d; yChange_d = yAccel; //remember this accel value for next iteration double propTemp = kp*yAccel; double intTemp = ki* yChange_i; double dervTemp = kd*yDiff; double totalAdjustment = (propTemp+intTemp+dervTemp); //Determines different motor power distrubution. Some motors are weaker than others. double offset = 0;//(motorSpeedHover[2]-motorSpeedHover[3])/2; motorSpeedCorrection[2] = motorSpeedCorrection[2] + (totalAdjustment + offset); motorSpeedCorrection[3] = motorSpeedCorrection[3] - (totalAdjustment + offset); return; }
double squareRoot (double x) { const double epsilon = .00001; double guess = 1.0; while ( absoluteValue (guess * guess - x) >= epsilon ) guess = ( x / guess + guess ) / 2.0; return guess; }
float squareRoot (float n) { float guess = 1.0; float epsilon = .0001; while ( absoluteValue ((guess * guess / n) - 1) > epsilon ) guess = (guess + n / guess) / 2.0; return guess; }
float squareRoot (float x) { const float epsilon = .00001; float guess = 1.0; while ( absoluteValue (guess * guess - x) >= epsilon ) { guess = ( x / guess + guess ) / 2.0; printf("Guess = %f \n", guess); } return guess; }
float SquareRoot(float x) { float guess = 1.0; const float epsilon = .00001; while (absoluteValue(guess * guess - x) >= epsilon) { guess = (x / guess + guess) / 2.0; } return guess; }
double squareRoot(double x) { static const double epsilon = 0.000001; double guess = 1.0; static int count = 1; while (absoluteValue(guess * guess - x) >= epsilon) { printf("%i: %f\n", guess, count++); guess = (x / guess + guess) / 2.0; } return guess; }
bool RenderLineBoxList::rangeIntersectsRect(RenderBoxModelObject* renderer, LayoutUnit logicalTop, LayoutUnit logicalBottom, const LayoutRect& rect, const LayoutPoint& offset) const { LayoutUnit physicalStart = logicalTop; LayoutUnit physicalEnd = logicalBottom; LayoutUnit physicalExtent = absoluteValue(physicalEnd - physicalStart); physicalStart = std::min(physicalStart, physicalEnd); physicalStart += offset.y(); if (physicalStart >= rect.maxY() || physicalStart + physicalExtent <= rect.y()) return false; return true; }
float squareRoot(float x) { float s = 2; float result = (x/s + s)/2; while(absoluteValue(result-s) >= .001) { //uses another function that returns the absolute value s = result; result = (x/s + s)/2; cout << result << endl; } return result; }
float squareRoot (float x) { const float episolon = .00001; float guess = 1.0; float absoluteValue (float x); if ( x < 0) { printf ("Negative argument to squareRoot.\n"); return -1.0; } while ( absoluteValue ( guess * guess - x) >= episolon ) guess = ( x / guess + guess ) / 2.0; return guess; }
float squareRoot (float x) { const float epsilon = 0.00001; float guess = 1.0; if ( x < 0 ) { printf ("Negative argument to squareRoot\n"); return -1.0; } while ( absoluteValue ((guess * guess) / x - 1) >= epsilon ) guess = ( x / guess + guess ) / 2.0; return guess; }
float squareRoot (float x) { const double epsilon = .000001; float guess = 1.0; float absoluteValue (float x); if ( x < 0 ) { printf ("Roots are imaginary\n"); return -1.0; } while ( absoluteValue ((guess * guess) / x - 1.0) >= epsilon ){ guess = ( x / guess + guess ) / 2.0; } return guess; }
bool LineBoxList::rangeIntersectsRect(LineLayoutBoxModel layoutObject, LayoutUnit logicalTop, LayoutUnit logicalBottom, const CullRect& cullRect, const LayoutPoint& offset) const { LineLayoutBox block; if (layoutObject.isBox()) block = LineLayoutBox(layoutObject); else block = layoutObject.containingBlock(); LayoutUnit physicalStart = block.flipForWritingMode(logicalTop); LayoutUnit physicalEnd = block.flipForWritingMode(logicalBottom); LayoutUnit physicalExtent = absoluteValue(physicalEnd - physicalStart); physicalStart = std::min(physicalStart, physicalEnd); if (layoutObject.style()->isHorizontalWritingMode()) { physicalStart += offset.y(); return cullRect.intersectsVerticalRange(physicalStart, physicalStart + physicalExtent); } else { physicalStart += offset.x(); return cullRect.intersectsHorizontalRange(physicalStart, physicalStart + physicalExtent); } }
bool LineBoxList::rangeIntersectsRect(LayoutBoxModelObject* renderer, LayoutUnit logicalTop, LayoutUnit logicalBottom, const LayoutRect& rect, const LayoutPoint& offset) const { LayoutBox* block; if (renderer->isBox()) block = toLayoutBox(renderer); else block = renderer->containingBlock(); LayoutUnit physicalStart = block->flipForWritingMode(logicalTop); LayoutUnit physicalEnd = block->flipForWritingMode(logicalBottom); LayoutUnit physicalExtent = absoluteValue(physicalEnd - physicalStart); physicalStart = std::min(physicalStart, physicalEnd); if (renderer->style()->isHorizontalWritingMode()) { physicalStart += offset.y(); if (physicalStart >= rect.maxY() || physicalStart + physicalExtent <= rect.y()) return false; } else { physicalStart += offset.x(); if (physicalStart >= rect.maxX() || physicalStart + physicalExtent <= rect.x()) return false; } return true; }
double calculate(int numInputTokens, char **inputString){ int i; double result = 0.0; char *s; struct DynArr *stack; double num; //set up the stack stack = createDynArr(20); // start at 1 to skip the name of the calculator calc for(i=1;i < numInputTokens;i++) { s = inputString[i]; // Hint: General algorithm: // (1) Check if the string s is in the list of operators. // (1a) If it is, perform corresponding operations. // (1b) Otherwise, check if s is a number. // (1b - I) If s is not a number, produce an error. // (1b - II) If s is a number, push it onto the stack if(strcmp(s, "+") == 0){ add(stack); printf("Adding\n"); } else if(strcmp(s,"-") == 0){ subtract(stack); printf("Subtracting\n"); } else if(strcmp(s, "/") == 0){ divide(stack); printf("Dividing\n"); } else if(strcmp(s, "x") == 0){ multiply(stack); printf("Multiplying\n"); } else if(strcmp(s,"^") == 0){ power(stack); printf("Power\n"); } else if(strcmp(s, "^2") == 0){ squared(stack); printf("Squaring\n"); } else if(strcmp(s, "^3") == 0){ cubed(stack); printf("Cubing\n"); } else if(strcmp(s, "abs") == 0){ absoluteValue(stack); printf("Absolute value\n"); } else if(strcmp(s, "sqrt") == 0){ squareRoot(stack); printf("Square root\n"); } else if(strcmp(s, "exp") == 0){ exponential(stack); printf("Exponential\n"); } else if(strcmp(s, "ln") == 0){ naturalLog(stack); printf("Natural Log\n"); } else if(strcmp(s, "log") == 0){ logBase10(stack); printf("Log\n"); } else{ // FIXME: You need to develop the code here (when s is not an operator) // Remember to deal with special values ("pi" and "e") //check if not a number if (isNumber(s, &num) == 0){ if (strcmp(s, "pi") == 0){ num = 3.14159265; } else if (strcmp(s, "e") == 0){ num = 2.7182818; } else{ //wrong printf("%s is not valid (number or operator) \n", s); break; } } pushDynArr(stack, num); } } //end for /* FIXME: You will write this part of the function (2 steps below) * (1) Check if everything looks OK and produce an error if needed. * (2) Store the final value in result and print it out. */ if (sizeDynArr(stack) != 1) { printf("Incorrect count of numbers is detected! Calculations CANNOT be preformed. "); return 0; } else { result = topDynArr(stack); } return result; }
void solveIK(const AwPoint &startJointPos, const AwPoint &midJointPos, const AwPoint &effectorPos, const AwPoint &handlePos, const AwVector &poleVector, double twistValue, AwQuaternion &qStart, AwQuaternion &qMid) // // This is method that actually computes the IK solution. // { // vector from startJoint to midJoint AwVector vector1 = midJointPos - startJointPos; // vector from midJoint to effector AwVector vector2 = effectorPos - midJointPos; // vector from startJoint to handle AwVector vectorH = handlePos - startJointPos; // vector from startJoint to effector AwVector vectorE = effectorPos - startJointPos; // lengths of those vectors double length1 = vector1.length(); double length2 = vector2.length(); double lengthH = vectorH.length(); // component of the vector1 orthogonal to the vectorE AwVector vectorO = vector1 - vectorE*((vector1*vectorE)/(vectorE*vectorE)); ////////////////////////////////////////////////////////////////// // calculate q12 which solves for the midJoint rotation ////////////////////////////////////////////////////////////////// // angle between vector1 and vector2 double vectorAngle12 = vector1.angle(vector2); // vector orthogonal to vector1 and 2 AwVector vectorCross12 = vector1^vector2; double lengthHsquared = lengthH*lengthH; // angle for arm extension double cos_theta = (lengthHsquared - length1*length1 - length2*length2) /(2*length1*length2); if (cos_theta > 1) cos_theta = 1; else if (cos_theta < -1) cos_theta = -1; double theta = acos(cos_theta); // quaternion for arm extension AwQuaternion q12(theta - vectorAngle12, vectorCross12); ////////////////////////////////////////////////////////////////// // calculate qEH which solves for effector rotating onto the handle ////////////////////////////////////////////////////////////////// // vector2 with quaternion q12 applied vector2 = vector2.rotateBy(q12); // vectorE with quaternion q12 applied vectorE = vector1 + vector2; // quaternion for rotating the effector onto the handle AwQuaternion qEH(vectorE, vectorH); ////////////////////////////////////////////////////////////////// // calculate qNP which solves for the rotate plane ////////////////////////////////////////////////////////////////// // vector1 with quaternion qEH applied vector1 = vector1.rotateBy(qEH); if (vector1.isParallel(vectorH)) // singular case, use orthogonal component instead vector1 = vectorO.rotateBy(qEH); // quaternion for rotate plane AwQuaternion qNP; if (!poleVector.isParallel(vectorH) && (lengthHsquared != 0)) { // component of vector1 orthogonal to vectorH AwVector vectorN = vector1 - vectorH*((vector1*vectorH)/lengthHsquared); // component of pole vector orthogonal to vectorH AwVector vectorP = poleVector - vectorH*((poleVector*vectorH)/lengthHsquared); double dotNP = (vectorN*vectorP)/(vectorN.length()*vectorP.length()); if (absoluteValue(dotNP + 1.0) < kEpsilon) { // singular case, rotate halfway around vectorH AwQuaternion qNP1(kPi, vectorH); qNP = qNP1; } else { AwQuaternion qNP2(vectorN, vectorP); qNP = qNP2; } } ////////////////////////////////////////////////////////////////// // calculate qTwist which adds the twist ////////////////////////////////////////////////////////////////// AwQuaternion qTwist(twistValue, vectorH); // quaternion for the mid joint qMid = q12; // concatenate the quaternions for the start joint qStart = qEH*qNP*qTwist; }
void ik2Bsolver::solveIK(const MPoint &startJointPos, const MPoint &midJointPos, const MPoint &effectorPos, const MPoint &handlePos, const MVector &poleVector, double twistValue, MQuaternion &qStart, MQuaternion &qMid, const double & softDistance, const double & restLength1, const double & restLength2, double & stretching) // // This is method that actually computes the IK solution. // { // vector from startJoint to midJoint MVector vector1 = midJointPos - startJointPos; vector1 = vector1.normal() * restLength1; // vector from midJoint to effector MVector vector2 = effectorPos - midJointPos; vector2 = vector2.normal() * restLength2; // vector from startJoint to handle MVector vectorH = handlePos - startJointPos; // vector from startJoint to effector MVector vectorE = effectorPos - startJointPos; // lengths of those vectors const double length1 = restLength1; const double length2 = restLength2; const double lengthH = vectorH.length(); // component of the vector1 orthogonal to the vectorE const MVector vectorO = vector1 - vectorE*((vector1*vectorE)/(vectorE*vectorE)); ////////////////////////////////////////////////////////////////// // calculate q12 which solves for the midJoint rotation ////////////////////////////////////////////////////////////////// // angle between vector1 and vector2 const double vectorAngle12 = vector1.angle(vector2); // vector orthogonal to vector1 and 2 const MVector vectorCross12 = vector1^vector2; const double lengthHsquared = lengthH * lengthH; double weight = 0.0; double slowLH = lengthH;// / (1.0 + (6.8 - softDistance) / (restLength1 + restLength2)); const double da = restLength1 + restLength2 - softDistance; if(slowLH > da) { float s = (slowLH - da) / softDistance; if(s> 1.f) s= 1.f; Vector3F pt = m_herm.interpolate(s); // MGlobal::displayInfo(MString("herm ")+ pt.y); // approading l1+l2 slower // weight = 1.0 - exp(-(slowLH - da) / softDistance * 6.98); weight = pt.y * weight + (1.f - pt.y) * s; slowLH = da + softDistance * weight; // MGlobal::displayInfo(MString("wei ")+weight); } // // 1 // / \ // l1 / \ l2 // / \ ---l1---1 ---l2--- // 0 ------ l ------ 2 0 ------ l ------- 2 // // angle for arm extension double cos_theta = (slowLH * slowLH - length1*length1 - length2*length2) / (2*length1*length2); if (cos_theta > 1) cos_theta = 1; else if (cos_theta < -1) cos_theta = -1; const double theta = acos(cos_theta); // quaternion for arm extension MQuaternion q12(theta - vectorAngle12, vectorCross12); ////////////////////////////////////////////////////////////////// // calculate qEH which solves for effector rotating onto the handle ////////////////////////////////////////////////////////////////// // vector2 with quaternion q12 applied vector2 = vector2.rotateBy(q12); // vectorE with quaternion q12 applied vectorE = vector1 + vector2; // quaternion for rotating the effector onto the handle MQuaternion qEH(vectorE, vectorH); if(lengthH > vectorE.length()) { // MGlobal::displayInfo(MString("vle ")+(lengthH-vectorE.length())); stretching = (lengthH-vectorE.length()) * weight; } ////////////////////////////////////////////////////////////////// // calculate qNP which solves for the rotate plane ////////////////////////////////////////////////////////////////// // vector1 with quaternion qEH applied vector1 = vector1.rotateBy(qEH); if (vector1.isParallel(vectorH)) // singular case, use orthogonal component instead vector1 = vectorO.rotateBy(qEH); // quaternion for rotate plane MQuaternion qNP; if (!poleVector.isParallel(vectorH) && (lengthHsquared != 0)) { // component of vector1 orthogonal to vectorH MVector vectorN = vector1 - vectorH*((vector1*vectorH)/lengthHsquared); // component of pole vector orthogonal to vectorH MVector vectorP = poleVector - vectorH*((poleVector*vectorH)/lengthHsquared); double dotNP = (vectorN*vectorP)/(vectorN.length()*vectorP.length()); if (absoluteValue(dotNP + 1.0) < kEpsilon) { // singular case, rotate halfway around vectorH MQuaternion qNP1(kPi, vectorH); qNP = qNP1; } else { MQuaternion qNP2(vectorN, vectorP); qNP = qNP2; } } ////////////////////////////////////////////////////////////////// // calculate qTwist which adds the twist ////////////////////////////////////////////////////////////////// MQuaternion qTwist(twistValue, vectorH); // quaternion for the mid joint qMid = q12; // concatenate the quaternions for the start joint qStart = qEH*qNP*qTwist; }
double calculate(int numInputTokens, char **inputString) { int i; double result = 0.0; char *s; struct DynArr *stack; //for keeping track of # of operands and operators int numOfOperands = 0; int numOfOperators = 0; int check = 0; //set up the stack stack = createDynArr(20); // start at 1 to skip the name of the calculator calc for(i=1;i < numInputTokens;i++) { s = inputString[i]; if(strcmp(s, "+") == 0) { numOfOperators++; //increase for binary operators add(stack); } else if(strcmp(s,"-") == 0) { numOfOperators++; subtract(stack); } else if(strcmp(s, "/") == 0) { numOfOperators++; divide(stack); } else if(strcmp(s, "x") == 0) { numOfOperators++; multiply(stack); } else if(strcmp(s, "^") == 0) { numOfOperators = numOfOperators; //for unary operators don't increase power(stack); } else if(strcmp(s, "^2") == 0) { numOfOperators = numOfOperators; square(stack); } else if(strcmp(s, "^3") == 0) { numOfOperators = numOfOperators; cube(stack); } else if(strcmp(s, "abs") == 0) { numOfOperators = numOfOperators; absoluteValue(stack); } else if(strcmp(s, "sqrt") == 0) { numOfOperators = numOfOperators; squareRoot(stack); } else if(strcmp(s, "exp") == 0) { numOfOperators = numOfOperators; exponential(stack); } else if(strcmp(s, "ln") == 0) { numOfOperators = numOfOperators; naturalLog(stack); } else if(strcmp(s, "log") == 0) { numOfOperators = numOfOperators; logTen(stack); } else { //check if its a number (or pi or e) if(isNumber(s, &result)) { numOfOperands++; //increase count of operands pushDynArr(stack, result); //if it is push onto stack } else { printf("You entered bad characters, exiting!\n"); //else print error message exit(0); } } } //end for //check for correct balance of operands and operators //must be one more operand than operators in sequence check = numOfOperands - numOfOperators; //this must be equal to 1 for valid input if (check > 1) { printf("Illegal Input, too many operands or too few operators, exiting\n"); exit(0); } else if (check < 1) { printf("Illegal Input, too few operands or too many operators, exiting\n"); exit(0); } //if passed check store final result and return result = topDynArr(stack); return result; }