views:

215

answers:

6

I have 2 arrays of ints I am using to create a polygon (that looks like a fish). What do I need to do to the arrays to flip the polygon horizontally?

x = new int[]
   { 0, 18, 24, 30, 48, 60, 60, 54, 60, 48, 30, 24, 0 };
y = new int[]
   { 0, 18, 6, 0, 0, 12, 18, 24, 24, 36, 36, 30, 36 };
+4  A: 

Modify x coordinate using this formulae x = 60 - x

alex
This is correct but it would probably be nice to explain to the OP how he could arrive at this answer
Falaina
If the OP lacks the basic understanding of coordinate systems he should not be doing any gfx coding at all.
Bombe
+1  A: 

http://en.wikipedia.org/wiki/Rotation%5Fmatrix

tuinstoel
A reflection and a rotation are fundamentally different operations. But looking up linear algebra isn't a bad idea for the OP.
Falaina
It shouldn't be rotation, it should be scaling of X coordinates by factor of -1.
Dmitriy Matveev
I confused reflaction in a point with reflaction in a line.
tuinstoel
+10  A: 

You need to find the maximum value of the x array. In this case it is 60. Then set each x coordinate to 60 - x using a loop, like this:

for (i = 0; i < NUMBER_OF_POINTS; i++) {
    x[i] = MAX_X - x[i];
}
yjerem
it is sending the fish back to the otherside of the sceen
Josh Curren
maybe post your code.
yjerem
after playing around with a few other things this appears to be working, there was another bug in the code that was making it jump to the other side of the screen.
Josh Curren
Ah, great. I love it when bugs make fish jump.
yjerem
A: 

I think what you mean by 'flip' is to make a fish that is headed left to right change into one that is right to left. This means that you are effectively reflecting the fish around the line x=a, where a is the horizontal coordinate of the mid point of the fish. In this case a=(max(x[])-min(x[]))/2.

For each point, we check if it is to the left or right of x=a. If it is to the left, we simple change it so that it is now the same distance to the right, otherwise we change it so that it is to the same distance to the left.

I think the following (untested) code will work. I'm keeping all values as ints, so a slight distortion might occur. But it should be easy to tweak the code until the distortion disappears.

int max_x=-1;
int min_x=Integer.MAX_VALUE;
for (int v:x){
   max_x=Math.max(max_x,v);
   min_x=Math.miN(min_x,v);
}

int mid=(max_x-min_x)/2;
int[] reflected_x=new int[x.length];

for(int i=0;i<x.length;i++){
   int diff=Math.abs(x[i]-mid);
   if (x[i]<mid) reflected_x[i]=mid+diff;
   else reflected_x[i]=mid-diff;
}
MAK
Why the down vote?
MAK
A: 

You might find it easier to use the graphics class' translate and scale methods rather than manipulating the array contents.

Pete Kirkham
A: 

You might find this a lot easier if you were using a Point2D abstraction instead of two arrays of ints. These aren't separate entities, they're related. So why are you writing code as if they had no relationship at all? Where are your Point and Polygon classes? Where's the abstraction?

If I understand what you mean by "flipping" horizontally, I think you want reflection about the y-axis. If that's true, all you have to do is change the signs of all the x-coordinates and you're done.

So a vector with endpoints A (xa, ya) and B (xb, yb) becomes (-xa, ya) and (-xb, yb).

duffymo