From 49ac16c2ae6753328e07db1cbe76d085242de127 Mon Sep 17 00:00:00 2001 From: Ruben de Laat Date: Wed, 17 Jul 2019 21:20:34 +0200 Subject: [PATCH] Improved getArea method, now also works on (already) closed paths --- PluginBase/src/org/bimserver/utils/IfcTools2D.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/PluginBase/src/org/bimserver/utils/IfcTools2D.java b/PluginBase/src/org/bimserver/utils/IfcTools2D.java index 48ebd07391..5a65560f65 100644 --- a/PluginBase/src/org/bimserver/utils/IfcTools2D.java +++ b/PluginBase/src/org/bimserver/utils/IfcTools2D.java @@ -464,7 +464,7 @@ public static Area findSmallest(Area area) { return null; } - public static float getArea(Area area) { + public static float getArea(Area area) { float sum = 0; PathIterator pathIterator = area.getPathIterator(null); float[] coords = new float[6]; @@ -473,15 +473,18 @@ public static float getArea(Area area) { while (!pathIterator.isDone()) { int currentSegment = pathIterator.currentSegment(coords); if (currentSegment == PathIterator.SEG_CLOSE) { - - } else if (currentSegment == PathIterator.SEG_MOVETO) { + if (last != null) { + sum = sum + Math.abs(last[0] * coords[1] - last[1] * coords[0]); + } + last = new float[]{coords[0], coords[1]}; + } else if (currentSegment == PathIterator.SEG_MOVETO) { last = new float[]{coords[0], coords[1]}; if (first == null) { first = new float[]{coords[0], coords[1]}; } } else if (currentSegment == PathIterator.SEG_LINETO) { if (last != null) { - sum = sum + last[0] * coords[1] - last[1] * coords[0]; + sum = sum + Math.abs(last[0] * coords[1] - last[1] * coords[0]); } last = new float[]{coords[0], coords[1]}; @@ -489,7 +492,7 @@ public static float getArea(Area area) { pathIterator.next(); } if (last != null && first != null) { - sum = sum + last[0] * first[1] - last[1] * first[0]; + sum += Math.abs(last[0] * first[1] - last[1] * first[0]); } return sum / 2f; }