Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: Affine Transformations

What's the best way to implement Affine Transformations in Scala? There don't seem to be any in the standard library or in Spire. The AWT AffineTransformation class is horribly mutable and I definitely don't want to mutate the Graphics2D class. Is it more efficient to write my own or to wrap the Java class in value returning functions, or is there already a suitable Scala library?

Edit: I don't think the basic equations are too challenging to code. The complication seem to be adding special cases for 90/180/270 rotations and dealing with int/ double/ float conversions for a comprehensive solution.

like image 614
Rich Oliver Avatar asked Nov 04 '22 11:11

Rich Oliver


1 Answers

I agree with the comment, don't invest the effort to implement the formulas yourself.

import java.awt.geom.{AffineTransform => JTransform}

object AffineTransform {
   implicit def toJava(at: AffineTransform): JTransform = at.toJava
   implicit def fromJava(j: JTransform): AffineTransform = {
      import j._
      AffineTransform(getScaleX,     getShearY,
                      getShearX,     getScaleY,
                      getTranslateX, getTranslateY)
   }
   val identity: AffineTransform = new JTransform()
}
final case class AffineTransform(scaleX:     Double, shearY:     Double,
                                 shearX:     Double, scaleY:     Double,
                                 translateX: Double, translateY: Double) {

   def toJava: JTransform = new JTransform(scaleX,     shearY,
                                           shearX,     scaleY,
                                           translateX, translateY)

   override def toString = "AffineTransform[[" + 
      scaleX + ", " + shearX + ", " + translateX + "], [" +
      shearY + ", " + scaleY + ", " + translateY + "]]"

   def scale(sx: Double, sy: Double): AffineTransform = {
      val j = toJava; j.scale(sx, sy); j
   }
   // ... etc
}

Test:

AffineTransform.identity.scale(0.5, 1.0)
like image 57
0__ Avatar answered Nov 08 '22 10:11

0__