What would be the formula to rotate a curve by the x axis in java?
Asked
Active
Viewed 463 times
-4
-
C++ or Java? Rotate around the x- or y-axis? What you show is flipped around the y-axis... – Alexis Pigeon Dec 19 '12 at 13:12
-
And what form is your curve in? A formula? An array of y-values? A table of x-y pairs? – Useless Dec 19 '12 at 13:26
-
I have two arrays: pointsX and pointsY – menemenemu Dec 19 '12 at 13:27
-
2I'm voting to close this question as off-topic because it's asking for a mathematical formula; not strictly a programming question. – Nathaniel Ford Aug 13 '15 at 22:55
2 Answers
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
-
Err, that would rather be `-f(x)` to flip around the x-axis, and `f(-x)` to flip around the y-axis – Alexis Pigeon Dec 19 '12 at 13:10
-