Author Topic: Average point ...  (Read 4106 times)

0 Members and 1 Guest are viewing this topic.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Average point ...
« on: October 02, 2005, 10:27:15 AM »
Needed this for myself, might be handy for someone else --

Code: [Select]
(defun AveragePoint ( points )
    (   
        (lambda (n)
            (mapcar
               '(lambda (fn) (/ (apply '+ (mapcar fn points)) n))
                (if (vl-every '(lambda (x) (eq 3 (length x))) points)
                   '(car cadr caddr)
                   '(car cadr)
                )   
            )
        )
        (float (length points))
    )   
)

Example --

Code: [Select]
(AveragePoint
   '(   
        (49.8454 9.65345 27.9676)
        (27.9676 11.3813 13.6014)
        (12.7809 10.8554 41.0673)
        (14.7364 26.5733 15.6604)
        (10.3007 23.8688 17.9287)
        (10.6014 10.8554 21.3007)
        (14.6604 18.0673 54.3813)
        (29.9223 18.0673 23.3813)
        (42.9287 10.1042 71.8681)
        (48.8681 22.5166 11.7364)
    )
)

Result --

Code: [Select]
(26.2612 16.1943 29.8893)
There's gotta be a more efficient way but I can't see it this morning. Anyone? Anyone? Bueller?
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Fatty

  • Guest
Re: Average point ...
« Reply #1 on: October 02, 2005, 10:56:23 AM »
Hi MP

