Page 1 of 1

PostScript atan error

PostPosted: Wed Jul 12, 2017 6:08 am
by TedWalther
Hi. On macOS Sierra, I am using postscript.lsp (newlisp 10.7.0)

In the function drawto, there is a line
Code: Select all
  newy ypos sub newx xpos sub atan neg 90 add /orient exch def


So I generate my postscript instructions with ps:render. Then it comes time to convert the postscript file to a PDF so I can view it, and sometimes it gives an error, other times it doesn't. I make minor tweaks to my output code and the error goes away. But when the error is there, it stays until I change the code, but I never figured out what change I made to avoid the error.

But anyways, I can read postscript a little, but that line doesn't make sense to me. Why is it in there? When I hand edit the postscript file to comment out that line, I can create the PDF without problem.

Sample code that shows the error:

Code: Select all
(define pi (mul 4 (atan 1)))
(define (rad d) (mul d (div pi 180)))
(define (deg r) (mul r (div 180 pi)))

(module "postscript.lsp")

;; I work in inches as my base unit (72 points per inch), but scale things down
;; by scalefactor so they fit on a regular sheet of paper.

(define scalefactor 4)
(ps:scale (div 72 scalefactor) (div 72 scalefactor))

(define ¼ 0.25)
(define ½ 0.5)
(define origx (mul ½ 8.5 scalefactor))
(define origy (mul ½ 11 scalefactor))
(define my-goto (lambda (x y) (ps:goto (add x origx) (add y origy))))
(define my-drawto (lambda (x y) (ps:drawto (add x origx) (add y origy))))

;; https://math.stackexchange.com/questions/76099/polar-form-of-a-superellipse
;; answered Oct 26 2011 at 17:03 by Ross Millikan
;; polar coordinate form of a superellipse
;; r = ab/(|b cos theta|^n + |a sin theta|^n)^(1/n)
;; a is half the height
;; b is half the width
;; n is the exponent of the superellipse
(define super-ellipse-radius (lambda (a b n theta)
  (div (mul a b)
       (pow (add (pow (abs (mul b (cos theta))) n)
                 (pow (abs (mul a (sin theta))) n))
            (div n)))))

(define draw-super-ellipse (lambda (a b n)
  (letn (anglestep (rad 0.1)
         ab (mul a b)
         startr (super-ellipse-radius a b n 0)
         startx (mul startr (cos 0))
         starty (mul startr (sin 0)))
    (my-goto startx starty)
    (for (theta anglestep (mul 2 pi) anglestep)
      (let (r (super-ellipse-radius a b n theta))
        (my-drawto (mul r (cos theta)) (mul r (sin theta)))))
    (my-drawto startx starty))))

(draw-super-ellipse (mul 7 (sqrt 2)) 7 2.718)

(ps:render "standing-desk2.ps")
(! "pstopdf standing-desk2.ps")

(exit)



And here is the error when trying to open the file (macOS auto-converts it to PDF, and pstopdf also has this error:

Code: Select all
%%[ Error: undefinedresult; OffendingCommand: atan; ErrorInfo: CharOffsets %%[ Flushing: rest of job (to end-of-file) will be ignored ]%%
%%[ Warning: PostScript error. No PDF file produced. ] %%
pstopdf failed on file standing-desk2.ps with error code -31000

Re: PostScript atan error

PostPosted: Wed Jul 12, 2017 6:10 am
by TedWalther
I think I found the error. The "atan" is bombing out when drawto is called from a point, to the identical point. So I changed my code a little to guarantee drawto isn't called when the start and end points are identical. Now it works.

Re: PostScript atan error

PostPosted: Fri Sep 07, 2018 9:43 am
by georgecraft
Hi! I am glad you were able to include and outline all these features. They are necessary to consider for the beginners, like I am!