-4

What would be the formula to rotate a curve by the x axis in java?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
menemenemu
  • 209
  • 1
  • 2
  • 8

2 Answers2

4

Assuming you have the curve as an array of Points here is something like a pseudocode:

Point[] curve;

double x_max = curve[0].x, x_min = curve[0].x;
for( point : curve) {
  x_max = max(x_max, point.x);
  x_min = max(x_min, point.x);
}

for (point : curve) {
  point.x = x_max - point.x + x_min;
}

How does it work? In fact I try to mirror the curve's normalized coordinates - that is the coordinates that the points would have if they started from x = 0 (the formula for that is point.x - x_min) and then you subtract the result from x_max so that the curve now is defined right to left instead of left to right.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • Thanks. I'm not using vectors, just two arrays pointsX & pointsY. "max" is just the maximum of two values? – menemenemu Dec 19 '12 at 13:30
  • No max is the maximum value in PointX array. x_min is the minimum in the PointY array. Y coordinates of the points should stay intact. – Ivaylo Strandjev Dec 19 '12 at 13:33
  • I have adapted your code but it gives me strange result when I have more then two breakpoints in the curve.. Please see my original post. Thanks! – menemenemu Dec 19 '12 at 14:03
  • @menemenemu x_min depends again on the x coordinate not the y value! As I mentioned in my comment above y should stay intact so you shouldn't use it in the calculations. Try to understand the logic and it should become clear what I mean. – Ivaylo Strandjev Dec 19 '12 at 14:06
3

It's hard to know what this has to do with C++ or Java but mathematically, if you have a function f(x) that you want to flip along the x axis, you just do f(-x).

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324