views:

180

answers:

1

Hi everyone,

I'm working on a project that requires me to do simple geometrical transformations (translation, reflection over x and y axis) on some figures drawn on a Java applet. The previous guy working on the applet was drawing the figures from arrays representing the caretesian points for the vertices of each figure. I decided to represent the figures as Polygons because it added some nicer organization to the code, I could use the arrays he was using to construct one, and also because I figured transformations would become easier. After finding Polygon didn't have any methods for reflection, I tried another route: I cast the Polygon as a Shape, then an Area, and then applied a AffineTransform that should have done what I wanted; unfortunately, Graphics doesn't have a method to draw Area objects, and I couldn't cast back into a shape.

So, does anyone know of a way to do geometric reflection using Polygons, or is there some other means through which I could perform this?

+2  A: 

Is there any reason why you can't just write your own functions for this? Like:

Polygon reflectX(Polygon p) {
    Polygon np = new Polygon();
    for (int i = 0; i < p.npoints; i++) {
        np.addPoint(p.xpoints[i], -p.ypoints[i]);
    }
    return np;
}
Jonathan
Just implements methods that moves each point to right position as Jonathan just did.
Silence
My only problem is that I can't move the origin without breaking other code in the applet, so having those negative points draws the figures off of the viewable area.
But if negative points are off of the viewable area, how were you doing reflections? Perhaps what you want is actually two layers -- a back-end which does all of the calculations properly using Euclidian geometry, and a front-end that renders these to the appropriate places on the screen.
Jonathan
I wasn't doing reflections before, sorry for not clarifying that in the original post. That's a good idea though; I can just apply offsets to these values based on where the origin should be to ensure the figure is on the drawable area. Also, I probably should have thought of that ^_^; I'll give it a shot. Thanks everyone.
Does your viewable area shows the negative quadrants as well ? If it only show the positive x and y quadrant, every reflexion upon an axis will put your objects off grid. Unless you want to reflect uopn an axis that is not placed on the origin. Am I clear enough ?
Silence