(defun getPolarCoors(stp int / lst )
(setq x 0)
(repeat 4
(setq lst (cons (polar stp 0.0 (setq x (+ x int)) )lst))
)
)
Is this the function signature ??Code - Auto/Visual Lisp: [Select]
;| IncrementXvalue <StartPoint> <Interval> StartPoint -> [list of 3 numbers ] Point coordinate list. Interval -> [number] Step distance. Return -> [list of [point list][point list][point list]] The x component of each point shall be sequentially increased by the Interval value eg: (IncrementXvalue (list 1 2 42) 5) ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0)) |;
Good.
I tried about 4 variations and surprisingly polar seemed the fastest.Code - Auto/Visual Lisp: [Select]
Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] ................. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
I thought this would cream it, but still takes 1.4 times as long as the polar version and ended up being the slowest :)Code - Auto/Visual Lisp: [Select]
) ) ) Code - Auto/Visual Lisp: [Select]
;| IncrementXvalue <StartPoint> <Interval> StartPoint -> [list of 3 numbers ] Point coordinate list. Interval -> [number] Step distance. Return -> [list of [point list][point list][point list]] The x component of each point shall be sequentially increased by the Interval value eg: (IncrementXvalue (list 1 2 42) 5) ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0)) |; (setq lst lst ) ) ) )
recursive:Code - Auto/Visual Lisp: [Select]
< .. >
wow....its too advanced for me. can you please give a simple code which explain better how to design recursive functions?
Hi Evgeniy,
Nice bit of code.
I think (f (list 1 2 42) '(5 2 0) 4)
should return ( (21 10 42) (16 8 42) (11 6 42) (6 4 42) )
Regards,
Hi Lee. THANX. how do you get the time report?
Hi Lee. THANX. how do you get the time report?
I use MP's excellent Benchmark (http://www.theswamp.org/lilly_pond/mp/lisp/benchmark.txt?nossi=1) utility.
;_measure coordinates and retrive a list
;_stp - start point
;_etp - end point
;_ int - interval
(defun measurex (stp etp int / x lst)
(setq x 0)
(append
(repeat (fix (/ (distance stp etp) int))
(setq lst
(cons
(polar stp (angle stp etp) (setq x (+ x int)))
lst
) ;_ end of cons
) ;_ end of setq
) ;_ end of lst
(list stp)
)
) ;_ end of defun
;_$ (setq res (measurex '(0 0 0) '(12 0 0) 2))
1. how can i make sure that the precision of the coordinate '(0 0 0) is respecting the precision factor in rule?
2. advise [how to] make this function more stable and efficient
hi
simple code to get an array of points. i tried using macpar,lambda and others....but coudnt make it work.
is this the only way to achieve the task?Code: [Select](defun getPolarCoors(stp int / lst )
(setq x 0)
(repeat 4
(setq lst (cons (polar stp 0.0 (setq x (+ x int)) )lst))
)
)
Please explain exactly what you mean ..
what is "respecting the precision factor in rule" ?
$ (setq res (measurex '(0 0 0) '(12 0 0) 2))
Where is it not stable ??
Where is it not efficient ??
i dont understand why my first post and the current one is not related in your eyes.both retrivie points , both needs iteration of some sort.
Is this the function signature ??Code - Auto/Visual Lisp: [Select]
;| IncrementXvalue <StartPoint> <Interval> StartPoint -> [list of 3 numbers ] Point coordinate list. Interval -> [number] Step distance. Return -> [list of [point list][point list][point list]] The x component of each point shall be sequentially increased by the Interval value eg: (IncrementXvalue (list 1 2 42) 5) ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0)) |;
exactly Kerry!
< ... >QuoteWhere is it not stable ??
Where is it not efficient ??
thats why i post it. i want you guys to try to kill it. im sure its easy for you
Thanks
Shay
We are not mind readers ! and can only work with what we are given.
I think that YOU should be the one to look for ways to "kill" the routine. That is an excellent way to learn how to understand code.
If you can't find at least 3 or 4 I'd be very disappointed. :)
Code: [Select](polar stp (angle stp etp) (setq x (+ x int)))
You don't need complicated error trapping.
Do you know the words assert or assertion ?
(if
(and
(is int a number)
(is start a point)
(is end a point)
)
;; then
(proceed)
)
(defun isCoordReal (crd)
(car
(mapcar
(function
(lambda (x y z)
(= (type x) 'REAL)
(= (type y) 'REAL)
(= (type z) 'REAL)
)
)
(list (car crd))
(list (cadr crd))
(list (caddr crd))
)
)
)
Code: [Select](polar stp (angle stp etp) (setq x (+ x int)))
Hint: (angle stp etp) will not change, therefore it need not be recalculated for every iteration of the repeat loop :wink:
This should offer some minor performance improvements:Code - Auto/Visual Lisp: [Select]
) ) )
i cant test it right now but looking at the code cant really understand why you put s in a list?Code - Auto/Visual Lisp: [Select]
i cant test it right now but looking at the code cant really understand why you put s in a list?
< ..>
no i dont , but i read about it, i assume its thisCode: [Select](defun isCoordReal (crd)
(car
(mapcar
(function
(lambda (x y z)
(= (type x) 'REAL)
(= (type y) 'REAL)
(= (type z) 'REAL)
)
)
(list (car crd))
(list (cadr crd))
(list (caddr crd))
)
)
)
i cant understand why but the returned value is a list (T) rather than an element. the car exp is totally ignored.
Quotei cant test it right now but looking at the code cant really understand why you put s in a list?
What does the function return ... which variable ??
what is the result of thisCode - Auto/Visual Lisp: [Select]
Then:Code - Auto/Visual Lisp: [Select]
Then :Code - Auto/Visual Lisp: [Select]
< ..>
no i dont , but i read about it, i assume its thisCode: [Select](defun isCoordReal (crd)
(car
(mapcar
(function
(lambda (x y z)
(= (type x) 'REAL)
(= (type y) 'REAL)
(= (type z) 'REAL)
)
)
(list (car crd))
(list (cadr crd))
(list (caddr crd))
)
)
)
i cant understand why but the returned value is a list (T) rather than an element. the car exp is totally ignored.
Perhaps have a play with something like this as an alternative :Code - Auto/Visual Lisp: [Select]
Perhaps have a play with something like this as an alternative :Not doubt you meant to use vl-every instead of vl-member-if:Code - Auto/Visual Lisp: [Select]
(point-p '(1 "a" 3)) => T
anyhow i would be happy to understand why my writen function return a list and not an atom as it were instructed to do.But your function does not return a list! It is flawed though:
(isCoordReal '(1.0 "a" 3.0)) => T
Perhaps have a play with something like this as an alternative :Not doubt you meant to use vl-every instead of vl-member-if:Code - Auto/Visual Lisp: [Select]Code: [Select](point-p '(1 "a" 3)) => T
wow....its too advanced for me. can you please give a simple code which explain better how to design recursive functions?
(defun f (s i n)
(if (> n 0)
(cons s (f (mapcar '+ s i) i (1- n)))
)
)
Or(defun f (s i n)
(if (> n 1)
(cons s (f (mapcar '+ s i) i (1- n)))
(list s)
)
)
_$ (f '(1 2 42) '(5 0 0) 5)
((1 2 42) (6 2 42) (11 2 42) (16 2 42) (21 2 42))
(defun f (s i n)
(if (> n 0)
(cons (setq s (mapcar '+ s i)) (f s i (1- n)))
)
)
_$ (f '(1 2 42) '(5 0 0) 4)
((6 2 42) (11 2 42) (16 2 42) (21 2 42))
no i dont , but i read about it, i assume its thisCode: [Select](defun isCoordReal (crd)
(car
(mapcar
(function
(lambda (x y z)
(= (type x) 'REAL)
(= (type y) 'REAL)
(= (type z) 'REAL)
)
)
(list (car crd))
(list (cadr crd))
(list (caddr crd))
)
)
)
i cant understand why but the returned value is a list (T) rather than an element. the car exp is totally ignored.
(defun 3Dpoint-p (pt)
(and (numberp (car pt)) (numberp (cadr pt)) (numberp (caddr pt)) (null (cdddr pt)))
)
This should offer some minor performance improvements:Code - Auto/Visual Lisp: [Select]
A genius is a person who displays exceptionally superior intellectual ability, creativity, or originality, typically to a degree that is associated with the achievement of new advances in a domain of knowledge. A scholar in many subjects or a scholar in a single subject may be referred to as a genius.[1] There is no scientifically precise definition of genius, and the question of whether the notion itself has any real meaning has long been a subject of debate, although psychologists are converging on a definition that emphasizes creativity and eminent achievement.
(defun mapcar-x2(lst)
(mapcar
(function
(lambda (a b)
(command "._line" a b)
(princ (strcat "Segment length: " (distance a b) ))
)
)
lst ;_ how to shuffle the list in a way a and b will have not the same number each iteration?
lst
)
)
(mapcar (function (lambda (a b)
.....
)
)
lst
(cdr lst)
)
(defun mapcar-x2(lst)
(mapcar
(function
(lambda (a b)
(princ (strcat "Segment length: " (distance a b) ))
)
)
lst
(cdr lst)
)
)
_$ (mapcar-x2 '( (0 0 0)(0 0 0)(0 0 0)(0 0 0)) )
; error: bad argument type: stringp 0.0
Hi Evgeniy,
Nice bit of code.
I think (f (list 1 2 42) '(5 2 0) 4)
should return ( (21 10 42) (16 8 42) (11 6 42) (6 4 42) )
Regards,
Code: [Select](defun mapcar-x2(lst)
(mapcar
(function
(lambda (a b)
(princ (strcat "Segment length: " (distance a b) ))
)
)
lst
(cdr lst)
)
)Code: [Select]_$ (mapcar-x2 '( (0 0 0)(0 0 0)(0 0 0)(0 0 0)) )
Code: [Select]; error: bad argument type: stringp 0.0
_$ (mapcar-x2 '( (0 0 0)(1 0 0)(2 0 0)(3 0 0)) )
1th iteration
a = 0
b = 1
2th iteration
a = 1
b = 2
3th iteration
a = 2
b = 3
1th iteration
a = 0
b = 1
2th iteration
a = 2
b = 3
i wantCode: [Select]1th iteration
a = 0
b = 1
2th iteration
a = 2
b = 3
Why mapcar wasnt your defualt choise for thos task?
Why mapcar wasnt your defualt choise for thos task?
Because mapcar iterates over every item; you are asking to iterate over every other item.
;_divids length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval
(defun measurex ( s e i / a r )
(setq r (list s)
a (angle s e)
)
(repeat (fix (/ (distance s e) i))
(setq r (cons (polar (car r) a i) r))
)
)
;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts
(defun dividex ( s e i / a r sl)
(setq r (list s)
a (angle s e)
sl (/ (distance s e) i) ;
)
(repeat (fix i)
(setq r (cons (polar (car r) a sl) r))
)
)
;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the sprcified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ang twc rm rwl rwc PTLIST)
(if (and (>= typ mn) (<= typ mx)) ;; if typ is greater/smaller or equal to min/max
(if (/= (distance (setq stp (cadr (setq PTLIST (measurex stp etp typ)))) etp) typ );; if ther is reminder
(append (vl-remove (car PTLIST) PTLIST) (dividex stp etp 2))
PTLIST
)
)
)
(getTypicalLength '(0.0 0.0 0.0)'(20.0 0.0 0.0) 6.0 5.0 8.0)
((20.0 0.0 0.0)(16.0 0.0 0.0) (12.0 0.0 0.0) (6.0 0.0 0.0) (0.0 0.0 0.0))
(vl-remove (car PTLIST) PTLIST)
Instead of this:(cdr PTLIST)
When you use append in your 3rd function the new start point is still in the list of points.
The rest you should be able to figure out yourself.
Note:
Please don't use this:Code: [Select](vl-remove (car PTLIST) PTLIST)
Instead of this:Code: [Select](cdr PTLIST)
;_divids length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval
(defun measurex ( s e i / a r )
(setq r (list s)
a (angle s e)
)
(repeat (fix (/ (distance s e) i))
(setq r (cons (polar (car r) a i) r))
)
)
;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts
(defun dividex ( s e i / a r sl)
(setq r (list s)
a (angle s e)
sl (/ (distance s e) i) ;
)
(repeat (fix i)
(setq r (cons (polar (car r) a sl) r))
)
)
;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the sprcified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ang twc rm rwl rwc PTLIST)
(if (and (>= typ mn) (<= typ mx)) ;; if typ is greater/smaller or equal to min/max
(if (/= (distance (setq stp (cadr (setq PTLIST (measurex stp etp typ)))) etp) typ );; if the distance between past last point to the end is different than typ
;; it means that there is a remaining
(reverse (append (dividex stp etp 2)(cddr PTLIST)))
(reverse PTLIST)
)
)
)
_$ (getTypicalLength '(0.0 0.0 0.0)'(20.0 0.0 0.0) 6.0 5.0 8.0)
((0.0 0.0 0.0) (6.0 0.0 0.0) (12.0 0.0 0.0) (16.0 0.0 0.0) (20.0 0.0 0.0))
;_divids length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval
(defun measurex ( s e i / a r )
(setq r (list s)
a (angle s e)
)
(repeat (fix (/ (distance s e) i))
(setq r (cons (polar (car r) a i) r))
)
)
;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts
(defun dividex ( s e i / a r sl)
(setq r (list s)
a (angle s e)
sl (/ (distance s e) i) ;
)
(repeat (fix i)
(setq r (cons (polar (car r) a sl) r))
)
)
;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the specified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ml i w f )
(setq i 1)
(if (and (> typ mn) (< typ mx)(>= (distance stp etp) typ))
(if (/= (setq dist (distance (setq stp (cadr (setq ml (measurex stp etp typ)))) etp)) typ ) ;_if there is a reminder
(while (null f) ;_flag.is there a need to loop? t=no nil=yes
(if (>= dist mn) ;_is greater than minimum limit
(if (<= dist mx) ;_is smaller than maxiuum limit
(if (null w) ;_flag. segment were divided?. t=no nil-yes
(setq f t ml (cdr ml) ml (cons etp ml)) ;_flag. no loop is needed. return list+end point
(progn
(setq f t) ;_flag. no loop is needed.
(append (reverse(cdr ml))(cdr(reverse(dividex stp etp i)))) ;_return list+divid seg+reminder equally
)
)
(setq i (1+ i) dist (/ (distance stp etp)i) w t) ;_divid seg when it doesnt meet maximum length, start over
)
(princ "Cannot constract a layout with those limits") ;_stop when seg length doesnt meet minimum length
)
)
ml ;_return list when no reminder left
)
)
)