I want to write some postscript code that will shade a triangle. Here is some code I have cobbled together from a couple of google searches that produces a shaded square:
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 400 400
/square{
0 0 40 40 rectclip
<< /ShadingType 2
/ColorSpace [ /DeviceRGB ]
/Coords [ 0 0 40 40 ]
/Function <<
/FunctionType 2
/Domain [ 0 1 ]
/C0 [ 0.9 0.2 0.0 ]
/C1 [ 0.0 0.2 0.9 ]
/N 1
>>
>>
} bind def
save gsave
200 200 translate 45 rotate
square shfill
grestore restore
100 100 translate 300 rotate
square shfill showpage
Now, if I create a plain text file with this code and give it the filename shade.ps --- the important thing being the .ps --- and double click to open on a mac then the Apple Preview app will open the file and produce a picture with two copies of the shaded square. Great!
But now I want to shade a triangle in a similar way. My goal is to have a few lines of code in the spirit of the above that produces a shaded triangle.
Can anyone help? Is this even the right forum to be asking this question?
Note added 16 Jan 2023: I removed the redundant line of code mentioned in the answer below.
My goal is to draw a Penrose tiling with thin and thick rhombuses and then shade the rhombuses to help give the appearance that they are sloping rooves in the spirit of John Conway's city named Penrasia.
The part that describes the square is this line:
0 0 40 40 rectclip
To change it into a triangle, you'll need to write a function that creates a triangular path and then calls clip on it. This may be easier to understand if you look at an expanded version of rectclip which would look something like this:
/rectclip {
4 dict
/height exch def
/width exch def
/y exch def
/x exch def
x y moveto
width 0 rlineto
0 height rlineto
width neg 0 rlineto
closepath
clip
end
} def
For a triangle, you'll need to do something more like this, depending upon how you want to describe the triangle:
/triclip {
4 dict
/height exch def
/width exch def
/y exch def
/x exch def
x y moveto
width 0 rlineto
width 2 div neg height rlineto
closepath
clip
end
} def
And you'd call this function the same way that rectclip is called in the original.
0 0 40 40 triclip
A more idiomatic way to write the function in PostScript would use stack manipulation to avoid the need for a local dictionary.
/triclip { % x y width height . - ;sets clippath
4 2 roll % |- width height x y
moveto % |- width height
exch dup 0 % |- height width width 0
rlineto % |- height width
2 div neg exch % |- -width/2 height
rlineto % |-
closepath clip
} def
With practice you can start to "read" the stack manipulation part and treat the code instead as several statements and break the lines differently:
/triclip { % x y width height . - ;sets clippath
4 2 roll moveto % width height
exch dup 0 rlineto % height width
2 div neg exch rlineto % -
closepath clip
} def
The important part is the calls to moveto, lineto, closepath, and clip, and the rest is just the "syntax" of PostScript.
How can I have the text size shrink automatically depending on character length in Postscipt? Or to have the text resize in order to fit a fixed area?
I think it's simpler to make a 1-pt font, measure,and then scale up. Something like (untested)
/showwid { % string width /font-name
gsave
1 selectfont
1 index stringwidth pop div % width/stringwidth
currentfont scalefont setfont
show
grestore
} def
This code doesn't account for height at all.
The way i do it is:
get string width
divide area width by string width
multiply current font size by that
then use this as new font size and show
So, the code is something like:
%some bogus vars to make it understandable
/myfont /Helvetica def
/mysz 10 def
/mywidth 100 def
/mystr (Hello PS world!) def
%supposedly you'd be in the middle of something
%therefore we would have the font already selected
myfont mysz selectfont
50 50 moveto
myfont mywidth mystr stringwidth pop div mysz mul selectfont
mystr show
% and for good measure, a line showing the size
50 49 moveto
mywidth 0 rlineto stroke
This is quite a tough challenge I have with my code. First of all the code I am putting here is not runnable because I am using an Excel sheet (but I am happy to email it if people want to try using my code).
What I have is an Excel sheet with data on cross-sectional fibres in a microscopic image I took. The information is basically: location of the section, area, angle of rotation.
From that I calculate the angle of orientation Phi, and Gamma. After that I use the scatter function to plot a dot of different colors for each Phi angle value. I use a constant color for a range of 10 degrees. Which gives me a picture like this:
Now my aim to is calculate the area of each homogeneous region. So I look for a way to plot let's say all the dots within the -10 +10 region (I'm doing 20 degrees for now, but will do 10 after). I used a look and I get a picture like this:
The white corresponds where the dots are within the range I selected. After that I use the toolbox in MATLAB to convert each dot into a pixel. So I'll get a black background with loads of white pixels, then I use imdilate to make circles, fill holes and isolate each region with a specific color. Finally I use the functions boundary and patch, to create each boundary and fill them with a color. And I get a picture like this:
Which is what I want and I can get the area of each region and the total area (I used a threshold to discard the small areas). Then I run the code several time for each region, and I use imfuse to put them back together and see what it looks like.
THE PROBLEM is, they overlap quite a lot, and that is because there are some errors in my data, and therefore some blue dots will be in the red and so on.
So I want to run the code once, then when I rerun it with another range, it does the same thing but doesn't take into account value when there's already something plotted before.
I tried to do that by, after running once, saving the matrix bw4 and adding a condition when plotting the black and white pic, by saying if Phi is in my range AND there no white here then you can put white, otherwise it's black. But it doesn't seem to work.
I understand this is quite a complicated thing to explain, but I would appreciate any ideas, and open to chat via email or otherwise. I am putting the full code now, and I can send you my Excel sheet if you want to run it on your computer and see for yourself.
clearvars -except data colheaders bw4
close all
clc
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% CHANGE DATA FOR EACH SAMPLE %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
cd 'C:\Users\dkarta\Desktop\Sample 12\12.6'
data=xlsread('Sample12_6res.xlsx');
cd 'C:\Users\dkarta\Documents\MATLAB'
%data=Sample121res; % Data name
imax=length(data); % Numbers of rows in data sheet
y=11900; % Number of pixels in the y on image j
%%
data(:,15)=data(:,9)*pi/180; % Convers Column 9 (angle of rotation) in rads
data(:,16)=y-data(:,6); % Reset the Y coordinate axis to bottom left
delta = 0 : 0.01 : 2*pi; % Angle in paramteric equations
theta=45*pi/180; % Sample cutting angle in rads
%AA=[data(:,5)' data(:,16)' phi']
% Define colors
beta=acos(data(1:imax,8)./data(1:imax,7));%./acos(0);
phi=atan(sin(beta).*cos(data(1:imax,15))./(sin(theta)*sin(beta).*sin(data(1:imax,15))+cos(theta)*cos(beta)))/(pi/2);
phi2=phi/2+1/2; % Scales in plane angle phi between 0 and 1
gamma=atan((cos(theta)*sin(beta).*sin(data(1:imax,15))-sin(theta)*cos(beta))./...
(sin(theta)*sin(beta).*sin(data(1:imax,15))+cos(theta)*cos(beta)))/(pi/2);
gamma2=gamma+1/2; % Scales out of plane angle gamma between 0 and 1
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% MESHGRID AND COLOURMAP %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
x1=data(1:imax,5);
y1=data(1:imax,16);
z1=phi*90;
z2=gamma*90;
n=300;
%Create regular grid across data space
[X,Y] = meshgrid(linspace(min(x1),max(x1),n), linspace(min(y1),max(y1),n));
% Creating a colormap with 10 degree constant colors
map4=[0 0 1;0 1/3 1;0 2/3 1; 0 1 1;0 1 2/3;0 1 1/3;0 1 0;1/3 1 0;2/3 1 0;1 1 0;1 0.75 0;1 0.5 0;1 0.25 0;1 0 0;0.75 0 0.25;0.5 0 0.5;0.25 0 0.75; 0 0 1];
Colormap4=colormap(map4);
h=colorbar;
caxis([-90 90])
set(h, 'YTick', [-90:10:90])
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% PLOT USING SCATTER - ISOLATE SOME REGIONS %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
a=-10; % Lower boundary for angle interval
b=10; % Upper boundary for angle interval
c=z1>a & z1 < b;
c=c.*1;
%j=1;
y1=(y1-min(y1)+1);
y2=max(y1)-y1+1;
[X1,Y1]=meshgrid(1:500,1:500);
griddata(x1,y2,c,X1,Y1);
clear c1
for i=1:imax
if z1(i)< b && z1(i)> a %&& bw4(round(y1(i)),round(x1(i))) == 0
c(i) = 1;
c1(round(y2(i)),round(x1(i)))=1;
else
c(i)= 0;
c1(round(y2(i)),round(x1(i)))=0;
end
end
C=[c c c];
%c(find(c==0)) = NaN;
%contourf(X,Y,griddata(x1,y1,c,X,Y),100,'EdgeColor', 'None')
figure(1), scatter(x1,y1,3,z1,'filled');
axis equal
axis ([0 8000 0 12000])
axis off
figure(2), scatter(x1,y1,3,C,'filled');
axis equal
axis ([0 8000 0 12000])
axis off
se=strel('disk',50,8);
bw2=imdilate(c1,se);
bw4=bwlabel(bw2);
bw3=imfill(bw4,'holes');
max(bw4(:));
figure(3),imshow(c1,'InitialMagnification', 10);
figure(4), imshow(bw2,'InitialMagnification', 10);
figure(5), imshow(bw3,'InitialMagnification', 10);
figure(6),imshow(label2rgb(bw4),'InitialMagnification', 10);
k=ones(max(bw4(:)),1);
clear bw5
for i=1:length(x1)
if bw3(round(y2(i)),round(x1(i))) ~= 0
m=bw3(round(y2(i)),round(x1(i)));
bw5{m}(k(m),1)=x1(i); bw5{m}(k(m),2)=y2(i);
k(m)=k(m)+1;
end
end
figure(7), imshow(~c1,'InitialMagnification', 10);
hold on
for i=1:max(bw4(:))
%scatter(bw5{i}(:,1),bw5{i}(:,2))
j = boundary(bw5{i}(:,1),bw5{i}(:,2),0.5);
%poly=convhull(bw5{i}(:,1),bw5{i}(:,2));
%plot(bw5{i}(poly,1),bw5{i}(poly,2)), title('convhull')
if polyarea(bw5{i}(j,1),bw5{i}(j,2))> 10^5;
patch(bw5{i}(j,1),bw5{i}(j,2),'r'), title('boundary')
indexminy(i)=find(min(bw5{i}(:,2)) == bw5{i}(:,2));
indexminx(i)=find(min(bw5{i}(:,1)) == bw5{i}(:,1));
indexmaxy(i)=find(max(bw5{i}(:,2)) == bw5{i}(:,2));
indexmaxx(i)=find(max(bw5{i}(:,1)) == bw5{i}(:,1));
%xmin = bw5{i}(indexminx); xmax = bw5{i}(indexmaxx);
%ymin = bw5{i}(indexminy); ymax = bw5{i}(indexmaxy);
str=[(indexminx(i)+indexmaxx(i))/2,(indexminy(i)+indexmaxy(i))/2,'Region no.',num2str(i)];
text((min(x1(i))+max(x1(i)))/2,(min(y1(i))+max(y1(i)))/2,str)
polya(i)=polyarea(bw5{i}(j,1),bw5{i}(j,2));
end
end
spolya=sum(polya(:))
print -dpng -r500 B
Just to show you more pictures of when I fuse several of them:
And when I fuse:
As you can see they overlap, which I don't want, so I want each image that I create to 'know' what I'm doing on the previous runs so that it doesn't overlap. I want to get the percentage area of each region and if they overlap I cannot use the actual total area of my sample and the results are wrong.
I dont have my matlab working but here is what you need to do.
For the first run make an array of zeros equal to your image size
already_taken = zeros(size(bw3));
Then on each run, you can fill up the regions taken by this iteration. So at the end of your code, where you save the output to a png, read it back into something like
this_png = rgb2gray(imread(current_png_path))>threshold;
Convert this into a logical array by doing some thresholding and add these values into already taken. So at the end of the code, do a
already_taken = already_taken | this_png; % You might need to check if you need a single | or a double ||
So now you have an image of already taken pixels, ill bake sure I don't allow bw2 to take these values at first place
bw2(already_taken) = 0;
And at the end of the code when I want to write my png, my smart boundary creation might again have entered into already_taken area so there again I'll have to put some check. As far as I understand, this boundary is being created based upon your bw5. So where ever you fill this matrix, try putting a similar check as I did above for bw2.
I hope this helps.
Is there a way via PostScript to add a string such that is will be truncated by "..." so as not to exceed a certain width?
I've looking at some old report generation code and would like add this feature. In the existing reports, values that are too long are visually overwriting other data.
The reason I'm trying to do this at the PS level is that in the existing code I don't see anything that could calculate any kind of accurate width metric.
I've yet to write any Postscript, so maybe this is trivial. (?)
Per comment below: Yes, localization will an issue. So I guess a user defined "ellipsis" string makes sense.
Here is some example output that shows how strings are currently printed:
% Change font style and/or size
/Times-Roman-ISOLatin1 findfont 12 scalefont setfont
219 234 moveto (AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA_) show
Can this be modified to ellipsize things?
Well you can do something like this (replace the char in front of concatstrings with your ellipsis):
/concatstrings % (a) (b) -> (ab)
{ exch dup length
2 index length add string
dup dup 4 2 roll copy length
4 -1 roll putinterval
} bind def
/ellipsis_show {
1 dict begin
/width_t exch def
{dup stringwidth pop width_t lt {exit} if dup length 1 sub 0 exch getinterval} loop
(_) concatstrings
show
end
}def
% Change font style and/or size
/Times-Roman-ISOLatin1 findfont 12 scalefont setfont
0 0 moveto (foobar barfoo foofoo barbar) 100.0 ellipsis_show
concatstrings copied from: http://en.wikibooks.org/wiki/PostScript_FAQ#How_to_concatenate_strings.3F
The simple answer is 'no'. A longer answer is that, since PostScript is a programming language, you can do this, but it will require some knowledge of PostScript, and some work, it certainly is not trivial.
You can redefine the various operators which draw text on the output, there are quite a few; show, ashow, cshow, kshow, xshow, yshow, xyshow, widthshow, awidthshow, and glyphshow. You could define modified versions of these which determine (using stringwidth and the parameters used by the various operators) the width of thefinal printed text. Probably you would want to calculate this glyph by glyph and terminate with your ellipsis when the value exceeds some threshold. (NB not all fonts will contain an ellipsis glyph, and its encoded position may vary).
However, given that you are working with existing code, there is most probably already a function defined to draw text and it probably only uses a subset of the possible operators. You would probably be better advised to modify that.
I was wondering how can I rotate a graphic, say a rectangular by certain angle in post script.
Or at least is there any way to draw a very bold ! like, with an angle !?
I have list of sentence around a circle, so each or in 1 direction, and now, I would like to put each in a rectangular and make hyperlink for them.
The power of Postscript is its ruthless pursuit of the ideal of "delayed binding". The implementation of rotations is no exception. It works by making use of a more general tool, the Affine Transformation Matrix.
You can rotate both text and graphics (because text IS graphics) because all user specified coordinates are first multiplied through this matrix to produce device coordinates.
To perform all the necessary tricks (scaling, rotation, shear, translation), we first have to extend the 2d points to 3d points on the plane z=1 (don't ask me why; read Bill Casselman's Mathematical Illustrations or the Adobe Blue Book for more).
[ x [ a b 0
y * c d 0 = [ x' y' 1 ] = [ ax+cy+e bx+dy+f 1 ]
1 ] e f 1 ]
Since the 3rd column of the matrix is always [ 0 0 1 ] it is omitted from the external representation, and the matrix is described in postscript as:
[ a b c d e f ]
So when you use a coordinate pair for, say, a moveto operator, moveto first transforms it to device coordinates, x' = ax+by+e, y' = cx+dy+f, before adding a <</move [x' y']>> element to the current path.
Change the matrix: change the "meaning" of user coordinates.
The identity matrix is this:
[ 1 0 0 1 0 0 ] % x' = x, y' = y
To scale, replace the 1s with x and y scaling factors:
[ Sx 0 0 Sy 0 0 ] % x' = Sx*x, y' = Sy*y
To translate, replace e and f with the x and y translation offsets:
[ 1 0 0 1 Tx Ty ] % x' = x+Tx, y' = y+Ty
To rotate, replace a,b,c,d with sin and cos scaling and shearing factors:
[ cosW sinW -sinW cosW 0 0 ] % x' = x*cosW-y*sinW, y' = x*sinW+y*cosW, where W is angle(degrees) from x-axis
You "install" this matrix with concat which takes the Current Tranformation Matrix (CTM), multiplies it by your new matrix, and uses the product as the new CTM. So translate, rotate, and scale are just "convenience functions" which could be implemented like this:
/translate { [ 1 0 0 1 7 -2 roll ] concat } def
/scale { [ 3 1 roll 0 0 3 -1 roll 0 0 ] concat } def
/rotate { [ exch dup cos exch sin dup neg 2 index 0 0 ] concat } def
Since the CTM is part of the graphics state, you can use the graphics state stack to manipulate your transformations in a hierarchical manner:
/box { % x y w h %create a path in the shape of a box w*h with lower left corner at x,y
4 2 roll moveto
exch dup 3 1 roll
0 rlineto
0 exch rlineto
neg 0 rlineto
closepath
} def
/Courier 10 selectfont
100 100 100 100 box stroke % draw an oriented box
120 120 moveto (inside) show
gsave
150 150 translate % make the center of the box the new 0,0 point
45 rotate % rotate CCW 45 degrees
0 0 100 100 box stroke % a rotated, shifted box
20 20 moveto (inside) show
grestore
100 200 100 100 box stroke % another box, just north of the first, in the original coordinte system
120 220 moveto (inside) show
This produces the following image:
(source: googlecode.com)
I haven't used PostScript for a long time, but as I remember you could just use "rotate".
% do some steps
% ...
% ...
20 20 moveto % go to new position
30 /Times-Roman SetFont % select active font
45 rotate % set direction to diagonal
(Something)show % print text "Something"
showpage % show it all
cheers Kris
Postscript renders graphics in a given context - and it is this context that can be rotated (or scaled/translated) before drawing. Therefore any element on the image can be transformed as one wishes, all you have to do is to perform the necessary context transforms beforehand.
However, unfortunately, while I can give you an idea of it in this writing, i is a fundamental concept of Postscript, and you won't be able to do any real work in it without understanding that first. I suggest reading a brief tutorial such as the one in http://paulbourke.net/dataformats/postscript/ .
So, the "rotate" name is a function that does rotate the graphcis context - you use rotate, before drawing anything you want (rendering text also being "drawing" in this case).
%!PS
(Helvetica) findfont 12 scalefont setfont %select a font to use
300 300 translate % sets the orign at 300,300 points from the bottom left of page
/start 5 def % creates variable for keeping track of horizontal position of text
36 % pushes number of repeats on the stack
{
start 5 moveto % places cursor on the starting position
(postscript) show % renders the string in the starting position, within the current context
/start start 3 add def % increases the value on the variable
10 rotate % rotates the context 10 degrees clockwise (around the 300,300 new origin)
} repeat
showpage % renders whole page