on SolveQuadratic(a, b, c) ------------------------------------------- -- INPUT: , and should be numbers corresponding to the -- indices of the quadratic equation a*x*x + b*x + c = 0. -- If b and c are not numbers, they will be assumed to be -- zero. If a is not a number an error will be returned. -- ACTION: Uses the formula x = -b ± sqrt(b*b - 4*a*c) / (2*a) to -- calculate the real roots of the equation -- a*x*x + b*x + c = 0 -- OUTPUT: Returns a linear list of real roots in ascending order. -- This list may contain zero, one or two floating point -- numbers. If a is not a number, returns an error symbol. -------------------------------------------------------------------- vRoots = [] case ilk(a) of #float: -- continue #integer: -- Ensure that the determinant and the divisor are floats so -- that the output will be in floating point format. a = float(a) otherwise: return #quadraticExpected end case if not ilk(b, #number) then b = 0 end if if not ilk(c, #number) then c = 0 end if -- Calculate the determinant vSquare = b * b - 4 * a * c if vSquare < 0 then -- There are no real solutions. Leave vRoots empty. else if vSquare = 0 then -- There is only one real root vRoot = -b / (2 * a) vRoots.append(vRoot) else -- Calculate both roots vSquareRoot = sqrt(vSquare) vRoot = (-b - vSquareRoot) / (2 * a) vRoots.append(vRoot) vRoot = (-b + vSquareRoot) / (2 * a) vRoots.append(vRoot) end if return vRoots end SolveQuadratic on createQuadratic(dataList) ----------------------------------------- -- PARAMETER: is a property list with the format: -- [#a: , -- #b: , -- #c: , -- #xMin: , -- #xMax: , -- #xScale: , -- #yScale: , -- #member: ] -- -- None of the properties are essential. Default values will be -- used for those which are missing. -- -- ACTION: Plots a graph with the equation y = ax^2 + bx + c, -- with x in the range xMin-xMax in the chosen vector shape member. -- x and y axes are also plotted. -- -- DEFAULTS: If no parameters are used, creates the curve y = x^2 in -- the range -4 - 4, with a scale of x10 -- -- RETURNS: the Vector Shape member containing the graph -------------------------------------------------------------------- -- Use defaults for any invalid property values if ilk(dataList) <> #propList then dataList = [:] end if a = dataList[#a] case ilk(a) of #integer, #float: -- correct ilk otherwise: a = 1 end case b = dataList[#b] case ilk(b) of #integer, #float: -- correct ilk otherwise: b = 0 end case c = dataList[#c] case ilk(c) of #integer, #float: -- correct ilk otherwise: c = 0 end case xMin = dataList[#xMin] case ilk(xMin) of #integer, #float: -- correct ilk otherwise: xMin = -4 end case xMax = dataList[#xMax] case ilk(xMax) of #integer, #float: -- correct ilk otherwise: xMax = 4 end case xScale = dataList[#xScale] case ilk(xScale) of #integer, #float: -- correct ilk otherwise: xScale = 10 end case yScale = dataList[#yScale] case ilk(yScale) of #integer, #float: -- correct ilk otherwise: yScale = 10 end case vectorMember = dataList[#member] if ilk(vectorMember) <> #member then vectorMember = new(#vectorShape) else if vectorMember.type <> #vectorShape then vectorMember = new(#vectorShape) end if -- Adjust values so that scaling works correctly b = b * xScale c = c * xScale * xScale xMin = integer(xMin * xScale) xMax = integer(xMax * xScale) yScale = float(yScale) / (xScale*xScale) yMin = the maxInteger yMax = -the maxInteger vertices = [] -- Determine x,y values for graph repeat with x = xMin to xMax y = a*x*x + b*x + c y = -y -- correct screen coordinates to cartesian coordinates y = y * yScale vertices.append([#vertex: point(x, y)]) -- Determine maximum and minimum values for y if yMin > y then yMin = y end if if yMax < y then yMax = y end if end repeat if not ((the globals).version starts "7") then -- Ensure that axes meet at point(0, 0) if xMin > 0 then xMin = 0 else if xMax < 0 then xMax = 0 end if if yMin > 0 then yMin = 0 else if yMax < 0 then yMax = 0 end if -- Show axes vertices.append([#newCurve]) vertices.append([#vertex: point(xMin, 0)]) vertices.append([#vertex: point(xMax, 0)]) vertices.append([#newCurve]) vertices.append([#vertex: point(0, yMin)]) vertices.append([#vertex: point(0, yMax)]) end if -- Plot the graph vectorMember.vertexList = vertices return vectorMember end