12,453,183 members (63,471 online)
Rate this:
See more:
Hi, developers!
I have a function graph (bmp pictute). I need a way to get F(x) values from this graph.
The problem is looking like very easy one: X value is in range from 0 to 1 and Y value ranges from 0 to 1 and the graph is a curve drawn by hand.
Is there any way to get F(x) value in real time by few lines of code without inventing a cycle?
Posted 17-Mar-13 8:23am
Updated 17-Mar-13 8:32am
v2
Marco Bertschi 17-Mar-13 14:53pm

If the graph is only available as a BMP - nope. You'd have to do a bit of picture analysis to know how the graph looks like (which is required for calc'ing f(x)).

cheers,
Marco Bertschi
Arseniy-developer 17-Mar-13 15:46pm

I can make a graph in any appropriate form if the is any way to extract values from image.
Well.. maybe there is a way to get text file from image?.. point =(1,1) - next point = (2,1) ..etc..
Marco Bertschi 17-Mar-13 16:07pm

So you want to create the graph by using f(x)?
Arseniy-developer 18-Mar-13 6:27am

Dear Marco, vise versa - I want to get values from image http://img254.imageshack.us/img254/1992/graphsample.png
Marco Bertschi 18-Mar-13 7:18am

Now I am confused. I am truly sorry but I will not be able to help you since I simply don't get it. Anyways, I wish you best of luck for your project!

cheers,
Marco Bertschi
ali_crash 17-Mar-13 19:55pm

can you show us one of that graphs for example?
Arseniy-developer 18-Mar-13 4:51am

Here is example graph:
http://img254.imageshack.us/img254/1992/graphsample.png[^]

I need to get valus F(x) for example F(0.5) will be 0.32
Sergey Alexandrovich Kryukov 17-Mar-13 20:43pm

It would be the one of the greatest abuse I ever heard of. Why would anyone need it?
—SA
Arseniy-developer 18-Mar-13 4:51am

The goal is to simulate car wheel angle turning (Y axis is angle from 0 to 1 (60 grad in scaling) ) depending on left and right arrow pressing time (X axis is time from 0 to 1 sec)
For every car model and tuning there is individual wheel reaction function.

It looks like Need for speed have such kind of graps or they wrote some complex functions which is hard for my understanding.
Sergey Alexandrovich Kryukov 18-Mar-13 10:08am

I was asking why getting data from a picture, while it should be the opposite?
—SA
Arseniy-developer 19-Mar-13 4:43am

Sergey I dont know a function formula, but I am able to draw it. And I need to change it in future easy just by changing image quickly (not working with coefficients and formula polinoms)

No. If you know how to draw, you know a function. Not a formula of a function, but a function, data. When you draw, you loose data. But you should not loose data, keep it. After all, use your brain.
—SA
Steve44 18-Mar-13 19:04pm

Hi Arseniy,
I am with Sergey here, the question is why do you provide a hand drawn bitmap to define a F(x) function? Where do these bitmaps come from and are there other ways to have a user enter the coordinate pairs (x, F(x))? Are they visualization of some measurement?
(I can give you guidance how to extract your information from the bitmap, but first I want to know, if there are better ways to solve the whole problem.)
Arseniy-developer 19-Mar-13 4:37am

I just have an idea how mechanical wheel should react on player key pressing and I can express it in Image in paint.exe program or any other graphic or function editor by hand. The function is neither linear nor the power of x, it is maybe some kind of 3rd or 4th power of x with a lot of coefficients which I cannot predict.
Steve44 19-Mar-13 4:52am

OK, I see.
In this case I would recommend that you write a function that does linear approximation of parts of the curve (or some other approximation (e.g. quadratic) that you can model reasonably).
The function would look somehow along this line:
double F(double x)
{
double RetCode;
if(x < 0.2) {
RetCode = a1 * x + b1;
} else if (x < 0.6) {
RetCode = a2 * x + b2;
} else if (x < 0.8) {
RetCode = a3 * x + b3;
} else {
RetCode = a4 * x + b4;
}
return RetCode;
}
and find matching a1, b1, a2, b2, ...

Extracting the values form a hand-drawn pixel image is just not something you should ever reasonably consider. For example, some columns of your image had more than one pixel, what value would you assign in this case?

Rate this:

## Solution 3

OK, it seem like you are bound to have to extract the data from the image, so here some metacode that will extract the function value as defined by the lowest black pixel in each column:

```// Assumptions:
// x is in the range [0.0, 1.0], both inclusive
// f(x) is in the range [0.0, 1.0], both inclusive
// Every column value is defined by the lowest black pixel

public static double f(double x, Bitmap bmap)
{
double RetCode = 1.0; // no pixel, assume top most value 1.0
int positionX = (int)((double)bmap.Width * x + 0.5);
for(int positionY = 0; positionY < bmap.Height; positionY++)
{
Color pix = bmap.GetPixel( positionX, positionY );
if( pix.R == 0 )
{
RetCode = (double)positionY / (double)bmap.Height;
break;
}
}
return RetCode;
}```

You can improve on the value extracted by also scanning the adjacent column and then linear interpolating by the fraction that the real positionX value is off from the integer part, but since this is a hand-drawn function curve, I think approximation by whole pixels is acceptable.
v2
Rate this:

## Solution 2

(Adding this in the solution as I can use the layout tags here.)

OK, I see.
In this case I would recommend that you write a function that does linear approximation of parts of the curve (or some other approximation (e.g. quadratic) that you can model reasonably).
The function would look somehow along this line:
```double F(double x)
{
double RetCode;
if(x < 0.2) {
RetCode = a1 * x + b1;
} else if (x < 0.6) {
RetCode = a2 * x + b2;
} else if (x < 0.8) {
RetCode = a3 * x + b3;
} else {
RetCode = a4 * x + b4;
}
return RetCode;
}
```
and find matching rages with the a1, b1, a2, b2, ...
Extracting the values form a hand-drawn pixel image is just not something you should ever reasonably consider.
v2
Arseniy-developer 20-Mar-13 1:37am

Well, it is pretty good unless you need to change the curve form. You need to change half of coefficients carefully to avoid functing breake :(

Top Experts
Last 24hrsThis month
 Maciej Los 370 OriginalGriff 235 Richard MacCutchan 178 ppolymorphe 163 Dave Kreskowiak 140
 OriginalGriff 6,258 ppolymorphe 3,833 Karthik Bangalore 3,620 Maciej Los 3,375 Richard Deeming 2,835