c# code to solve horizontal parabola, y as func of x=1,2,3,4.., given 3 points, incl. x=0,y=0

Posted on 2009-04-20
Last Modified: 2013-11-07
For a parabola X = A Y * Y + B Y
This is horizontal parabola passing through x=0,y=0
The c#  DotNet 2.0 function should look like
ArrayList yList getNsuccessiveYvalues (int x1, int y1, int x2, int y2, int x3, int y3, yListSize)
It gets 3 points x1,y1,x2,y2,x3,y3 and also a number representing how many Y values should be calculated, for X=1,2,3,4 ...
That is for X starting at 1 in steps of 1.
Actually x1,y1 are 0,0 in my case, though it would be nice if the function could be more general :-).
The returned Y values I need them to be of int type, so the double results should be rounded to the close integers.

an example of actual parameters is: x1=0,y1=0,x2=1,y2=6,x3=20,y3=60,yListSize=50

I want also to show the results as a graph in .NET 2.0 WinForm by GDI.

I would appreciate a solution.
Question by:michael-lev
    LVL 32

    Expert Comment

    by:Daniel Wilson
    For starters:
    • X = A Y * Y + B Y is only guaranteed to pass through (0,0) if B = 0.
    • Are you guaranteed that all 3 points provided will fit such a parabola?  Will you throw an exception if they do not?
    If you know the origin is at (0,0), then the only variable for which you need to solve is A:

    Using (x1,y1) to do so:
    A = (X / (Y*Y));

    Then you would loop from 0 to yListSize or -yListSize depending on whether A is positive or negative.  Knowing Y for the values you're plugging in, you would solve for X.  The resulting pairs are what you place into the list you will return.


    Author Comment

    To DanielWilson:

    1. x=a*y*y + b*y  passes through 0,0  also fo b != 0
        What you wrote does not make sense to me. Maybe you were too fast...
        Anyway, if you solve my question more generally, i.e. also for 1st point other than 0,0
       then of course the horizontal parabola full equation is X=A*Y*Y + B*Y + C

    2. Pay attention that in this equation, after the program evaluates A and B,
        the intuitive solution is to evaluate X as a function of Y, by x=a*y*y + b*y
        But I need to put in the list y values for x = 1, 2, 3, etc.

    So I suspect this is not trivial...
    LVL 84

    Expert Comment

    X = A Y * Y + B Y
    x1 == A * y1 * y1 + B * y1
    x2 == A * y2 * y2 + B * y2
    x1*y2 == A * y1 * y1 * y2 + B * y1 * y2
    x2*y1 == A * y2 * y2 * y1 + B * y2 * y1
    x1*y2 - x2*y1 == A * (y1 * y1 * y2 -  y2 * y2 * y1 ) + B * (y1 * y2 -  y2 * y1)
    A = (x1*y2 - x2*y1) / (y1 * y1 * y2 -  y2 * y2 * y1)
    B = (x1 -  A * y1 * y1) / y1
    LVL 32

    Expert Comment

    by:Daniel Wilson
    1.  Yeah, I wasn't thinking right.

    Will think some more ... unless ozo beats me to it :)

    Author Comment

    to Ozo:

    Please see what I answered Daniel -
    finding A,B is only first step. :-)
    It enables evaluating X as a function of Y, by x=a*y*y + b*y
    But I need to evaluate Y values for series of X values (for X=1, 2, 3, 4, etc)

    So indeed I am still wondering what is the effective\efficient way for doing this?
    LVL 84

    Accepted Solution

    x=a*y*y + b*y
    a*y*y + b*y - x = 0
    y = (-b ± sqrt(b² - 4ax))/2a
    LVL 84

    Expert Comment

    y = (-b ± sqrt(b² + 4ax))/2a

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
    One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now