Author Topic: Change a negative number to positive from a list ...  (Read 2318 times)

0 Members and 1 Guest are viewing this topic.

Hangman

  • Swamp Rat
  • Posts: 566
Change a negative number to positive from a list ...
« on: July 09, 2010, 07:44:35 PM »
Afternoon everyone,

I am playing with a point list of some zero length arcs, and wanting to put circles in place of these zero length arcs.
So I am pulling the DXF code 10 to get the center point.
There are times however (I don't understand how this is working) when the point value has a negative X value even though the zero length arc is clearly in the positive.
So, for example, I have the DXF code 10 of a zero length arc:
Quote
(10 -443.597 275.719 0.0)
but in reality, the arc is sitting at:
Quote
(10 443.597 275.719 0.0)

I am disecting this list and want to change the negative value to a positive and then recreate the list.
By pulling the X value out:
Code: [Select]
(setq a (cdr (assoc 10 <the object>)))
(setq b (car a))
!b  -443.597
So I was thinking I would have to convert this to a string, then use SUBST to strip the negative from the value, then convert back to a number and create a new list to use for the location of the circle.
If I use RTOS, I get
Quote
"-36'-11 19/32\""
and I don't know that I want that, although it shouldn't matter as the value is not changing, merely the negative value.
But, is there a better way to go about doing this ??

I haven't got the code done yet, ... I'm trying to think ahead before spending too much time.  Here's what I do have so far:
Code: [Select]
< ... >
(while (setq Obj (ssname SS (setq i (1+ i))))
    (if (setq eList (entget Obj))
      (progn
        (setq objAnglS (cdr (assoc 50 eList))
              objAnglE (cdr (assoc 51 eList)))
        (if (equal objAnglS objAnglE 0.001)
          (progn
            (setq objLayr (cdr (assoc 8 eList))
                  objColr (cdr (assoc 62 eList))
                  objLTyp (cdr (assoc 6 eList))
                  objLWgt (cdr (assoc 370 eList))
                  objCtrPT (cdr (assoc 10 eList))
                  objRad (cdr (assoc 40 eList))
            )
            (CheckNegativeValue)
 < ... >
(defun CheckNegativeValue (/ )
  (setq objCtrPT-X (car objCtrPT)
        objCtrPT-Y (car (cdr objCtrPT)))
  (if (minusp objCtrPT-X)
    ( ...
And my psuedo code:
Quote
; if x is negative and y is negative
; then do nothing
;
; if x is negative and y positive
; then make x positive
; recreate point list
;
; if y is negative and x positive
; then make y positive
; recreate point list

Anyway; thoughts, questions, suggestions, ideas, etc. are always welcome.   Thank you.
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Lee Mac

  • Seagull
  • Posts: 12929
  • London, England
Re: Change a negative number to positive from a list ...
« Reply #1 on: July 09, 2010, 07:50:09 PM »
abs

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Change a negative number to positive from a list ...
« Reply #2 on: July 09, 2010, 07:52:40 PM »

easy

(setq dxfstart '(10 -443.597 275.719 0.0) )

(mapcar 'abs dxfstart)

;;==>> (10 443.597 275.719 0.0)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Change a negative number to positive from a list ...
« Reply #3 on: July 09, 2010, 07:54:27 PM »
... and don't bother to check if the values are negative, just DoIt

except I didn't fully read your spec .... thats not exactly what you want is it ..
« Last Edit: July 09, 2010, 08:00:54 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Change a negative number to positive from a list ...
« Reply #4 on: July 09, 2010, 08:37:49 PM »
Perhaps ...

Code: [Select]

(defun _ValidateDXF10 (dxf10 / X Y Z)
  (setq X (cadr dxf10)
Y (caddr dxf10)
Z (cadddr dxf10)
  )
  (cond
    ((and (minusp X) (not (minusp Y)))
     ;; make X Positive
     (setq new_dxf10 (list 10 (abs X) Y Z))
    )
    ((and (minusp Y) (not (minusp X)))
     ;; make Y Positive
     (setq new_dxf10 (list 10 X (abs Y) Z))
    )
    (T
     ;; either both positive or both negative, so leave
     (setq new_dxf10 dxf10)
    )
  )
)




(_ValidateDXF10 '(10 443.597 275.719 0.0))    ;-> (10 443.597 275.719 0.0)

(_ValidateDXF10 '(10 -443.597 -275.719 0.0))   ;-> 10 -443.597 -275.719 0.0)

(_ValidateDXF10 '(10 -443.597 275.719 0.0))    ;-> (10 443.597 275.719 0.0)

(_ValidateDXF10 '(10 443.597 -275.719 0.0))    ;-> (10 443.597 275.719 0.0)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Change a negative number to positive from a list ...
« Reply #5 on: July 09, 2010, 08:46:10 PM »

This is cleaner

Code: [Select]

(defun _ValidateDXF10 (dxf10)
  (if (and (minusp (cadr dxf10)) (minusp (caddr dxf10)))
    dxf10
    (mapcar 'abs dxf10)
  )
)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Hangman

  • Swamp Rat
  • Posts: 566
Re: Change a negative number to positive from a list ...
« Reply #6 on: July 12, 2010, 10:28:32 AM »
I'll bet you guys wonder about some of my questions.  They seem so simple according to your explanations.  And yes, I should have seen 'abs', but alas, I love watching you guys work.  It is such an inspiration.

Just look at what Kerry did here.  First he jumps in there with a really quick explanation.  Then realizing there's more to it than the quick assumption, he redoes his mastry with a complete set of code and explanation (absolutely brilliant), then to top it off, he comes back again and refines his process to make it even better.

That's not just code, that's art.

Thank you Kerry.
Hangman  8)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Drafting Board, Mechanical Arm, KOH-I-NOOR 0.7mm
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~