Trapezoidal Teacups -- Geometry Test (Problem) -- 12-09-2015
Phil Weinstein, CADSWES ... go to fixed
Rectangular | Trapezoidal Normal |
Trapezoidal Rectangular |
Trapezoidal Inverted |
Code |
// returns success indication bool TeacupGfxItem::computeTrapezoidValueDims ( TcupGeom geom, // Input double pix2PerUnitVal, // Input, square pixel per unit val double value, // Input double& retValueHeight, // Returned double& retValueWidth) const // Returned ... ... ... ... ... ... // ********************************** // *** (1) Compute Value Height *** // ********************************** // All measurements are made with respect to the "Reference" trapezoid. // Both the "Reference" and "Value" trapeziods have the same botWid. // // KNOWN: // Reference Trapezoid: maxTopWid, botWid, refHgt // Value Trapezoid: botWid, valArea // // SOLVE FOR: // Value Trapezoid: valHgt (retValueHeight) // // Equation 1 (trapezoid): // valArea == 0.5 * (valTopWid + botWid) * valHgt // 2 * valArea == (valTopWid + botWid) * valHgt // // Equation 2 (congruent): // (valTopWid / valHgt) == (maxTopWid / refHgt) // valTopWid == valHgt * (maxTopWId / refHgt) // // Combined Equations, Format as Quadratic Equation // 2 * valArea == ((valHgt * (maxTopWid / refHgt)) + botWid) * valHgt // 2 * valArea == (valHgt^2 * (maxTopWid / refHgt)) + (valHgt * botWid) // 0 == ((maxTopWid/refHgt) * valHgt^2) + (botWid*valHgt) + (-2*valArea) // // Apply Quadratic Forumla // a == maxTopWid/refHgt // b == botWid // c == (-2 * valArea) // x == -b +- sqrt (b^2 - 4ac) / 2a const double a = maxTopWid / refHgt; const double b = botWid; const double c = (-2.0 * valArea); const double squareTerm = (b * b) - (4.0 * a * c); if (squareTerm < 0.0) return (false); //----->> const double rootTerm = sqrt (squareTerm); const double x1 = (-b - rootTerm) / (2.0 * a); const double x2 = (-b + rootTerm) / (2.0 * a); retValueHeight = std::max (x1, x2); // ********************************* // *** (2) Compute Value Width *** // ********************************* retValueWidth = (retValueHeight * (maxTopWid - botWid) / refHgt) + botWid; // std::cout << mname << " [#" << callCnt << "]" // << " val " << value // << ", valArea " << valArea // << ", p2perV " << pix2PerUnitVal // << ", refHgt " << refHgt // << ", hgt1 " << x1 // << ", hgt2 " << x2 // << ", botWid " << botWid // << ", topWid " << maxTopWid // << ", wid " << retValueWidth // << std::endl; return true; }--- (end) ---