Visual LISP, AutoLISP and General Customization

## Visual LISP, AutoLISP and General Customization

*Expert Elite*
Posts: 2,406
Registered: ‎11-24-2009
Message 21 of 26 (151 Views)

# Re: Polyline areas

03-23-2013 09:58 AM in reply to: Kent1Cooper

Kent1Cooper wrote:

But the same can be accomplished by having a rounding function that only rounds up.

But it should not round up if the starting value is an exact multiple.  If the largest Polyline is 500 square units, with a divided value of 100, that should not be increased to 105, because the un-increased size of 100 will make a top category from 400 to 500, which will include the size of that largest Polyline.

Nice explanation Kent.

perhaps

```(defun roundup (num near / v)
(if (<= (setq v (* (fix (/ (+ num (/ near 2.0)) near)) near))
num
)
(+ near v)
v
)
)```

On the last code i posted . i  tested the n value multiplied by number of range if it encompass the max value , if not then add the extra range to n. The adding of the extra range will be done outside the rounding routine. Not sure how that approach fairs with straigth round-up only routine [posted snippet]

*Expert Elite*
Posts: 5,237
Registered: ‎09-13-2004
Message 22 of 26 (144 Views)

# Re: Polyline areas

03-23-2013 10:55 AM in reply to: pbejse

pbejse wrote:
...

perhaps

```(defun roundup (num near / v)
(if (<= (setq v (* (fix (/ (+ num (/ near 2.0)) near)) near))
num
)
(+ near v)
v
)
)```

On the last code i posted . i  tested the n value multiplied by number of range if it encompass the max value , if not then add the extra range to n. The adding of the extra range will be done outside the rounding routine. Not sure how that approach fairs with straigth round-up only routine [posted snippet]

That's probably a little more work than needed, mostly because the whole adding-half-the-nearest-multiple-number thing is there specifically to make it round either up or down.  Think of it in terms of rounding to the nearest whole number ['near' = 1], where a number with a .5 or greater decimal part should be rounded up, but with less than that should be rounded down.  It adds .5 [half of 1] to the number, and uses (fix) to strip off any decimal portion from the result.  So if the decimal component is .5 or more, adding .5 to it sends it up to or above the next whole number, and that's where (fix) leaves it.  But if the decimal component is less than .5, adding .5 leaves it still below that next whole number, so (fix) takes it downward.

When you want to round up only, there's no purpose in messing around with half the value of the nearest-multiple number that way.

Here's the way it's done [split into more lines here] in my latest attached routine, after calculating 'areainc' as the unrounded quotient from dividing the largest area by the number of categories:

(setq areainc ; replacing previously calculated value

(if (= (rem areainc mult) 0); if it's an exact multiple

(fix areainc); then -- leave it alone

(* (1+ (fix (/ areainc mult))) mult); else -- round it up to the next-

; higher multiple; that is, round the number of times 'mult' goes

; into it down, then add one, and multiply that by 'mult'

); if

); setq

That's based on the use of integers for the nearest-multiple-of number [the 'mult' variable; in our examples, 5], and the resulting category sizes.  But the same approach could be used with non-integer values.  For instance, if you want to round something to the nearest multiple of 2.5 but upward only, you would use 2.5 for 'mult' and omit the (fix) wrapper in the 'then' argument:

(setq areainc

(if (= (rem areainc mult) 0); if it's an exact multiple

areainc; then -- leave it alone [without (fix)]

(* (1+ (fix (/ areainc mult))) mult); else -- round it up to the next-higher multiple

); if

); setq

So I think a simpler generic round-up-only-and-only-if-not-already-an-exact-multiple function would be:

(defun roundup (num near)
(if (= (rem num near) 0)
num ; then [use (fix num) if dealing in integers only]
(* (1+ (fix (/ num near))) near); else
); if
); defun

Kent Cooper
*Expert Elite*
Posts: 2,406
Registered: ‎11-24-2009
Message 23 of 26 (132 Views)

# Re: Polyline areas

03-24-2013 07:57 PM in reply to: Kent1Cooper

Kent1Cooper wrote:

When you want to round up only, there's no purpose in messing around with half the value of the nearest-multiple number that way.

So I think a simpler generic round-up-only-and-only-if-not-already-an-exact-multiple function would be:

(defun roundup (num near)
(if (= (rem num near) 0)
num ; then [use (fix num) if dealing in integers only]
(* (1+ (fix (/ num near))) near); else
); if
); defun

By george I think i got it.  It took me a while to understand the reasoning behind the round-up, only after reading again your last post that it clicks.

Thank you for the lesson Kent. Math was never my greatest strength

Cheers

Distinguished Contributor
Posts: 162
Registered: ‎02-10-2004
Message 24 of 26 (120 Views)

# Re: Polyline areas

03-25-2013 01:10 AM in reply to: sean.keohane

Thank you  Pbejse and Kent. I haven't used your code yet but I had to thank you both for your help. I need to copy this down now and study it to come close to understanding it 'cause I sure don't understand it at first glance.( I've got the tortoise approach to lisp!)

Regards

Sean

*Expert Elite*
Posts: 5,237
Registered: ‎09-13-2004
Message 25 of 26 (108 Views)

# Re: Polyline areas

03-25-2013 06:35 AM in reply to: sean.keohane

sean.keohane wrote:

Thank you  Pbejse and Kent. I haven't used your code yet but I had to thank you both for your help. I need to copy this down now and study it to come close to understanding it 'cause I sure don't understand it at first glance.( I've got the tortoise approach to lisp!)

....

You're welcome -- use it in good health.  I threw in some further EXPLanatory notes in the attached version, to help you understand what it's doing.

Kent Cooper
Distinguished Contributor
Posts: 162
Registered: ‎02-10-2004
Message 26 of 26 (105 Views)

# Re: Polyline areas

03-25-2013 06:50 AM in reply to: pbejse

Hi Kent,

That is seriously cool to do that for me! You are a gentleman.

Best Regards

Sean

### You are not logged in.

Announcements
Are you familiar with the Autodesk Expert Elites? The Expert Elite program is made up of customers that help other customers by sharing knowledge and exemplifying an engaging style of collaboration. To learn more, please visit our Expert Elite website.