Find center by 4 points

Find center by 4 points

Lukasvop1
Advocate Advocate
2,617 Views
25 Replies
Message 1 of 26

Find center by 4 points

Lukasvop1
Advocate
Advocate

Hi guys, I realize I need tool like this, can you help?

This lisp "FC" should find center between 4 picked points and place block there.


-In lisp should be path to the block.
-Would be greate if there will be option set Scale factor of the block (similar like BREAKLINE (Express Tool)).

 

Find center_4_points_lisp.png


 

@komondormrex

0 Likes
Accepted solutions (4)
2,618 Views
25 Replies
Replies (25)
Message 2 of 26

CodeDing
Advisor
Advisor

Not sure how YOU would define the "center" of 4 points...

 

But here's a program that will determine the intersection of 2 hypothetical lines (created by 4 points):

;; Center of 4 Points
(defun c:C4P ( / Center4Points pts centerPoint)
  ;; Finds point where 2 lines intersect, provided 4 points
  ;; points - list, of 4 points
  ;; returns - point, where 2 lines intersect, else nil
  (defun Center4Points (points / p1 p2 p3 p4)
    (setq p1 (car points) p2 (cadr points) p3 (caddr points) p4 (cadddr points))
    (cond
      ((inters p1 p2 p3 p4 t))
      ((inters p1 p3 p2 p4 t))
      ((inters p1 p4 p2 p3 t))
    );cond
  );defun
  (setq pts
    (mapcar
      '(lambda (x) (initget 1) (getpoint (strcat "\nSelect Point " (itoa x) ": ")))
      '(1 2 3 4)
    );mapcar
  );pts
  (setq centerPoint (Center4Points pts))
  (command "_.POINT" "non" centerPoint)
  (princ)
);defun

 

4-points.gif

 

Best,

0 Likes
Message 3 of 26

Kent1Cooper
Consultant
Consultant

What did you use to make the video?  Does that not do what you want?

Kent Cooper, AIA
0 Likes
Message 4 of 26

hak_vz
Advisor
Advisor

@CodeDing  Add OP to select block and move it to centerpoint. Also, I agree with @Kent1Cooper 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 5 of 26

Kent1Cooper
Consultant
Consultant

If the "center" is the weighted-average center-of-gravity location, then once points p1/p2/p3/p4 have been picked, this will give you that location, even if there is no virtual intersection of virtual lines [for example, if all four points are collinear, or if they are not co-planar]:

(mapcar '/ (mapcar '+ p1 p2 p3 p4) '(4 4 4))

Kent Cooper, AIA
0 Likes
Message 6 of 26

john.uhden
Mentor
Mentor

@Lukasvop1 ,

The problem as I see it is that 3 points determine  circle, but there's no guarantee that a fourth point will comply with any of the other three.

That would be sorta like trying to take the two ends and two sides of an egg and trying to derive a circle out of all four.  I just don't know of any bird that lays circular eggs when viewed from the side.

Perhaps you are looking to derive an elliptical circle?  No, that would be incorrect.  Perhaps an egg shaped circle?

John F. Uhden

0 Likes
Message 7 of 26

Lukasvop1
Advocate
Advocate
I used only ordinary commands, the video is edited to demonstrate as final lisp should look like..
0 Likes
Message 8 of 26

Lukasvop1
Advocate
Advocate

I would focus more on the coordinates of individual points. Create formula to calculate coordinates of center point and place block there. Maybe something like that.

 

Find center_4_points_coordinates.png

 

0 Likes
Message 9 of 26

Lukasvop1
Advocate
Advocate

Sure, "center" in this lisp means the weighted average center of gravity.

0 Likes
Message 10 of 26

Sea-Haven
Mentor
Mentor

Removed thinking about 3 points and centre of circle in a triangle.

0 Likes
Message 11 of 26

john.uhden
Mentor
Mentor

Ya know, if he connected the 4 points with a closed polyline, and then curve fitted the polyline, he would have a quasi-circular shape, which is good if you like amoebas.

John F. Uhden

0 Likes
Message 12 of 26

Sea-Haven
Mentor
Mentor

Look at an answer by Kent about using Gcen onece you make a pline of the points, then just erase the dummy pline.

 

(setq pt (osnap (vlax-curve-getStartPoint (vlax-ename->vla-object (entlast)) "gcen")))

 

 

0 Likes
Message 13 of 26

Kent1Cooper
Consultant
Consultant

@Sea-Haven wrote:

....using Gcen onece you make a pline of the points, then just erase the dummy pline.

