سؤال

Im trying to do a graph from evalued math function and this is last think I need to do. I have graph with limit coordinates -250:-250 left down and 250:250 right up. I have Y-limit function, which is defined as -10:10, but it could be user redefined and if it is redefined, I need to calculate new coordinates.

I have now field of y-coordinates with 20000 values and each of is multiply by: ratioY = 25 / (fabs( up-limit - down-limit ) / 20) which will make coordinates adapt for new Y-limit (if limit is -5:5, graph looks 2x bigger), this works good, but now isnt graph exactly where it should be (see pictures). Simply 25 is multiplied for postscript coordinates and (up-limit - down-limit) / 20 is ratio for "zooming" Y coordinates. This works fine.

Now Im trying to "move coordinates" which will subtracted from revaluated value: ycoor = (ycoor * ratioY) - move-coorY ;.

Now I have something like this: move-coorY = 25* ( ( up-limit - down-limit) /2 ); and it doesnt work correctly. I need to do sin(0) start from 0.

This is a correct graph which is -10:10

correct one -10:10
(source: matematika.cz)

This is a bad graph which is -5:10

moved one -5:10
(source: matematika.cz)

هل كانت مفيدة؟

المحلول

Maybe its easier not to do this with fixed numbers (like your ratioY) but with two different coordinate systems. Physical coordinates are in your problem domain, i.e. they are the real values of your sine curves. Logical coordinates refer to the device, in your case they are the point values in Postscript, but they might be pixels on a HTML canvas or whatever.

I'll denote the physical coordinates of the first axis with a small x and the corresponding logical coordinate with a capital X. In each coordinate system we have:

Lower bound:    x_min, X_max
Upper bound:    x_max, X_max
Range:          dx = x_max - x_min
                dX = X_max - X_min

Then you can calculate your logical coordinates from the physical ones:

X(x) = X_min + (x - x_min) * dX / dx

This also works vice versa, which is not an issue for Postscript files, but max be useful for an intractive canvas where a mouse click should yield the physical coordinates.

x(X) = x_min + (X - X_min) * dx / dX

In your case, the ratio or scale factor is dX / dx, which you can calculate once for each axis. Let's plot the first point with y == 0 in your first graph:

y_min = -10
y_max = 10
   dy = 20

Y_min = -250
Y_max = 250
   dX = 500

 Y(0) = -250 + (0 - (-10)) * 500 / 20
      = -250 + 10 * 500 / 20
      = 0

In the second graph, the logical coordinates are the same, but:

y_min = -5
y_max = 10
   dy = 15

 Y(0) = -250 + (0 - (-5)) * 500 / 15
      = -250 + 5 * 500 / 15
      = -83.3333

If you change the range of your graph, e.g. from (-10, 10) to (-5, 10), just adjust the physical coordinates. If you resize your graph, change the logical coordinates. (Also, calculating the point in the graph is the same as calculating the position of the tick mark for the axis. Strangely, you got the tick marks right, but not your graph. I think your problem was to account for the non-zero lower bound in both graph and curve data.)

Also, it's probably better to re-evaluate the logical coordinates when printing instead of transfroming them from a previous plot. You can do that on the fly, so that you only need to keep the physical data in an array.

(And lastly, I'll admit that I'm not entirely sure these two kinds of cooirdinates are called physical and logical. I know these terms are used, but it may be the other way round or they might even mean sonething different altogether.)

نصائح أخرى

My friend did a well yob for me and programmed this...

    double zeroPosition(double startY, double endY){ 
        double range = endY - startY; 
        double topSize = endY / range; 
        return 250.0 - 500 * topSize; 
    }

This will calculate position of zero, which I just add to my Y position with ratio and It works exactly how I need!

But thanks M Oehm ;)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top