### Author Topic: [XDrX-Function(5)] Points Fitting Elliptic Function (xdrx-points->fitellipse)  (Read 768 times)

0 Members and 1 Guest are viewing this topic.

• Bull Frog
• Posts: 424
##### [XDrX-Function(5)] Points Fitting Elliptic Function (xdrx-points->fitellipse)
« on: December 03, 2023, 10:40:56 AM »
The following code is used to generate a random point cloud around the original ellipse, and then the function xdrx-points->fitellipse is used to fit the ellipse, and then the fitting accuracy is compared.

Looking at the chart data, it should be a perfect fit.

Code - Auto/Visual Lisp: [Select]
1. (defun c:tt ()
2.   (if (setq e (car
3.                 (xdrx-entsel "\nPick Ellipse<Exit>:" '((0 . "ellipse")))
4.               )
5.       )
6.       (xdrx-begin)
7.       (setq info1 (xdrx-getpropertyvalue
8.                     e              "center"       "area"
9.                     "majorAxis"    "minorAxis"    "majorRadius"
11.                    )
12.             info1 (cons "1" (cons "primitive ellipse" (mapcar 'vl-princ-to-string info1)))
13.       )
14.       (setq pts  (xdrx-curve-getsamplepoints e 200)
15.             pts1 nil
16.       )
17.       (mapcar '(lambda (k)
18.                  (repeat 10
19.                    (setq x (xdrx-math-rand -5.0 5.0)
20.                          y (xdrx-math-rand -5.0 5.0)
21.                    )
22.                    (setq pt (mapcar '+ k (list x y 0.0)))
23.                    (xdrx-point-make pt)
24.                    (setq pts1 (cons pt pts1))
25.                  )
26.                )
27.               pts
28.       )
29.       (if (setq elli (xdrx-points->fitellipse (append pts1 pts)))
30.           (xdrx-entity-setcolor elli 1)
31.           (setq info2 (xdrx-getpropertyvalue
32.                         elli            "center"        "area"
33.                         "majorAxis"     "minorAxis"     "majorRadius"
35.                        )
36.                 info2 (cons "2" (cons "Fit Ellipse" (mapcar 'vl-princ-to-string info2)))
37.           )
38.           (setq
39.             lst (list (list "Ellipse Fitting Comparison Table" nil nil nil nil nil nil nil))
40.             lst (append lst
41.                         (list (list "No"           nil
42.                                     "CENTER"       "AREA"
45.                                    )
46.                         )
47.                 )
48.             lst (append lst (list info1))
49.             lst (append lst (list info2))
50.             scl (* (xd::var:getratio) (xd::var:getscaleratio))
51.             h   (* scl 4)
52.           )
53.           (xd::table:makefromlist lst '(0 0 0) h (/ h 2.0))
54.           (xd::drag:simplemove (entlast) "\nTable Insert:" 8 t)
55.           (xd::drag:simplemove elli "\nEllipse Insert Point:" 5 t)
56.         )
57.       )
58.       (xdrx-end)
59.     )
60.   )
61.   (princ)
62. )
63.
« Last Edit: December 04, 2023, 01:29:51 PM by xdcad »
The code I wrote uses XDRX-API，which can be downloaded from github.com and is updated at any time.
===================================
[XDrx-Sub Forum]
https://www.theswamp.org/index.php?board=78.0