Polygon Centroid
See also Geometric Functions
Function Syntax | (LM:PolyCentroid <e>) |
Current Version | 1.1 |
Arguments | ||
---|---|---|
Symbol | Type | Description |
e | EName | LWPolyline Entity |
Returns | ||
Type | Description | |
List | Centroid of LWPolyline Polygon expressed in WCS |
Function Description
This function will return the centroid (expressed relative to WCS) of an LWPolyline Polygon, that is, a non-self-intersecting closed LWPolyline with straight segments.
Since this function uses only coordinate manipulation to calculate the position of the centroid, it will consequently evaluate much faster than the common equivalent method of creating a temporary Region Object and extracting the Centroid property (since the creation of a Region requires the loading of various Modelling DLLs).
;; Polygon Centroid - Lee Mac ;; Returns the WCS Centroid of an LWPolyline Polygon Entity (defun LM:PolyCentroid ( e / l ) (foreach x (setq e (entget e)) (if (= 10 (car x)) (setq l (cons (cdr x) l))) ) ( (lambda ( a ) (if (not (equal 0.0 a 1e-8)) (trans (mapcar '/ (apply 'mapcar (cons '+ (mapcar (function (lambda ( a b ) ( (lambda ( m ) (mapcar (function (lambda ( c d ) (* (+ c d) m)) ) a b ) ) (- (* (car a) (cadr b)) (* (car b) (cadr a))) ) ) ) l (cons (last l) l) ) ) ) (list a a) ) (cdr (assoc 210 e)) 0 ) ) ) (* 3.0 (apply '+ (mapcar (function (lambda ( a b ) (- (* (car a) (cadr b)) (* (car b) (cadr a))) ) ) l (cons (last l) l) ) ) ) ) )
Test Program
This short and simple program will prompt the user to select a closed LWPolyline composed entirely of straight segments, and will proceed to create a Point Entity at the centroid of the selected LWPolyline.

(defun c:test ( / ss ) (if (setq ss (ssget "_+.:E:S:L" '( (0 . "LWPOLYLINE") (-4 . "&=") (70 . 1) (-4 . "<NOT") (-4 . "<>") (42 . 0.0) (-4 . "NOT>") ) ) ) (entmake (list '(0 . "POINT") (cons 10 (LM:PolyCentroid (ssname ss 0))))) ) (princ) )
See also Geometric Functions