0

I would like to provide the vertex and two points for a parabola to find the a, b and c values.

Goal

The user input (the X value of the vertex) will always have a 'score' of 100

Background

I read this article, which by the way, got me quite a bit further than what I was doing ... Thank You... but I think I misinterpreted the gist of the article. I was under the assumption that the vertex would be one of the three points - boy was I wrong. :-(

How to calculate the vertex of a parabola given three points

my code

The code for putting the text boxes on the form is not here... but you get the gist of it.,

using System;
using System.Windows.Forms;

namespace Score
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        int min { get; set; }
        int ideal { get; set; }
        int max { get; set; }

        int m { get; set; }
        double a { get; set; }
        double b { get; set; }
        double c { get; set; }


        private void button2_Click(object sender, EventArgs e)
        {
            GetValues();
            CalcParabolaValues();
            CalcUserVal();
        }

        private void GetValues()
        {
            int temp_min = Convert.ToInt16(textBox_Min.Text);     //  50, 0
            int temp_ideal = Convert.ToInt16(textBox_Ideal.Text); //  75, 100
            int temp_max = Convert.ToInt16(textBox_Max.Text);     // 100, 0

            min = temp_min;
            ideal = temp_ideal;
            max = temp_max;

            if (temp_ideal == temp_min || temp_ideal == temp_max)
            {
                // adjust the vertex to be the mid point between min and max, as it can't be min or max
                ideal = temp_min + ((temp_max - temp_min) / 2);
            }
        }

        public void CalcParabolaValues()
        {
            int x1 = min;
            int y1 = 0;
            int x2 = ideal;
            int y2 = 100;
            int x3 = max;
            int y3 = 0;

            double denom = (x1 - x2) * (x1 - x3) * (x2 - x3);
            a = (x3 * (y2 - y1) + x2 * (y1 - y3) + x1 * (y3 - y2)) / denom;
            b = (x3 * x3 * (y1 - y2) + x2 * x2 * (y3 - y1) + x1 * x1 * (y2 - y3)) / denom;
            c = (x2 * x3 * (x2 - x3) * y1 + x3 * x1 * (x3 - x1) * y2 + x1 * x2 * (x1 - x2) * y3) / denom;

            double xVertex = -b / (2 * a);
            double yVertex = c - b * b / (4 * a);

            textBox_A.Text = a.ToString();
            textBox_B.Text = b.ToString();
            textBox_C.Text = c.ToString();
            textBox_yVertex.Text = yVertex.ToString();
            textBox_xVertex.Text = xVertex.ToString();
        }

        private void CalcUserVal()
        {
            int X = Convert.ToInt16(textBox_UserVal.Text);
            double y = (a * (X * X)) + (b * X) + c;
            textBox_Score.Text = Math.Abs(y).ToString();
        }
    }
}

Missing link?

I think this ties in somehow... but I am not sure

y=ax2+bx+c

Running program

This is what I get when I start messing around with the numbers.

I would like the vertex to be at (in this example) 85:

enter image description here

However, when I click the process button, the vertex is at 75

enter image description here

When I enter a user value of 65, the value is 100

enter image description here

When I enter a user value of 85, the value is 100

enter image description here

MLissCetrus
  • 423
  • 3
  • 21
  • Could you add a formula or two to define "vertex" and show what you mean by `a`, `b`, and `c`? – Beta May 15 '20 at 00:51
  • https://stackoverflow.com/users/128940/beta - did you read the stack overflow link I put in my question? ( I would not like to repeat what they wrote ) – MLissCetrus May 16 '20 at 16:54

0 Answers0