My regards...
Maybe better yet to walk on this way:
Code: [Select]
(defun aver (points)
(if (car points)
(cons (/ (apply '+ (mapcar 'car points))(length points))
      (aver (mapcar 'cdr points)))))

Thank you

F.

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Average point ...
« Reply #2 on: October 02, 2005, 11:00:19 AM »
Okay, this brings up a question I had. (yes its a bit off subject but...) I have a question of using lambda off the start.
-i.e. (defun foo (x) ( (lambda (n) ...)))

What is the point/benefit to this method? I know that instead of using a helper function, you can "bind your local variables with lambda instead of using a separate named abstraction(s)" but I guess I don't see the point otherwise. (Am I correct?)

I will have to hold my thoughts on the efficiency till Monday, when I get to an AutoCAD. But I will tell you that my first impressions are that I think you are doing a good job. I mean your iterating thru the points only once (I don't think "length" iterates thru the points to count them) so...seems perty good to me.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Average point ...
« Reply #3 on: October 02, 2005, 11:07:24 AM »
Hello Fatty,

Nice code, but do you think that might lose its efficiency over larger lists? (I mean I'm a big fan of recursion) but the memory on that procedure over a larger list would be huge! (Well this is just my initial thoughts. But like I said I would need to get a copy of AutoCAD to comment further/accurately/etc.)

Oh BTW, Welcome to theSwamp. Hope you enjoy the place. Let me know if you have any questions, i would be more then happy to help in any way i can.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Average point ...
« Reply #4 on: October 02, 2005, 11:08:36 AM »
An alternative --

Code: [Select]
(defun AveragePoint ( points / average result )

    (defun average ( numbers )
        (   
            (lambda (n) (/ (apply '+ numbers) n))   
            (float (length numbers))
        )   
    )
   
    (setq result
        (list
            (average
                (mapcar 'car points)
            )
        )   
    )
   
    (while (setq points (vl-remove-if 'null (mapcar 'cdr points)))
        (setq result
            (cons
                (average (mapcar 'car points))
                result
            )
        )
    )
   
    (reverse result)

)

Not as succinct as the first stab but it let's you do weird sht like this --

Code: [Select]
(AveragePoint
   '(   
        (01 01 01 01 01 01)
        (02 02 02 02 02 02)
        (03 03 03 03 03)
        (04 04 04 04 04)
        (05 05 05 05)
        (06 06 06 06)
        (07 07 07)
        (08 08 08)
        (09 09)
        (10 10)
        (11)
        (12)
    )
)

Result --

Code: [Select]
(6.5 5.5 4.5 3.5 2.5 1.5)
Whether or not there is value in this I dunno, I've no need, just enjoyed writing it.

:lol:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Re: Average point ...
« Reply #5 on: October 02, 2005, 11:11:34 AM »
Needed this for myself, might be handy for someone else --

I can use this!! I Might add the ability to work AECC points.
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10652
Re: Average point ...
« Reply #6 on: October 02, 2005, 11:14:54 AM »
Well succient or not, that looks more like the way i would have taken a stab at the problem. *lol*

...Oh, before i hit the "post button" i saw this:  (mapcar 'car points) ...I would create a var out of that so i didnt have to call it twice. (Just nit-picking)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Average point ...
« Reply #7 on: October 02, 2005, 11:32:02 AM »
Hi MP

My regards...
Maybe better yet to walk on this way:
Code: [Select]
(defun aver (points)
(if (car points)
(cons (/ (apply '+ (mapcar 'car points))(length points))
      (aver (mapcar 'cdr points)))))

Thank you

F.

Very nice job F., very 'D.Broad'ish to me.  Despite the recursion overhead I like it.

If I were to pen a recursive one I'd go this route --

Code: [Select]
(defun AveragePoint ( points / numbers )
    (if (setq numbers (mapcar 'car points))
        (cons
            (/ (apply '+ numbers) (float (length numbers)))
            (AveragePoint (vl-remove-if 'null (mapcar 'cdr points)))
        )
    )
)

Can handle either --

Code: [Select]
(AveragePoint
   '(   
        (49.8454 9.65345 27.9676)
        (27.9676 11.3813 13.6014)
        (12.7809 10.8554 41.0673)
        (14.7364 26.5733 15.6604)
        (10.3007 23.8688 17.9287)
        (10.6014 10.8554 21.3007)
        (14.6604 18.0673 54.3813)
        (29.9223 18.0673 23.3813)
        (42.9287 10.1042 71.8681)
        (48.8681 22.5166 11.7364)
    )
)

Result --

Code: [Select]
(26.2612 16.1943 29.8893)
Or

Code: [Select]
(AveragePoint
   '(   
        (01 01 01 01 01 01)
        (02 02 02 02 02 02)
        (03 03 03 03 03)
        (04 04 04 04 04)
        (05 05 05 05)
        (06 06 06 06)
        (07 07 07)
        (08 08 08)
        (09 09)
        (10 10)
        (11)
        (12)
    )
)

Result --

Code: [Select]
(6.5 5.5 4.5 3.5 2.5 1.5)
Thanks for your input, good stuff.

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Average point ...
« Reply #8 on: October 02, 2005, 12:13:44 PM »
Okay, this brings up a question I had. (yes its a bit off subject but...) I have a question of using lambda off the start.
-i.e. (defun foo (x) ( (lambda (n) ...)))

What is the point/benefit to this method? I know that instead of using a helper function, you can "bind your local variables with lambda instead of using a separate named abstraction(s)" but I guess I don't see the point otherwise. (Am I correct?)

I've developed this odd coding querk -- I hate the "untidiness" of explicit variable declarations / assignment if I can pass values instead -- it looks a lot cleaner and more expressive to me. I had done some tests way back and generally speaking, found there was a nominal performance hit, certainly one I can live with for the ease of code maintenance it affords. Of course, others many not see it as a more maintainable style; I understand that subjectivity, but the way my strange nuggin works it's preferred as noted.

Also, writing lambda based code structures facilitates greater code reuse, as a lambda structure sometimes becomes a formal function down the line: replace lambda with defun and presto, instant function, sometimes without furter mods.

/mmm mileage, got some?

:)

Doh! Gotta get ready for church, catch ya'll later.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Fatty

  • Guest
Re: Average point ...
« Reply #9 on: October 02, 2005, 12:17:54 PM »
It is very pleasant to hear good sounds from the big man...
Thank you

F.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Average point ...
« Reply #10 on: October 02, 2005, 03:37:36 PM »
??  "the big man"  ??

Whatcha talkin' bout? Ain't nobody here but us chickens.

:lol:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst