def rotate(surface, angle): """ Return Surface rotated by the given angle. """ if not angle: return surface.copy() theta = angle * _deg_rad width_i = surface.getWidth() height_i = surface.getHeight() cos_theta = _fabs(_cos(theta)) sin_theta = _fabs(_sin(theta)) width_f = int((width_i * cos_theta) + (height_i * sin_theta)) height_f = int((width_i * sin_theta) + (height_i * cos_theta)) surf = Surface((width_f, height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.translate(width_f / 2.0, height_f / 2.0) at.rotate(-theta) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, -width_i // 2, -height_i // 2, None) g2d.setTransform(ot) g2d.dispose() return surf
def rotozoom(surface, angle, size): """ Return Surface rotated and resized by the given angle and size. """ if not angle: width = int(surface.getWidth() * size) height = int(surface.getHeight() * size) return scale(surface, (width, height)) theta = angle * _deg_rad width_i = int(surface.getWidth() * size) height_i = int(surface.getHeight() * size) cos_theta = _fabs(_cos(theta)) sin_theta = _fabs(_sin(theta)) width_f = int(_ceil((width_i * cos_theta) + (height_i * sin_theta))) if width_f % 2: width_f += 1 height_f = int(_ceil((width_i * sin_theta) + (height_i * cos_theta))) if height_f % 2: height_f += 1 surf = Surface((width_f, height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.translate(width_f / 2.0, height_f / 2.0) at.rotate(-theta) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, -width_i // 2, -height_i // 2, width_i, height_i, None) g2d.setTransform(ot) g2d.dispose() return surf
def rotozoom(self, surface, angle, size): """ Return Surface rotated and resized by the given angle and size. """ if not angle: width = int(surface.getWidth()*size) height = int(surface.getHeight()*size) return self.scale(surface, (width, height)) theta = angle*self.deg_rad width_i = int(surface.getWidth()*size) height_i = int(surface.getHeight()*size) cos_theta = _fabs( _cos(theta) ) sin_theta = _fabs( _sin(theta) ) width_f = int( _ceil((width_i*cos_theta)+(height_i*sin_theta)) ) if width_f % 2: width_f += 1 height_f = int( _ceil((width_i*sin_theta)+(height_i*cos_theta)) ) if height_f % 2: height_f += 1 surf = Surface((width_f,height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.translate(width_f/2, height_f/2) at.rotate(-theta) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, -width_i//2, -height_i//2, width_i, height_i, None) g2d.setTransform(ot) g2d.dispose() return surf
def rotate(self, surface, angle): """ Return Surface rotated by the given angle. """ if not angle: return surface.copy() theta = angle*self.deg_rad width_i = surface.getWidth() height_i = surface.getHeight() cos_theta = _fabs( _cos(theta) ) sin_theta = _fabs( _sin(theta) ) width_f = int( (width_i*cos_theta)+(height_i*sin_theta) ) height_f = int( (width_i*sin_theta)+(height_i*cos_theta) ) surf = Surface((width_f,height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.translate(width_f/2, height_f/2) at.rotate(-theta) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, -width_i//2, -height_i//2, None) g2d.setTransform(ot) g2d.dispose() return surf
def setSize(self, size): # First triangle, size is radius of circumcircle, center at (0,0) self.tri1 = GeneralPath() self.tri1.moveTo(size, 0) self.tri1.lineTo(int(-0.5 * size), int(0.866 * size)) self.tri1.lineTo(int(-0.5 * size), int(-0.866 * size)) self.tri1.closePath() # Second triangle like first, but rotated 180 degrees self.tri2 = self.tri1.clone() t = AffineTransform() t.rotate(math.pi) self.tri2.transform(t)
def _getArea(self): h = int((self._get("scale") * self._get("height"))) w = int((self._get("scale") * self._get("width"))) p = self._get("position") center = self._get("center") theta = self._get("rotation") #ar = Area(Rectangle2D.Double(-center.x, -center.y, 1, 1)) ar = self._area(self, center) g = AffineTransform() g.translate(p.x, p.y) g.rotate(theta) g.scale(w, h) ar.transform(g) return ar
def transform(g, dx=0, dy=0, sx=1, sy=1, shx=0, shy=0, r=0): """ Tranforms a geometry with an affine transformation. *g* is the :class:`Geometry <geoscript.geom.Geometry>` to transform. *dx*, *dy* specify the x,y translation. *sx*, *sy* specify the x,y scale factors. *shx, shy* specify the x,y shear factors. *r* specifies the rotation angle in radians. """ tx = AffineTransform(sx, shy, shx, sy, dx, dy) tx.rotate(r) return JTS.transform(g, AffineTransform2D(tx))
def rotate(self, surface, angle): """ Return Surface rotated by the given angle. """ theta = angle * self.deg_rad width_i = surface.getWidth() height_i = surface.getHeight() cos_theta = math.fabs(math.cos(theta)) sin_theta = math.fabs(math.sin(theta)) width_f = int((width_i * cos_theta) + (height_i * sin_theta)) height_f = int((width_i * sin_theta) + (height_i * cos_theta)) surf = Surface((width_f, height_f), BufferedImage.TYPE_INT_ARGB) at = AffineTransform() at.rotate(-theta, width_f / 2, height_f / 2) g2d = surf.createGraphics() ot = g2d.getTransform() g2d.setTransform(at) g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR) g2d.drawImage(surface, (width_f - width_i) // 2, (height_f - height_i) // 2, None) g2d.setTransform(ot) g2d.dispose() return surf
# for the origin of transformation (e.g. the center) isn't easy, # and the fidgety of it all makes it error prone. # For 2D, java offers an AffineTransform class that addresses this issue # quite trivially, with the method rotate that takes an origin of rotation # as argument. # BEWARE that positive angles rotate to the right (rather than to the left) # in the AffineTransform, so we use radians(45) instead of radians(-45). # And BEWARE that AffineTransform.getMatrix fills a double[] array in # an order that you wouldn't expect (see the javadoc), so instead # we call each value of the matrix, one by one, to fill our matrix. aff = AffineTransform() # initialized as the identity transform cx = imp.getWidth() / 2.0 cy = imp.getHeight() / 2.0 aff.rotate(radians(45), cx, cy) rotate45easy = AffineTransform2D() rotate45easy.set(aff.getScaleX(), aff.getShearX(), aff.getTranslateX(), aff.getShearY(), aff.getScaleY(), aff.getTranslateY()) print rotate45easy viewTransformed(imp, rotate45easy, title=imp.getTitle() + " rotate45easy") # An alternative that works also for 3D transformations exploits # a nice property of transformation matrices: that they can be combined. # That is, instead of applying a transform to an image, an then applying # a second transform to the resulting image, instead the transformation # matrices can be multiplied, and a single transform applied to the image. # This is desirable not only for performance reasons, but primarily # because it avoids the accumulation of interpolation errors.
def turn(self, angle): t = AffineTransform() t.rotate(angle) self.tri1.transform(t) self.tri2.transform(t)