How do I create an algoritm

T

Tony

Hello!
Assume I have on the x-axes the display area from x=40 pixels to x=750
pixels
If I now want to set a lot of point in this coordinate system.
For example if I have x-value 0 this would be positioned at x = 40 and
x-value 800 would be positioned at x=750.
So I have to transform my x value into the coordinate system where origo for
x is 40 and x max is 800.

I can't find a suitable algoritm that can transform my x values into this
coordinate system.

//Tony
 
M

michael.boehnisch

Am Montag, 25. Februar 2013 13:03:19 UTC+1 schrieb Tony:
Assume I have on the x-axes the display area from x=40 pixels to x=750
pixels

If I now want to set a lot of point in this coordinate system.
For example if I have x-value 0 this would be positioned at x = 40 and
x-value 800 would be positioned at x=750.
So I have to transform my x value into the coordinate system where origo for
x is 40 and x max is 800.

I can't find a suitable algoritm that can transform my x values into this
coordinate system.

You need to perform a simple linear transformation.
If x is a real data value to map, x0 the origin of the display
coordinate system, xmax is the maximum values of the data points to
map and xmax' is the display coordinate that should correspond to xmax.

In your case, x0 = 40, xmax = 800 and xmax' = 750.
x1 shall be a real world data point to map to the display system.

You need to perform a linear transformation:

1. Scale x1 to match the proportions of the display system:
x1' = ( x1 * (xmax' - x0) ) / xmax

Note the calculation order, first multiply, then divide. This way
you're off by less than one pixel to the left due to rounding errors.
If the product gets too large for int data type, or you want proper
rounding, you will need intermediate double operations, e.g.

int x1 = (int) ( ( (double) x1 * ( xmax' - x0 ) ) / xmax + 0.5 );

The result is int again, though. The "+ 0.5" is clever rounding.

2. Shift x1' to match the origin of the display coordinate system:
x1" = x1' + x0, which is the result.

Of course, you can do both operations in one calculation, the separation
above is for didactic purposes only.

Filling the values from your problem and some simplifications,
you can use this code:

public static class Display {

private const double scale = ( 750.0 - 40.0 ) / 800.0;

public static int transform( int x ) {
return ((int) ( x * scale + 0.5 )) + 40;
}

}

If you want more flexibility, add properties for x0, xmax and xmax' and a
static constructor to fill in other than your example values.

best,

MiB.
 
T

Tony

many thanks.

//Tony

wrote in message

Am Montag, 25. Februar 2013 13:03:19 UTC+1 schrieb Tony:
Assume I have on the x-axes the display area from x=40 pixels to x=750
pixels

If I now want to set a lot of point in this coordinate system.
For example if I have x-value 0 this would be positioned at x = 40 and
x-value 800 would be positioned at x=750.
So I have to transform my x value into the coordinate system where origo
for
x is 40 and x max is 800.

I can't find a suitable algoritm that can transform my x values into this
coordinate system.

You need to perform a simple linear transformation.
If x is a real data value to map, x0 the origin of the display
coordinate system, xmax is the maximum values of the data points to
map and xmax' is the display coordinate that should correspond to xmax.

In your case, x0 = 40, xmax = 800 and xmax' = 750.
x1 shall be a real world data point to map to the display system.

You need to perform a linear transformation:

1. Scale x1 to match the proportions of the display system:
x1' = ( x1 * (xmax' - x0) ) / xmax

Note the calculation order, first multiply, then divide. This way
you're off by less than one pixel to the left due to rounding errors.
If the product gets too large for int data type, or you want proper
rounding, you will need intermediate double operations, e.g.

int x1 = (int) ( ( (double) x1 * ( xmax' - x0 ) ) / xmax + 0.5 );

The result is int again, though. The "+ 0.5" is clever rounding.

2. Shift x1' to match the origin of the display coordinate system:
x1" = x1' + x0, which is the result.

Of course, you can do both operations in one calculation, the separation
above is for didactic purposes only.

Filling the values from your problem and some simplifications,
you can use this code:

public static class Display {

private const double scale = ( 750.0 - 40.0 ) / 800.0;

public static int transform( int x ) {
return ((int) ( x * scale + 0.5 )) + 40;
}

}

If you want more flexibility, add properties for x0, xmax and xmax' and a
static constructor to fill in other than your example values.

best,

MiB.
 
T

Tony

I found the problem.

//tony

wrote in message

Am Montag, 25. Februar 2013 13:03:19 UTC+1 schrieb Tony:
Assume I have on the x-axes the display area from x=40 pixels to x=750
pixels

If I now want to set a lot of point in this coordinate system.
For example if I have x-value 0 this would be positioned at x = 40 and
x-value 800 would be positioned at x=750.
So I have to transform my x value into the coordinate system where origo
for
x is 40 and x max is 800.

I can't find a suitable algoritm that can transform my x values into this
coordinate system.

You need to perform a simple linear transformation.
If x is a real data value to map, x0 the origin of the display
coordinate system, xmax is the maximum values of the data points to
map and xmax' is the display coordinate that should correspond to xmax.

In your case, x0 = 40, xmax = 800 and xmax' = 750.
x1 shall be a real world data point to map to the display system.

You need to perform a linear transformation:

1. Scale x1 to match the proportions of the display system:
x1' = ( x1 * (xmax' - x0) ) / xmax

Note the calculation order, first multiply, then divide. This way
you're off by less than one pixel to the left due to rounding errors.
If the product gets too large for int data type, or you want proper
rounding, you will need intermediate double operations, e.g.

int x1 = (int) ( ( (double) x1 * ( xmax' - x0 ) ) / xmax + 0.5 );

The result is int again, though. The "+ 0.5" is clever rounding.

2. Shift x1' to match the origin of the display coordinate system:
x1" = x1' + x0, which is the result.

Of course, you can do both operations in one calculation, the separation
above is for didactic purposes only.

Filling the values from your problem and some simplifications,
you can use this code:

public static class Display {

private const double scale = ( 750.0 - 40.0 ) / 800.0;

public static int transform( int x ) {
return ((int) ( x * scale + 0.5 )) + 40;
}

}

If you want more flexibility, add properties for x0, xmax and xmax' and a
static constructor to fill in other than your example values.

best,

MiB.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top