createFromBaseLines static method

AffineTransformation createFromBaseLines(
  1. Coordinate src0,
  2. Coordinate src1,
  3. Coordinate dest0,
  4. Coordinate dest1,
)
  • Creates an AffineTransformation defined by a mapping between two baselines.
    • The computed transformation consists of:
        • a translation
        • from the start point of the source baseline to the start point of the destination baseline,
        • a rotation through the angle between the baselines about the destination start point,
        • and a scaling equal to the ratio of the baseline lengths.
      • If the source baseline has zero length, an identity transformation is returned.
      • @param src0 the start point of the source baseline
      • @param src1 the end point of the source baseline
      • @param dest0 the start point of the destination baseline
      • @param dest1 the end point of the destination baseline
      • @return the computed transformation

Implementation

static AffineTransformation createFromBaseLines(
    Coordinate src0, Coordinate src1, Coordinate dest0, Coordinate dest1) {
  Coordinate rotPt =
      new Coordinate(src0.x + dest1.x - dest0.x, src0.y + dest1.y - dest0.y);

  double ang = Angle.angleBetweenOriented(src1, src0, rotPt);

  double srcDist = src1.distance(src0);
  double destDist = dest1.distance(dest0);

  // return identity if transformation would be degenerate
  if (srcDist == 0.0) return new AffineTransformation();

  double scale = destDist / srcDist;

  AffineTransformation trans =
      AffineTransformation.translationInstance(-src0.x, -src0.y);
  trans.rotate(ang);
  trans.scale(scale, scale);
  trans.translate(dest0.x, dest0.y);
  return trans;
}