Skip to content

Commit

Permalink
Add point centroid functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ianmackenzie committed Aug 20, 2018
1 parent 3d4391e commit bcdd655
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 2 deletions.
56 changes: 55 additions & 1 deletion src/Point2d.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Point2d
exposing
( Point2d
, along
, centroid
, circumcenter
, coordinates
, distanceFrom
Expand Down Expand Up @@ -66,7 +67,7 @@ like you can add two vectors.
# Constructors
@docs fromCoordinates, fromCoordinatesIn, fromPolarCoordinates, fromPolarCoordinatesIn, midpoint, interpolateFrom, along, circumcenter
@docs fromCoordinates, fromCoordinatesIn, fromPolarCoordinates, fromPolarCoordinatesIn, midpoint, centroid, interpolateFrom, along, circumcenter
# Properties
Expand Down Expand Up @@ -164,6 +165,59 @@ midpoint firstPoint secondPoint =
interpolateFrom firstPoint secondPoint 0.5


{-| Find the centroid of a list of points. Returns `Nothing` if the list is
empty.
p0 =
Point2d.origin
p1 =
Point2d.fromCoordinates ( 1, 0 )
p2 =
Point2d.fromCoordinates ( 1, 1 )
Point2d.centroid [ p0, p1, p2 ]
--> Just (Point2d.fromCoordinates ( 0.6667, 0.3333 ))
-}
centroid : List Point2d -> Maybe Point2d
centroid points =
case points of
[] ->
Nothing

first :: rest ->
let
( x0, y0 ) =
coordinates first
in
Just (centroidHelp x0 y0 1 0 0 rest)


centroidHelp : Float -> Float -> Float -> Float -> Float -> List Point2d -> Point2d
centroidHelp x0 y0 count dx dy points =
case points of
point :: remaining ->
let
( x, y ) =
coordinates point

newDx =
dx + (x - x0)

newDy =
dy + (y - y0)
in
centroidHelp x0 y0 (count + 1) newDx newDy remaining

[] ->
fromCoordinates
( x0 + dx / count
, y0 + dy / count
)


{-| Construct a point by interpolating from the first given point to the second,
based on a parameter that ranges from zero to one.
Expand Down
60 changes: 59 additions & 1 deletion src/Point3d.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Point3d
exposing
( Point3d
, along
, centroid
, circumcenter
, coordinates
, distanceFrom
Expand Down Expand Up @@ -70,7 +71,7 @@ like you can add two vectors.
# Constructors
@docs fromCoordinates, fromCoordinatesIn, midpoint, interpolateFrom, along, on, circumcenter
@docs fromCoordinates, fromCoordinatesIn, midpoint, centroid, interpolateFrom, along, on, circumcenter
# Properties
Expand Down Expand Up @@ -158,6 +159,63 @@ midpoint firstPoint secondPoint =
interpolateFrom firstPoint secondPoint 0.5


{-| Find the centroid of a list of points. Returns `Nothing` if the list is
empty.
p0 =
Point3d.origin
p1 =
Point3d.fromCoordinates ( 1, 0, 1 )
p2 =
Point3d.fromCoordinates ( 0, 1, 1 )
Point3d.centroid [ p0, p1, p2 ]
--> Just (Point3d.fromCoordinates ( 0.3333, 0.3333, 0.6667 ))
-}
centroid : List Point3d -> Maybe Point3d
centroid points =
case points of
[] ->
Nothing

first :: rest ->
let
( x0, y0, z0 ) =
coordinates first
in
Just (centroidHelp x0 y0 z0 1 0 0 0 rest)


centroidHelp : Float -> Float -> Float -> Float -> Float -> Float -> Float -> List Point3d -> Point3d
centroidHelp x0 y0 z0 count dx dy dz points =
case points of
point :: remaining ->
let
( x, y, z ) =
coordinates point

newDx =
dx + (x - x0)

newDy =
dy + (y - y0)

newDz =
dz + (z - z0)
in
centroidHelp x0 y0 z0 (count + 1) newDx newDy newDz remaining

[] ->
fromCoordinates
( x0 + dx / count
, y0 + dy / count
, z0 + dz / count
)


{-| Construct a point by interpolating from the first given point to the second,
based on a parameter that ranges from zero to one.
Expand Down

0 comments on commit bcdd655

Please sign in to comment.