]> granicus.if.org Git - graphviz/commitdiff
tests: SVGAnalyzer: add SVGRect::intersection()
authorMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Wed, 24 Aug 2022 07:48:19 +0000 (09:48 +0200)
committerMagnus Jacobsson <Magnus.Jacobsson@berotec.se>
Tue, 6 Sep 2022 19:30:03 +0000 (21:30 +0200)
This will be used in an upcoming commit the determine overlap between
edges and nodes.

tests/svg_element.cpp
tests/svg_element.h

index 474e01aa54c50af0b7e534461dd8c0bfc8448e36..c76428029d3f7ee5c592105dbcfcaa5c7611adea 100644 (file)
@@ -801,6 +801,19 @@ SVG::SVGPoint SVG::SVGRect::center() const {
   return {x + width / 2, y + height / 2};
 }
 
+SVG::SVGRect SVG::SVGRect::intersection(SVG::SVGRect other) const {
+  const SVG::SVGLine intersection_diagonal = {
+      std::max(x, other.x), std::max(y, other.y),
+      std::min(x + width, other.x + other.width),
+      std::min(y + height, other.y + other.height)};
+
+  return SVG::SVGRect{
+      .x = intersection_diagonal.x1,
+      .y = intersection_diagonal.y1,
+      .width = intersection_diagonal.x2 - intersection_diagonal.x1,
+      .height = intersection_diagonal.y2 - intersection_diagonal.y1};
+}
+
 void SVG::SVGRect::extend(const SVG::SVGRect &other) {
   const auto xmin = std::min(x, other.x);
   const auto ymin = std::min(y, other.y);
index 76167cef19470d7d64cc95da15bd5e7f9bb87691..9113d2798e814fef4106660a29dcbd905705894a 100644 (file)
@@ -33,6 +33,7 @@ struct SVGRect {
   double width;
   double height;
   SVGPoint center() const;
+  SVGRect intersection(SVGRect other) const;
   void extend(const SVGPoint &point);
   void extend(const SVGRect &other);
 };