public static Quadratic calculateQuadratic(decimal a, decimal b, decimal c) { decimal bNew = b / a, h = bNew / 2, k = (-a * h * h + c); Root roots = calculateRoots(a, b, c); string A = a.ToString(format), B = bNew.ToString(format), H = h.ToString(format), K = k.ToString(format); //Make function to make a string for addition/subtraction? string text; text = "Standard form: y = " + (a != 1 ? a != -1 ? A : "-" : "") + "x^2" + (b > 0 ? (b != 1 ? " + " + b + "x" : " + x") : (b < 0 ? (b != -1 ? " " + B + "x" : " - x") : "")) + (c >= 0 ? " + " + c : " " + c) + "\n"; text += "Vertex form: y = " + (a != 1 ? a != -1 ? A : "-" : "") + (h > 0 ? "(x + " + H + ")" : h < 0 ? "(x " + H + ")" : "x") + "^2" + (k > 0 ? " + " + K : k < 0 ? " " + K : "") + "\n"; text += "Vertex: (" + (-h).ToString(format) + ", " + K + ")\n"; text += (a > 0 ? "Min" : "Max") + " of " + K + " when x = " + (-h).ToString(format) + "\n"; text += "Root(s):\n"; if (roots.imaginary) text += roots.iOne + "\n" + roots.iTwo; else text += (roots.one == roots.two ? roots.one.ToString(format) : roots.one.ToString(format) + "\n" + roots.two.ToString(format)); Quadratic quad = new Quadratic(text, a, bNew, h, k); return quad; }
private void calculate() { decimal a = trimText(aBox.Text), b = trimText(bBox.Text), c = trimText(cBox.Text, false); if (a == 0) { return; } if (quadraticRadio.Checked) { b = trimText(bBox.Text, false); Quadratic q = Quadratics.calculateQuadratic(a, b, c);; answerLabel.Text = q.text; drawGraph(q); } else { answerLabel.Text = Trinomials.calculateTrinomial((int)Math.Round(a), (int)Math.Round(b), (int)Math.Round(c)); } }
private void drawGraph(Quadratic q) { graphBox.Image = new Bitmap(graphBox.Width, graphBox.Height); Graphics graph; graph = Graphics.FromImage(graphBox.Image); graph.Clear(Color.White); Point p1, p2; p1 = new Point((int)(graphBox.Width / 2), 0); p2 = new Point((int)(graphBox.Width / 2), graphBox.Height); graph.DrawLine(black, p1, p2); p2 = new Point(p2.X - 10, p2.Y - 17); graph.DrawString("y", Font, black.Brush, p2); p1 = new Point(0, (int)(graphBox.Height / 2)); p2 = new Point(graphBox.Width, (int)(graphBox.Height / 2)); graph.DrawLine(black, p1, p2); p2 = new Point(p2.X - 13, p2.Y + 2); graph.DrawString("x", Font, black.Brush, p2); List<Point> points = new List<Point>(); int a, num = 20, scale = (int)Math.Abs(q.a / 5); scale = scale == 0 ? 1 : scale; scale *= 2; for (int i = 0; i <= num; ++i) { a = i - num / 2; points.Add(new Point(graphBox.Width / 2 + a * scale, graphBox.Height / 2 - (int)((q.a * (a - q.h) * (a - q.h) + q.k) / scale))); } graph.DrawCurve(blue, points.ToArray()); string s; for (int i = 1; i <= 3; ++i) { s = (i * scale).ToString(); p1 = new Point(graphBox.Width / 2 + 10 * i, graphBox.Height / 2); p2 = new Point(p1.X, p1.Y + 5); graph.DrawLine(black, p1, p2); p2 = new Point(p2.X - (TextRenderer.MeasureText(s, Font).Width / 2), p2.Y + 3); graph.DrawString(s, Font, black.Brush, p2); } for (int i = 1; i <= 3; ++i) { s = (i * scale).ToString(); p1 = new Point(graphBox.Width / 2, graphBox.Height / 2 - 10 * i); p2 = new Point(p1.X - 5, p1.Y); graph.DrawLine(black, p1, p2); p2 = new Point(p2.X - 5 - (TextRenderer.MeasureText(s, Font).Width / 2), p2.Y - 7); graph.DrawString(s, Font, black.Brush, p2); } }
private void drawGraph(Quadratic q) { graphBox.Image = new Bitmap(graphBox.Width, graphBox.Height); Graphics graph; graph = Graphics.FromImage(graphBox.Image); graph.Clear(Color.White); Point p1, p2; p1 = new Point((int)(graphBox.Width / 2), 0); p2 = new Point((int)(graphBox.Width / 2), graphBox.Height); graph.DrawLine(black, p1, p2); p2 = new Point(p2.X - 10, p2.Y - 17); graph.DrawString("y", Font, black.Brush, p2); p1 = new Point(0, (int)(graphBox.Height / 2)); p2 = new Point(graphBox.Width, (int)(graphBox.Height / 2)); graph.DrawLine(black, p1, p2); p2 = new Point(p2.X - 13, p2.Y + 2); graph.DrawString("x", Font, black.Brush, p2); List <Point> points = new List <Point>(); int a, num = 20, scale = (int)Math.Abs(q.a / 5); scale = scale == 0 ? 1 : scale; scale *= 2; for (int i = 0; i <= num; ++i) { a = i - num / 2; points.Add(new Point(graphBox.Width / 2 + a * scale, graphBox.Height / 2 - (int)((q.a * (a - q.h) * (a - q.h) + q.k) / scale))); } graph.DrawCurve(blue, points.ToArray()); string s; for (int i = 1; i <= 3; ++i) { s = (i * scale).ToString(); p1 = new Point(graphBox.Width / 2 + 10 * i, graphBox.Height / 2); p2 = new Point(p1.X, p1.Y + 5); graph.DrawLine(black, p1, p2); p2 = new Point(p2.X - (TextRenderer.MeasureText(s, Font).Width / 2), p2.Y + 3); graph.DrawString(s, Font, black.Brush, p2); } for (int i = 1; i <= 3; ++i) { s = (i * scale).ToString(); p1 = new Point(graphBox.Width / 2, graphBox.Height / 2 - 10 * i); p2 = new Point(p1.X - 5, p1.Y); graph.DrawLine(black, p1, p2); p2 = new Point(p2.X - 5 - (TextRenderer.MeasureText(s, Font).Width / 2), p2.Y - 7); graph.DrawString(s, Font, black.Brush, p2); } }