DIRECTORY Algebra3d, Complex, Matrix3d, RealFns, SV2d, SVVector3d; Algebra3dImpl: PROGRAM IMPORTS Complex, RealFns EXPORTS Algebra3d = BEGIN Point2d: TYPE = Matrix3d.Point2d; Polygon: TYPE = REF PolygonObj; PolygonObj: TYPE = SV2d.PolygonObj; Vec: TYPE = Complex.Vec; Vector: TYPE = SVVector3d.Vector; root3Over2: REAL; -- a loadtime constant LinearFormula: PRIVATE PROC [a, b: REAL] RETURNS [root: REAL, rootCount: NAT] = { IF a = 0 THEN { IF b = 0 THEN {root _ 0.0; rootCount _ 0; RETURN} ELSE {root _ 0.0; rootCount _ 0; RETURN} -- inconsistent case } ELSE { IF b = 0 THEN {root _ 0.0; rootCount _ 1; RETURN} ELSE {root _ -b/a; rootCount _ 1; RETURN}; }; }; QuadraticFormula: PUBLIC PROC [a, b, c: REAL] RETURNS [roots: ARRAY [1..2] OF REAL, rootCount: NAT] = { discriminant, temp: REAL; IF a = 0 THEN { [roots[1], rootCount] _ LinearFormula [b, c]; RETURN; }; IF c = 0 THEN { roots[1] _ 0.0; [roots[2], rootCount] _ LinearFormula[a, b]; rootCount _ rootCount + 1; IF roots[1] > roots[2] THEN { -- swap them temp _ roots[1]; roots[1] _ roots[2]; roots[2] _ temp; }; RETURN}; discriminant _ b*b - 4*a*c; -- 3 mult, 1 add SELECT discriminant FROM =0 => {rootCount _ 1; roots[1] _ -b/(2*a); RETURN}; -- 1 mult, 1 div (4 mult, 1 div, 1 add total) <0 => {rootCount _ 0; RETURN}; -- (3 mult, 1 add total) >0 => { sqrtRadical: REAL _ RealFns.SqRt[discriminant]; term: REAL; rootCount _ 2; term _ IF b < 0 THEN sqrtRadical - b ELSE -sqrtRadical - b; roots[1] _ term/(2.0*a); roots[2] _ (2.0*c)/term; IF roots[1] > roots[2] THEN { -- swap them temp _ roots[1]; roots[1] _ roots[2]; roots[2] _ temp; }; RETURN}; -- 1 SqRt, 2 mult, 2 div, 2 add (1 SqRt, 5 mult, 2 div, 3 add total) ENDCASE => ERROR; }; -- end of QuadraticFormula ToPolar: PRIVATE PROC [vec: Vec] RETURNS [r: REAL, radians: REAL] = { r _ Complex.Abs[vec]; radians _ Complex.Arg[vec]; }; ComplexRoot: PRIVATE PROC [index: REAL, vec: Vec] RETURNS [rootVec: Vec] = { r, radians, resultR, resultRadians: REAL; [r, radians] _ ToPolar[vec]; resultR _ RealFns.Root[index, r]; resultRadians _ radians/index; rootVec _ Complex.FromPolar[resultR, resultRadians]; }; Init: PROC = { root3Over2 _ RealFns.SqRt[3]/2.0; }; Init[]; END. @File: Algebra3dImpl.mesa Last edited by Bier on December 18, 1982 1:37 am Author: Eric Bier on August 4, 1983 3:36 pm Contents: The quadratic formula, cubic formula and quartic formula GLOBALS The solution to the equation "ax + b = 0". The solution to the equation "ax2+bx+c=0". If a=0 this is just a linear equation. If c = 0, one root is zero and we solve a linear equation. Otherwise, we use the quadratic formula in either the form [-b+-(b2-4ac)1/2]/2a or (2c)/[-b-+(b2-4ac)1/2] depending on the sign of b. We will arrange the roots so that roots[1] < roots[2]. ΚŽ– "Cedar" style˜Ihead1šœ™Iprocšœ0™0Lšœ+™+LšœB™BšœΟk ˜ Lšœ ˜ L˜Lšœ ˜ Lšœ˜Lšœ˜Lšœ ˜ —šœ˜Lšœ˜Lšœ ˜—Lšœ˜˜Lšœ!˜!Lšœ œœ ˜Lšœ œœ ˜#L˜Lšœœ˜"Ihead2™Lšœ œΟc˜(—Lšœ˜šΟn œœœœœœ œ˜QL™*šœœ˜Lšœœœ˜1Lšœœž˜=L˜—šœ˜Lšœœœ˜2Lšœœ˜*L˜—Lšœ˜—šŸœœœ œœ œœœ œ˜gIlead1š œ Οuœ± œ œ œ œU™ΝLšœœ˜šœœ˜Lšœ-˜-Lšœ˜L˜—šœœ˜Lšœ˜Lšœ,˜,Lšœ˜šœ*˜*L˜L˜L˜L˜—Lšœ˜—Lšœž˜,Lšœ˜šœ˜Lšœ˜Lšœž-˜6—šœ˜Lšœž˜!—šœ˜Lšœ œ˜/Lšœœ˜ Lšœ˜Lšœœ'˜;Lšœ˜L˜šœ*˜*L˜L˜L˜L˜—LšœžD˜M—Lšœœ˜Lšœž˜—Lšœ˜š Ÿœœœ œœ œ˜ELšœ˜Lšœ˜L˜—š Ÿ œœœ œ œ˜LLšœ$œ˜)Lšœ˜Lšœ!˜!Lšœ˜Lšœ4˜4L˜L˜—šŸœœ˜L˜!L˜—L˜Lšœ˜L˜—…—”b