From e30affdfc2aca06b945bc7ba1981cfd0fde97ef5 Mon Sep 17 00:00:00 2001 From: Thomas Breloff Date: Wed, 4 May 2016 21:36:15 -0400 Subject: [PATCH] made center calc centroid --- src/components.jl | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/components.jl b/src/components.jl index 838c475a..f406607f 100644 --- a/src/components.jl +++ b/src/components.jl @@ -128,7 +128,26 @@ end # ----------------------------------------------------------------------- -center(shape::Shape) = (mean(shape.x), mean(shape.y)) +# center(shape::Shape) = (mean(shape.x), mean(shape.y)) + +# uses the centroid calculation from https://en.wikipedia.org/wiki/Centroid#Centroid_of_polygon +function center(shape::Shape) + x, y = shape_coords(shape) + n = length(x) + A, Cx, Cy = 0.0, 0.0, 0.0 + for i=1:n + ip1 = i==n ? 1 : i+1 + A += x[i] * y[ip1] - x[ip1] * y[i] + end + A *= 0.5 + for i=1:n + ip1 = i==n ? 1 : i+1 + m = (x[i] * y[ip1] - x[ip1] * y[i]) + Cx += (x[i] + x[ip1]) * m + Cy += (y[i] + y[ip1]) * m + end + Cx / 6A, Cy / 6A +end function Base.scale!(shape::Shape, x::Real, y::Real = x, c = center(shape)) sx, sy = shape_coords(shape)