• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1359
  • Last Modified:

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

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.
  • 3
  • 2
  • 2
1 Solution
Daniel WilsonCommented:
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.

michael-levAuthor Commented:
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...
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Will think some more ... unless ozo beats me to it :)
michael-levAuthor Commented:
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?
x=a*y*y + b*y
a*y*y + b*y - x = 0
y = (-b ± sqrt(b² - 4ax))/2a
y = (-b ± sqrt(b² + 4ax))/2a

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now