Jump to content

Heron's formula - Help with a lisp


Guest

Recommended Posts

  • Replies 48
  • Created
  • Last Reply

Top Posters In This Topic

  • BIGAL

    10

  • rlx

    6

  • steven-g

    3

  • dlanorh

    1

Top Posters In This Topic

Posted Images

  • 1 month later...

Hi i need a litle help with this code. I update the code but i find out that when i do the calculation the results is not all the time correct. I believe tha the code in the background  calculations use more decimals than 2 so when i do the calculations with my calculatior i find the diference. Can any one help me with this ?

 

 

(defun c:heron (/ tri-no p1 p2 p3 da db dc s E cp lst cnt fn fp Etotal)
(vl-load-com)
(setvar "OSMODE" 9) 
 (command "_layer" "_m" "Area" "_c" "7" "" "")
   (defun tricent (pt1 pt2 pt3)(mapcar '(lambda (x y z) (/ (+ x y z) 3)) pt1 pt2 pt3))
  (setq tri-no 0 Etotal 0)
  (while (and (setq p1 (getpoint "\nP1 : "))(setq p2 (getpoint "\nP2 : "))(setq p3 (getpoint "\nP3 : "))
              (setq da (distance p2 p3)) (setq db (distance p3 p1)) (setq dc (distance p1 p2))
              (setq s (/ (+ da db dc) 2.0) ) (setq E (sqrt (* s (- s da) (- s db) (- s dc)))))
    ; while valid points are given
    (if (assoc (setq cp (tricent p1 p2 p3)) lst)
      (prompt "\nPoint allready entered")
      (progn
        (setq lst (append lst (list (cons cp (list (setq tri-no (1+ tri-no)) s da db dc E )))))
        (entmakex (list '(0 . "TEXT") (cons 10 cp) (cons 40 0.5) (cons 1 (strcat "E" (itoa tri-no)))))
      )
    )
  )
  (if (and (vl-consp lst) (setq fn (vl-filename-mktemp ".txt"))(setq fp (open fn "w")))
    (progn
      (foreach x lst
        (setq x (cdr x) tri-no (nth 0 x) s (nth 1 x) da (nth 2 x) db (nth 3 x) dc (nth 4 x) E (last x))
	(write-line
	 (strcat "r" (vl-princ-to-string tri-no) " = " "1/2" " x " "(" (vl-princ-to-string (rtos da 2 2 ))  " + " (vl-princ-to-string (rtos db 2 2 )) " + " (vl-princ-to-string (rtos dc 2 2 )) " ) = " (rtos s 2 2) " m." ) fp)
        (write-line
          (strcat "E" (vl-princ-to-string tri-no) " = " "\U+221A" " " (vl-princ-to-string (rtos s 2 2 )) " x (" (vl-princ-to-string (rtos s 2 2))
                  " - " (vl-princ-to-string (rtos da 2 2 )) ") x (" (vl-princ-to-string (rtos s 2 2)) " - " (vl-princ-to-string (rtos db 2 2 )) ") x ("
                  (vl-princ-to-string (rtos s 2 2)) " - " (vl-princ-to-string (rtos dc 2 2 )) ") = " (rtos E 2 2) " sq.m." ) fp)
	(write-line (strcat "--------------------------------------------------------------------------------------------------") fp)
        (setq Etotal (+ Etotal E))
      )
      (write-line (strcat "Eολ. = " (vl-princ-to-string (rtos Etotal 2 2)) " sq.m.") fp)
      (close fp)
    )
  )
  (startapp "notepad" fn)
  (setvar "OSMODE" 9) 
  (princ)
);close defun

 

Link to comment
Share on other sites

Hi BIGAL .I dont understand what this is

 

3 (lambda (x y z) (/ (+ x y z) 3)) 

 

I try to change this line

 (setq da (distance p2 p3)) (setq db (distance p3 p1)) (setq dc (distance p1 p2))

to this

 

 (setq da (rtos (distance p2 p3)2 2)) (setq db (rtos(distance p3 p1)2 2 )) (setq dc (rtos(distance p1 p2) 2 2))

 

But is not working. The idea is to round up the numbers before the calculations

 

Can any one help.

 

Thanks

Link to comment
Share on other sites

is any way to be full calculated with 2 dec .My problem is that when i do the calculation with the calculator gimes me other results. I want when i do the calclulations to have the same results with 2 dec

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...