# resort polygon coordinates to NonIntersecting coordinates

## Recommended Posts

Hi all, I have four coordinates for each polygon in excel, number of some polygons isn't sort and when I draw them in google earth or autocad by vba programming, interference occurs, can we resort them to NonIntersecting  coordinates?.: (number of points is not important)

Edited by amir0914
##### Share on other sites

I know its to do with angles, but no idea something like the ccw of plines that's ok for 4 but what about a "H".

1-2 cw

2-3 cw

3-4 ccw stop

##### Share on other sites

13 hours ago, amir0914 said:

Hi all, I have four coordinates for each polygon in excel, number of some polygons isn't sort and when I draw them in google earth or autocad by vba programming, interference occurs, can we resort them to NonIntersecting  coordinates?.: (number of points is not important)

Hi Amir0914, just a question: how you will determinate if coords need to be sorted or not, and amount of points could be could be useful in order to sort in the correct way, and again how determinate the first point ? it will be always the first ? thank you.

##### Share on other sites

6 hours ago, PeterPan9720 said:

Hi Amir0914, just a question: how you will determinate if coords need to be sorted or not, and amount of points could be could be useful in order to sort in the correct way, and again how determinate the first point ? it will be always the first ? thank you.

I don't exactly how to know it's sorted or not, but I transfer all the polygons to google earth or autocad by vba and check them manually, so that if every polygon will have intersected line, I will correct them on excel file. for the second question, it's not important to determine first or second point, because I don't need to number of point, I only need to draw a nonIntersecting  polygon without number.

Edited by amir0914
##### Share on other sites

Maybe centre point then angle to each point sort points based on angle, ok for a polygon with no reverse points like a L.

amir0914 post some sample xls points. Really need to see the shapes your interested in.

##### Share on other sites

my \$0.02

1. EXCEL select xy columns, insert graph (scatter) as preview

2. If the polygon is rectang or square? calculate area if crossing you get zero

##### Share on other sites

5 hours ago, BIGAL said:

Maybe centre point then angle to each point sort points based on angle, ok for a polygon with no reverse points like a L.

amir0914 post some sample xls points. Really need to see the shapes your interested in.

Hi bigal, I don't know how I can find angle in excel, but I attached sample excel file

##### Share on other sites

4 hours ago, hanhphuc said:

my \$0.02

1. EXCEL select xy columns, insert graph (scatter) as preview

2. If the polygon is rectang or square? calculate area if crossing you get zero

If I found that the polygon need to sort by scatter graph, but what's next? (for resort or correct of points)

##### Share on other sites

8 hours ago, amir0914 said:

If I found that the polygon need to sort by scatter graph, but what's next? (for resort or correct of points)

Macro added. Click  B<>C or C<>D

##### Share on other sites

The attached macro-enable Excel file will sort 4 points such that the results will not have intersecting edges.

It computes the area of three different configurations and uses the configuration with the greatest area as the sorted solution.

BEFORE Execution

AFTER Execution

##### Share on other sites

LRM that's pretty cool using the graph to show result added the make pline bit just copy and paste to Autocad J1-J6

##### Share on other sites

That was great lrm, thank you so much.

The macro resolved my problem, but will the macro work if the points will be more than 4 points in the future? (for example 10 points). like this

Edited by amir0914
##### Share on other sites

You should post the xls for testing in future.

Any way so long as you always have a convex shape this should work. It looks at an internal angle of the points so no graph's etc required, H pattern will not work.

Use in excel cell F2 =(concatenate("point ",B2,",",C2) then copy down and copy and paste like sample F2-F11, As "close" is part of code so no need for last point.

```; pline around points
; By AlanH info@alanh.com.au
: June 2020

(defun c:plpoints ( / ss pt pts x ent lst)
(setq ss (ssget (list (cons 0 "POINT"))))
(setq pt (getpoint "pick center point"))
(setq lst '())
(repeat (setq x (sslength ss))
(setq ent (ssname ss (setq x (- x 1))))
(setq pts (cdr (assoc 10 (entget ent))))
(setq lst (cons (list (angle pts pt) (list (car pts)(cadr pts))) lst))
)
(setq lst (vl-sort lst '(lambda (x y) (< (car x)(car y)))))
(entmakex (append (list  (cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 (length lst))
(cons 70 1))
(mapcar (function (lambda (p) (cons 10 (nth 1 p)))) lst))
)
(princ)
)

(c:clpoints)
```

##### Share on other sites

@amir0914 No, my program is only intended to work for 4 points.

@BIGAL Pretty slick!!!

If the points that the OP is dealing with are all part of a convex hull then you could modify your code to define the center point as the centroid of the set of points eliminating the need for the user to specify a center point.

##### Share on other sites

Like to leave something for the end user to add I did think about it but more code. Watch this space.

Yeah code updated.

Edited by BIGAL
##### Share on other sites

15 hours ago, amir0914 said:

That was great lrm, thank you so much.

The macro resolved my problem, but will the macro work if the points will be more than 4 points in the future? (for example 10 points). like this

LISP try LM:ConvexHull

or

ExcelForum -  VB site hosted by www.andypope.info has been reported as unsafe!

Edited by hanhphuc
##### Share on other sites

6 hours ago, BIGAL said:

You should post the xls for testing in future.

Any way so long as you always have a convex shape this should work. It looks at an internal angle of the points so no graph's etc required, H pattern will not work.

Use in excel cell F2 =(concatenate("point ",B2,",",C2) then copy down and copy and paste like sample F2-F11, As "close" is part of code so no need for last point.

```
; pline around points
; By AlanH info@alanh.com.au
: June 2020

(defun c:plpoints ( / ss pt pts x ent lst)
(setq ss (ssget (list (cons 0 "POINT"))))
(setq pt (getpoint "pick center point"))
(setq lst '())
(repeat (setq x (sslength ss))
(setq ent (ssname ss (setq x (- x 1))))
(setq pts (cdr (assoc 10 (entget ent))))
(setq lst (cons (list (angle pts pt) (list (car pts)(cadr pts))) lst))
)
(setq lst (vl-sort lst '(lambda (x y) (< (car x)(car y)))))
(entmakex (append (list  (cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 (length lst))
(cons 70 1))
(mapcar (function (lambda (p) (cons 10 (nth 1 p)))) lst))
)
(princ)
)

(c:clpoints)
```

That was really good idea,  I change it a bit to find center point by average points : (only on Regular shapes)

```					; pline around points
; By AlanH info@alanh.com.au
:
June
2020

(defun c:plpoints (/ ss pt pts x ent lst)
(setq ss (ssget (list (cons 0 "POINT"))))

(setq ptlist nil)
(repeat (setq x (sslength ss))
(setq ent (ssname ss (setq x (- x 1))))
(setq pts (cdr (assoc 10 (entget ent))))
(setq ptlist (cons pts ptlist))
(setq pt (average ptlist))
)

;;;  (setq pt (getpoint "pick center point"))

(setq lst '())

(foreach a ptlist
(setq
lst (cons (list (angle a pt) (list (car a) (cadr a))) lst)
)
)

(setq lst (vl-sort lst '(lambda (x y) (< (car x) (car y)))))
(entmakex
(append (list (cons 0 "LWPOLYLINE")
(cons 100 "AcDbEntity")
(cons 100 "AcDbPolyline")
(cons 90 (length lst))
(cons 70 1)
)
(mapcar (function (lambda (p) (cons 10 (nth 1 p)))) lst)
)
)
(princ)
)

(c:clpoints)

(defun average (lst)
(mapcar '(lambda (x) (/ x (length lst)))
(apply 'mapcar (cons '+ lst))
)
)```

Edited by amir0914
##### Share on other sites

1 hour ago, hanhphuc said:

LISP try LM:ConvexHull

or

ExcelForum -  VB site hosted by www.andypope.info has been reported as unsafe!

Hi hanhphuc, it is awesome, can you attach the exact file that your testing on the attached image? I downloaded from  "www.andypope.info", but it's not  the same file and also changes the original coordinates.

Edited by amir0914
##### Share on other sites

36 minutes ago, amir0914 said:

Hi hanhphuc, it is awesome, can you attach the exact file that your testing on the attached image? I downloaded from  "www.andypope.info", but it's not  the same file and also changes the original coordinates.

```

(Public Function CalculateHull () As Boolean

;<snippet>

'
' Use the hull points as the set of value to check
' adding an extra set of points
'
blnAnimate = m_blnAnimate

;<snippet>

```

```
```
##### Share on other sites

I updated code in my post,  but the average defun is way better for points was trying to do the same using mapcar etc. Will save it for future reference.

There is a typo (c:clpoints) should be (c:plpoints) runs straighaway if you use appload, drag drop or a menu load.

Edited by BIGAL

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.