(setq pt (osnap (vlax-curve-getStartPoint (vlax-ename->vla-object (entlast)) "gcen")))


A few comments:

The (vlax-curve...) functions do not require conversion of an entity to a VLA object, but can use the entity name directly.  So that [which I believe has one of its right parentheses in the wrong place] can be simply:

(setq pt (osnap (vlax-curve-getStartPoint entlast) "gcen")))

 

I prefer the weighted-average approach [Message 5], because:

A.  It doesn't require drawing anything that later needs to be eliminated, but needs only the points;

B.  It doesn't matter in which order the points are picked.  If you happen to pick them in an order that, when drawn through them in order, would give this kind of Polyline:

Kent1Cooper_0-1710088432776.png

then GCEN Osnap fails [it doesn't like self-intersection].  So for that approach, it would be necessary to prompt the User to pick sequentially around, and do some kind of calculations to check that they did, and if not, adjust the order of the points to get a non-self-intersecting Polyline.

C.  It can work with points with different Z coordinates as well as different XY positions.  And if variations in Z coordinate are involved, it can't be done by drawing a 3D Polyline, because GCEN Osnap doesn't accept them, even if they are planar and non-self-intersecting.

Kent Cooper, AIA
0 Likes
Message 14 of 26

calderg1000
Mentor
Mentor

Regards @Lukasvop1 

Try this code

;;;___
(defun c:cg (/ p1 p2 p3 p4)
  (setvar 'pdmode 3)
  (setvar 'pdsize 0.2)
  (if (and
        (setq p1 (getpoint "\nPick Point 1: ")
              p2 (getpoint p1 "\nPick Point 2: ")
              p3 (getpoint p2 "\nPick Point 3: ")
              p4 (getpoint p3 "\nPick Point 4: ")
        )
      )
    (entmakex
      (list '(0 . "point")
            (cons 10
                  (mapcar
                    '(lambda (i j k w) (* (+ i j k w) 0.25)) p1 p2 p3 p4
                   )
            )
      )
    )
  )
  (princ)
)

Carlos Calderon G
EESignature
>Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

0 Likes
Message 15 of 26

Lukasvop1
Advocate
Advocate

..

0 Likes
Message 16 of 26

Lukasvop1
Advocate
Advocate

Sorry, in more depth test I realize, it's not 100% working I want.
I demonstrate it in video.

 

0 Likes
Message 17 of 26

Kent1Cooper
Consultant
Consultant

@Lukasvop1 wrote:

Sure, "center" in this lisp means the weighted average center of gravity.


(defun C:4PC (/ ptno p1 p2 p3 p4); = 4-Point Center
  (repeat 4
    (set (read (strcat "p" (itoa (setq ptno (1+ (cond (ptno) (0))))))) 
      (getpoint (strcat "\nPick point " (itoa ptno) ": "))
    )
  ); repeat
  (command "_.point" "_non" (mapcar '/ (mapcar '+ p1 p2 p3 p4) '(4 4 4)))
  (prin1)
)
Kent Cooper, AIA
0 Likes
Message 18 of 26

Lukasvop1
Advocate
Advocate
I was wrong. "Center of gravity" and "center of 4 edge points" of drawing is different thing. I need last one.
0 Likes
Message 19 of 26

Kent1Cooper
Consultant
Consultant
Accepted solution

@Lukasvop1 wrote:
... "Center of gravity" and "center of 4 edge points" of drawing is different thing. I need last one.

If that means the middle of the overall extents of the four points, try this:

(defun C:4PC (/ ptno p1 p2 p3 p4 pts); = 4-Point Center
  (repeat 4
    (set (read (strcat "p" (itoa (setq ptno (1+ (cond (ptno) (0))))))) 
      (getpoint (strcat "\nPick point " (itoa ptno) ": "))
    )
  ); repeat
  (setq
    pts (list p1 p2 p3 p4)
    minx (apply 'min (mapcar 'car pts))
    maxx (apply 'max (mapcar 'car pts))
    miny (apply 'min (mapcar 'cadr pts))
    maxy (apply 'max (mapcar 'cadr pts))
  ); setq
  (command "_.point" "_non"
    (mapcar '/ (mapcar '+ (list minx miny) (list maxx maxy)) '(2 2 2)))
  (prin1)
)

 

Kent Cooper, AIA
0 Likes
Message 20 of 26

Lukasvop1
Advocate
Advocate
Perfect, works exactly I wanted, thank you.
0 Likes