简体   繁体   中英

Java - Rotate Rectangle2D and set Length?

I am attempting to rotate a Rectangle around a Point and set its length dynamically. This is what I have so far:

Rectangle2D myRect = new Rectangle2D.Double(point.x - GameValues.ROPE_WIDTH, point.y, point.x + GameValues.ROPE_WIDTH, point.y + ropeLength);
    AffineTransform at = AffineTransform.getRotateInstance(
            Math.toRadians(rotation - 180), point.x, point.y);
    rope = at.createTransformedShape(myRect);

When I draw the shape, it doesn't behave as expected, the rectangles width changes as i move the Point around. How do I do this properly?

You're misinterpreting how Rectangle2Ds are defined. For a Rectangle2D, I would suggest setting a length that is not related to point (directly), and then using affine transforms to scale it.

The issue is that Rectangle2D isn't defined between (x1, y1) and (x2, y2) like you think it is; it's (x1, y1) and (Δx, Δy), or the offsets.

alter your code to use this:

Rectangle2D myRect = new Rectangle2D.Double(point.x, point.y, GameValues.ROPE_WIDTH, ropeLength);

or some permutation of it, and you should be in the clear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM