• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Need help with Postscript programming

New Here ,
Dec 09, 2013 Dec 09, 2013

Copy link to clipboard

Copied

Hi everyone!

I have to draw a polygon with every diagonal, and the problem is i have limited time to do that. So i would like to ask your help.

Please check attached file:
http://s27.postimg.org/4mrzze11v/polygon_with_all_diagonal.png


Something like this.

Thats all what i could work out:

300 200 translate

/S 28 def

S S scale

4 S div setlinewidth

1 setlinecap

1 setlinejoin

/n 6 def

/r 6 def

newpath

r 0 moveto

1 1 n 1 sub

{

    /i exch def

    360 n div i mul cos r mul

    360 n div i mul sin r mul lineto

} for

closepath

stroke

I have no idea how to draw the diagonals.

Please help me!

Best regards:
Mihaly Varga

TOPICS
Programming

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 09, 2013 Dec 09, 2013

Copy link to clipboard

Copied

Mihaly,

One method is to store each x and y pair (corner) of the polygon in corresponding arrays

and then iterate through the array, drawing a line between each possible pair (corner).

This will draw diagonals and also redraw the perimeters as well.

Iteration example concept:

Primary loop begins at the first array position and iterates to the last cell.

Secondary loop begins at the first array position and iterates to the last cell.

Skip the line drawing when Primary and Secondary loop iterations are on the same x and y pair. Otherwise, Primary x and y cells are used to position the starting point of each line and

Secondary x and y cells are used to draw the line to.

End of Secondary loop

End of Primary loop

Not an elegant solution, but you asked for fast to implement.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2013 Dec 09, 2013

Copy link to clipboard

Copied

Dear Mr. Horton, thank you very much for your kind and fast reply!

Could you please demonstrate it ? I have big deficiency with the post script language and stack(?) languages basicly.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 09, 2013 Dec 09, 2013

Copy link to clipboard

Copied

I have inserted/appended verbose code to yours in order for you to better understand what is going on, for future reference. Other techniques could be employed that would shorten and simplify the code, but may be harder for you to follow/understand. The print command is used to output text to distiller for debugging/demonstration only.

%!PS-Adobe

%%Style: Verbose for example

/Iters 0 def    % Total number of points to keep track of in polygon

/Xarray 100 array def    % x position array with 100 cells

/Yarray 100 array def    % y position array with 100 cells

/Str 20 string def

/StoreXY {    %Gets X and Y position values for each point in the polygon

        Iters 99 lt{    %check number of array cells used doesn't overflow array size

                dup /Ypos exch def exch dup /Xpos exch def exch Xarray Iters Xpos put Yarray Iters Ypos put

                /Iters Iters 1 add def    %Increment position counter

        }if

}def

300 200 translate

/S 28 def

S S scale

4 S div setlinewidth

1 setlinecap

1 setlinejoin

/n 6 def

/r 6 def

newpath

r 0 StoreXY moveto

1 1 n 1 sub

{

    /i exch def

    360 n div i mul cos r mul

    360 n div i mul sin r mul StoreXY lineto

} for

closepath

stroke

(\n) print

(Iters=) print Iters Str cvs print (\n) print

/Iters Iters 1 sub def

.5 setgray    % changes gray value of lines to 50% for visibility.

1 S div setlinewidth    % changes line width so original strokes can still be seen.

Iters 2 gt{    % Check to see that at least 4 sets of points have been given.

    /Piter 0 def    %Primary loop setup

    0 1 Iters{(Piter=) print Piter Str cvs print (\n) print

            /Siter 0 def    %Secondary loop setup

            0 1 Iters{(     Siter=) print Siter Str cvs print (\n) print

                    Siter Piter ne{

                            Xarray Piter get /Xpos exch def

                            Yarray Piter get /Ypos exch def

                            Xarray Siter get /Xpos2 exch def

                            Yarray Siter get /Ypos2 exch def

                            (\050) print Xpos Str cvs print (,) print Ypos Str cvs print (\051 to \050) print Xpos2 Str cvs print (,) print Ypos2 Str cvs print (\051\n) print

                            newpath Xpos Ypos moveto Xpos2 Ypos2 lineto stroke

                    }if

                    /Siter Siter 1 add def    %Increment Secondary loop/array position counter

            }for

            /Piter Piter 1 add def    %Increment Primary loop/array position counter

    }for

}if

showpage

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 16, 2013 Dec 16, 2013

Copy link to clipboard

Copied

LATEST

Dear Sir,
Sorry for the late reply.

Many thanks for your answer, it was really helpful.

Best Regards,
Mihaly

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines