Commit 65879199 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add layout_align helper functions

parent c72be025
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -63,6 +63,51 @@ Rectangle layout_margin_box(
  return box;
}

void layout_align(
    const Rectangle& bbox,
    double tx,
    double ty,
    double* ox,
    double* oy) {
  double mx = bbox.x + bbox.w / 2;
  double my = bbox.y + bbox.h / 2;
  *ox = (tx - mx);
  *oy = (ty - my);
}

void layout_align(
    const Rectangle& bbox,
    double tx,
    double ty,
    HAlign align_x,
    VAlign align_y,
    double* ox,
    double* oy) {
  switch (align_x) {
    case HAlign::LEFT:
      tx += bbox.w / 2;
      break;
    case HAlign::RIGHT:
      tx -= bbox.w / 2;
      break;
    case HAlign::CENTER:
      break;
  }

  switch (align_y) {
    case VAlign::TOP:
      ty += bbox.h / 2;
      break;
    case VAlign::BOTTOM:
      ty -= bbox.h / 2;
      break;
    case VAlign::CENTER:
      break;
  }

  return layout_align(bbox, tx, ty, ox, oy);
}

std::ostream& operator <<(std::ostream& os, const Rectangle& r) {
  os << "Rectangle(";
  os << r.x;
+37 −1
Original line number Diff line number Diff line
@@ -43,6 +43,16 @@ struct Rectangle {
  double h;
};

std::ostream& operator <<(std::ostream& os, const Rectangle& c);

enum class HAlign {
  LEFT, CENTER, RIGHT
};

enum class VAlign {
  TOP, CENTER, BOTTOM
};

Rectangle layout_margin_box(
    const Rectangle& parent,
    double margin_top,
@@ -50,7 +60,33 @@ Rectangle layout_margin_box(
    double margin_bottom,
    double margin_left);

std::ostream& operator <<(std::ostream& os, const Rectangle& c);
/**
 * Given a bounding box and a target point (tx, ty) and (sx, sy) == (0, 0),
 * calculate a translate offset (ox, oy) for the bounding box so that new
 * bounding box midpoint point is equal to the target point.
 */
void layout_align(
    const Rectangle& bbox,
    double tx,
    double ty,
    double* ox,
    double* oy);

/**
 * Given a bounding box and a target point (tx, ty) and (sx, sy) == (0, 0),
 * calculate a translate offset (ox, oy) for the bounding box so that new
 * bounding box alignment point is equal to the target point. You can choose
 * from one of 9 alignment points using the halign and valign parameters.
 */
void layout_align(
    const Rectangle& bbox,
    double tx,
    double ty,
    HAlign halign,
    VAlign valign,
    double* ox,
    double* oy);


} // namespace plotfx