Jump to content

Stuck in AutoLISP using polar


Cinthia99

Recommended Posts

Well I'm sort of stuck and don't know what to do. I need to write program using AutoLisp, which would draw five-pointed star. I don't have any knowledge in AutoLisp programming, but managed to write this and it seems to me, correct, but AutoCAD won't draw anything. Maybe someone could help? (long numbers are angles in radian) Code:

(defun C:Figura3 ()
  (setq pl (getpoint "\nStart coordinate: ")) ;;; Coordinates of circle center
  (setq aukst (getint "\nRadius: "))
  ;;; Coordinates of vertices
  (setq p2 (polar p1 1.570796327 aukst)) ;;; 90 (degrees) 
  (setq p3 (polar p1 2.827433388 aukst)) ;;; 162
  (setq p4 (polar p1 4.08407045 aukst)) ;;; 234
  (setq p5 (polar p1 5.340707511 aukst)) ;;; 306
  (setq p6 (polar p1 0.3141592654 aukst)) ;;; 18
  ;;; Drawing
  (command "color" "white")
  (command "lweight" 0.35)
  (command "circle" p1 aukst)
  (command "line" p2 p4 p6 p3 p5 p2 "")
)
Link to comment
Share on other sites

The main problem is that on your first (setq), you are setting the variable "pl" (that's the letter "P" and the letter "L"), but your subsequent setq statements, with the polar function, are referencing the variable "p1" (the letter "P" and the number "1"), as well as the line to draw the circle.

Link to comment
Share on other sites

Here's my quick take on it .. ask questions if you have any!

(defun c:figura3 (/ aukst p1 p2 p3 p4 p5 p6) ;; <- Localize variables
  ;; Make sure that we have a picked point AND a radius otherwise the code below will bomb
  (if (and (setq p1 (getpoint "\nStart coordinate: "))
	   ;; Coordinates of circle center
	   ;; RJP changed to getdist
	   (setq aukst (getdist p1 "\nRadius: "))
      )
    (progn ;; Coordinates of vertices
	   ;; I'd use angtof for legibility since you're starting out
	   (setq p2 (polar p1 (angtof "90") aukst))
	   (setq p3 (polar p1 (angtof "162") aukst))
	   (setq p4 (polar p1 (angtof "234") aukst))
	   (setq p5 (polar p1 (angtof "306") aukst))
	   (setq p6 (polar p1 (angtof "18") aukst))
	   (command "color" "white")
	   (command "lweight" 0.35)
	   (command "circle" p1 aukst)
	   (command "line" p2 p4 p6 p3 p5 p2 "")
    )
  )
  (princ)
)

 

Link to comment
Share on other sites

A star's vertices can be defined through a pentagon shape, so heres how I'd processt he task -

; Create a point 'pt'
; Create a polygon with 5 sides, where the center of the polygon is the 'pt', with some fixed radius
; investigate the polygon's vertices - how they are related to 'pt'
; Reverse engineer the manually-created point and polygon, 
; by obtaining the list of polygon's vertices, based on how they are related from the base-point (ReversePolar)
(defun test ( / ReversePolar p e r )
  
  (setq ReversePolar '(( p1 p2 ) (list p1 (angle p1 p2) (distance p1 p2))))
  
  (and 
    (setq p (getpoint "\nPick the center point: "))
    (setq e (car (entsel "\nPick the polygon: ")))
    (setq r 
      (mapcar '(lambda (x) (ReversePolar p x))
        (apply 'append (mapcar '(lambda (x) (if (= 10 (car x)) (list (cdr x)))) (entget e)))
      )
    )
  )
  r 
)  
>> ; Sample return:
( ; List of '(<OurBasePoint> <Angle> <Distance>)
  ((10342.2 -4014.9 0.0) 1.5708 200.0) ; (* 0.5 PI)
  ((10342.2 -4014.9 0.0) 2.82743 200.0) ; (* 0.9 PI)
  ((10342.2 -4014.9 0.0) 4.08407 200.0) ; (* 1.3 PI)
  ((10342.2 -4014.9 0.0) 5.34071 200.0) ; (* 1.7 PI)
  ((10342.2 -4014.9 0.0) 0.314159 200.0) ; (* 0.1 PI)
)
; Write our main function that would use the sample data from our previous list to create the star shape
(defun C:test ( / LWPoly p1 p2 a L ) ; always declare your variables (and income - for the taxes) 
  
  ; Lee Mac 
  (defun LWPoly (lst cls) ; helper subfunction to create the star shape as a LWPOLYLINE entity-type
    (entmakex
      (append 
        (list
          (cons 0 "LWPOLYLINE")
          (cons 100 "AcDbEntity")
          (cons 100 "AcDbPolyline")
          (cons 90 (length lst))
          (cons 70 cls)
        )
        (mapcar (function (lambda (p) (cons 10 p))) lst)
      )
    )
  )
  
  (and ; wrap the user-inputs within a conditional, that will stop immediately the whole function if the user interrupts at a prompt
    (setq p1 (getpoint "\nSpecify center point: "))
    (setq p2 (getpoint p1 "\nSpecify radius of the star: "))
    (setq a (+ (* 1.5 PI) (angle p1 p2))) ; angle that will adjust our star's direction to the specified vector of the points 'p1' and 'p2'
    (setq L (mapcar '(lambda (x) (polar p1 (+ a (* x PI)) (distance p1 p2))) '(0.5 0.9 1.3 1.7 0.1))) ; here we use the list of the 'scale-factors' for the angles for each star's vertex
    (setq L (apply '(lambda (a b c d e) (list a c e b d )) L)) ; our previous list defines a polygon shape, in order to draw a star-shape we need to relocate the vertex positions in our list
    (LWPoly L 1) ; create the star
  )
  (princ)
); defun 

 

Although @Lee Mac already wrote a complete program on creating a Star, the above is just my blindly coded practice attempt. 🍻

Link to comment
Share on other sites

Hmm, I think the OP is probably a spammer. Notice the off-topic link.

This technique is used quite often on the BricsCAD forum: Quote a message, copy-paste a message (sometimes from another forum), or add a vague comment, and hide some links.

 

Source of the original message:

https://stackoverflow.com/questions/23875094/stuck-in-autolisp-using-polar

Edited by Roy_043
  • Thanks 1
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...