Author Topic: Polar mode simulation (Just fun)  (Read 1421 times)

0 Members and 1 Guest are viewing this topic.

Augusto

  • Newt
  • Posts: 75
Polar mode simulation (Just fun)
« on: February 02, 2019, 09:18:14 AM »
This is just fun to exercise.
I'm trying to simulate the polar.  :-D
Then I will read the "F8" and "F10" keys.
What is your opinion?
Would they do it differently?

This is very fun and rewarding. :yay!:

Code: [Select]
(defun c:test ()

  ;; String to List  -  Lee Mac
  ;; Separates a string using a given delimiter
  ;; str - [str] String to process
  ;; del - [str] Delimiter by which to separate the string
  ;; Returns: [lst] List of strings
  (defun LM:str->lst (str del / pos)
    (if (setq pos (vl-string-search del str))
      (cons (substr str 1 pos)
    (LM:str->lst (substr str (+ pos 1 (strlen del))) del)
      ) ;_ >cons
      (list str)
    ) ;_ >if
  ) ;_ >defun
 
  ;; Unique  -  Lee Mac
  ;; Returns a list with duplicate elements removed.
  (defun LM:Unique (l)
    (if l
      (cons (car l) (LM:Unique (vl-remove (car l) (cdr l))))
    ) ;_ >if
  ) ;_ >defun
 
  (defun polarline (p / g)
    (while (= 5 (car (setq g (grread t 13 0))))
      (redraw)
      (setq ang (angle p (setq pt2 (cadr g)))
    closestAngle nil
      ) ;_ >setq
      (setq listAngles (list ang))
      (setq listAngles (vl-sort(append listAngles polarAngles)'<))
      (setq position (vl-position ang listAngles))
      (if (= position 0)
(setq before (last listAngles))
(setq before (nth (- position 1) listAngles))
      ) ;_ >if
      (if (= ang (last listAngles))
(setq after (car listAngles))
(setq after (nth (+ position 1) listAngles))
      ) ;_ >if
      (cond
((<=
   (abs
     (- ang before)
   ) ;_ >abs
   magneticFactor
) ;_ ><=
(setq ang before
       trackline t
) ;_ >setq
)
((<=
   (abs
     (- ang after)
   ) ;_ >abs
   magneticFactor
) ;_ ><=
(setq ang after
       trackline t
) ;_ >setq
)
      ) ;_ >cond
      (redraw)
      (if trackline
(progn
  (grdraw p (setq pt2(polar p ang (distance p pt2))) 1)
  (grdraw pt2 (polar pt2 ang tracklineDist) 3)
  )
(grdraw p (polar p ang (distance p pt2)) 1)
      ) ;_ >if
      (setq trackline nil)
    ) ;_ >while
  ) ;_ >defun

  ; converts degrees to radians
  (defun deg2rad(d) (* pi (/ d 180.0)))

  (setq ang              (getvar 'POLARANG) ;increment angles
magneticFactor (deg2rad 3)     ;to simulate magnetic
tracklineDist    10000     ;lenght of track line
nextAngle        0.0
incrementAngles  '()
additionalAngles '()
polarAngles      '()
orthogonalAngles '(0.0 1.5708 3.14159 4.71239)
listAngles       '()     ;temporary list
  ) ;_ >setq

  (while (< nextAngle (* pi 2))
    (setq nextAngle (+ nextAngle ang))
    (if (< nextAngle (* pi 2))
      (progn
(setq incrementAngles (append incrementAngles (list nextAngle)))
(princ nextAngle)
      ) ;_ >progn
    ) ;_ >if
  ) ;_ >while

  (setq additionalAngles
(append additionalAngles
(mapcar
   (function
     (lambda (ang)
       (deg2rad (atof ang))
     ) ;_ >lambda
   ) ;_ >function
   (LM:str->lst (getvar 'POLARADDANG) ";")
) ;_ >mapcar
) ;_ >append
  ) ;_ >setq
  (setq polarAngles (append polarAngles incrementAngles))
  (setq polarAngles (append polarAngles orthogonalAngles))
  (setq polarAngles (append polarAngles additionalAngles))
  (setq polarAngles (vl-sort(LM:Unique polarAngles)'<))

  (polarline (getpoint "\nBase Point: "))
  (princ)
) ;_ >defun

« Last Edit: February 02, 2019, 09:21:53 AM by Augusto »

Augusto

  • Newt
  • Posts: 75
Re: Polar mode simulation (Just fun)
« Reply #1 on: February 02, 2019, 11:57:26 AM »
Code: [Select]
(defun c:test ()

  ;; String to List  -  Lee Mac
  ;; Separates a string using a given delimiter
  ;; str - [str] String to process
  ;; del - [str] Delimiter by which to separate the string
  ;; Returns: [lst] List of strings
  (defun LM:str->lst (str del / pos)
    (if (setq pos (vl-string-search del str))
      (cons (substr str 1 pos)
    (LM:str->lst (substr str (+ pos 1 (strlen del))) del)
      ) ;_ >cons
      (list str)
    ) ;_ >if
  ) ;_ >defun
 
  ;; Unique  -  Lee Mac
  ;; Returns a list with duplicate elements removed.
  (defun LM:Unique (l)
    (if l
      (cons (car l) (LM:Unique (vl-remove (car l) (cdr l))))
    ) ;_ >if
  ) ;_ >defun
 
  (defun polarline (p / g)
    (while (= 5 (car (setq g (grread t 13 0))))
      (redraw)
      (setq ang (angle p (setq pt2 (cadr g)))
    closestAngle nil
      ) ;_ >setq
      (setq listAngles (list ang))
      (setq listAngles (vl-sort(append listAngles polarAngles)'<))
      (setq position (vl-position ang listAngles))
      (if (= position 0)
(setq before (last listAngles))
(setq before (nth (- position 1) listAngles))
      ) ;_ >if
      (if (= ang (last listAngles))
(setq after (car listAngles))
(setq after (nth (+ position 1) listAngles))
      ) ;_ >if
      (cond
((<=
   (abs
     (- ang before)
   ) ;_ >abs
   magneticFactor
) ;_ ><=
(setq ang before
       trackline t
) ;_ >setq
)
((<=
   (abs
     (- ang after)
   ) ;_ >abs
   magneticFactor
) ;_ ><=
(setq ang after
       trackline t
) ;_ >setq
)
      ) ;_ >cond
      (redraw)
      (if trackline
(progn
  (grdraw p (setq pt2 (polar p ang (distance p pt2))) 1)
  (repeat tracklineTraces
    (grdraw pt2 (setq pt2 (polar pt2 ang tracesSpace))3)
    (setq pt2 (polar pt2 ang tracesSpace))
  ) ;_ >repeat
) ;_ >progn
(grdraw p (polar p ang (distance p pt2)) 1)
      ) ;_ >if
      (setq trackline nil)
    ) ;_ >while
  ) ;_ >defun

  ; converts degrees to radians
  (defun deg2rad(d) (* pi (/ d 180.0)))

  (setq ang              (getvar 'POLARANG) ;increment angles
magneticFactor (deg2rad 3)     ;to simulate magnetic
tracklineTraces  500     ;number of dashed line dashes
tracesSpace      0.1     ;spacing and length of dashes
nextAngle        0.0
incrementAngles  '()
additionalAngles '()
polarAngles      '()
orthogonalAngles '(0.0 1.5708 3.14159 4.71239)
listAngles       '()     ;temporary list
  ) ;_ >setq

  (while (< nextAngle (* pi 2))
    (setq nextAngle (+ nextAngle ang))
    (if (< nextAngle (* pi 2))
      (progn
(setq incrementAngles (append incrementAngles (list nextAngle)))
(princ nextAngle)
      ) ;_ >progn
    ) ;_ >if
  ) ;_ >while

  (setq additionalAngles
(append additionalAngles
(mapcar
   (function
     (lambda (ang)
       (deg2rad (atof ang))
     ) ;_ >lambda
   ) ;_ >function
   (LM:str->lst (getvar 'POLARADDANG) ";")
) ;_ >mapcar
) ;_ >append
  ) ;_ >setq
  (setq polarAngles (append polarAngles incrementAngles))
  (setq polarAngles (append polarAngles orthogonalAngles))
  (setq polarAngles (append polarAngles additionalAngles))
  (setq polarAngles (vl-sort(LM:Unique polarAngles)'<))

  (polarline (getpoint "\nBase Point: "))
  (princ)
) ;_ >defun