在Scala中实现仿射转换的最佳方法是什么?标准库或尖顶中似乎没有任何东西。AWT AffinatrySformation类是可怕的,我绝对不想突变Graphics2D类。写我自己的写作或将java类包装在值返回功能上还是已经有一个合适的scala库?
更有效吗?编辑:我认为基本方程式不太具有挑战性,无法代码。并发症似乎正在增加90/180/270旋转的特殊情况,并处理INT/Double/Float转换以进行全面的解决方案。
我同意评论,不要投资自己实施公式。
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
}
测试:
AffineTransform.identity.scale(0.5, 1.0)