Hi,

I would do it as follows.
1. define Affine transform to the plane where your target rectangle
has axes parallel to the coordinate axes (actually in the example I also
made the transformed rectangle to be of width=1 and height=1, and the lower
left corner to coinside with the origin of the coordinate system).
2. then using the inverse get the rectangle on original plane

see attached file.
--
Oleksandr


2011/7/22 Errico Demly Chirulli <[email protected]>

> Hi,
> I need to build a geometry object, it requires some complex operation
> and I'm a bit lost in the API so I'm asking here to find if someone
> more experienced with GT can help.
> I start with two points.
> I need to build a Rectangle with these two points being at the center
> of two opposite sides.
> So the two points define one of the two dimensions(let's call it
> "height"), the other one (the "width") is from a known parameter.
> Here is a bad drawing of what I need to do :
> http://imageshack.us/photo/my-images/204/unledgxq.jpg/
> Thank you
> Errico
>
>
> ------------------------------------------------------------------------------
> 10 Tips for Better Web Security
> Learn 10 ways to better secure your business today. Topics covered include:
> Web security, SSL, hacker attacks & Denial of Service (DoS), private keys,
> security Microsoft Exchange, secure Instant Messaging, and much more.
> http://www.accelacomm.com/jaw/sfnl/114/51426210/
> _______________________________________________
> Geotools-gt2-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
>
package demo;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LinearRing;
import com.vividsolutions.jts.geom.util.AffineTransformationBuilder;
import com.vividsolutions.jts.geom.util.NoninvertibleTransformationException;
import org.geotools.factory.FactoryFinder;
import org.geotools.geometry.jts.JTSFactoryFinder;

/**
 *
 * @author huziy
 */
public class TestCreateGeometry {


    public static void main(String[] args) throws NoninvertibleTransformationException {
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null);

        double width = 10.0;
        Coordinate c1 = new Coordinate(6,7);
        Coordinate c2 = new Coordinate(10,10);
        
        double height = c1.distance(c2);

        //vector perpendicular to c1c2
        Coordinate c = new Coordinate((c1.y - c2.y) / height * width / 2.0 , (c2.x - c1.x) / height * width / 2.0);
        Coordinate middle = new Coordinate(0.5 * (c1.x + c2.x), 0.5 * (c1.y + c2.y));
        Coordinate perpendicular = new Coordinate(middle.x + c.x, middle.y + c.y);
        Coordinate perpendDest = new Coordinate(0.5, 1.0); //impose it to be like that in the transformed plane

        //transform the points to the coordinates with the lower left at the corner of the crs
        //and edges parellel to the axes
        Coordinate c1Dest = new Coordinate(0, 0.5);
        Coordinate c2Dest = new Coordinate(1, 0.5);
        AffineTransformationBuilder builder = new AffineTransformationBuilder(c1, c2, perpendicular, c1Dest,
                                        c2Dest, perpendDest);



        //work from point1 and create linear ring of points
        Coordinate[] coords = new Coordinate[]{
            new Coordinate(0,0), new Coordinate(0,1), new Coordinate(1,1), new Coordinate(1,0), null
        };
        coords[4] = coords[0];


        LinearRing ring = geometryFactory.createLinearRing(coords);
        Geometry rect = geometryFactory.createPolygon(ring, null);

        rect = builder.getTransformation().getInverse().transform(rect);

        System.out.println("Perimeters");
        System.out.println(rect.getLength());
        System.out.println(2 * (height + width));
        System.out.println("Areas");
        System.out.println(rect.getArea());
        System.out.println(width * height);
        //System.out.println(rect.toText());
    }
}
------------------------------------------------------------------------------
10 Tips for Better Web Security
Learn 10 ways to better secure your business today. Topics covered include:
Web security, SSL, hacker attacks & Denial of Service (DoS), private keys,
security Microsoft Exchange, secure Instant Messaging, and much more.
http://www.accelacomm.com/jaw/sfnl/114/51426210/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to