plot.c
Go to the documentation of this file.
1 #include "plot.h"
2 
3 #include "defines.h"
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 
23 #define CM2POINTS 450.0 /*number of points of each cm in Xfig*/
24 
30 #define POINTS2CM (1.0/CM2POINTS)
31 
41 #define USER2XFIG(x,y,p) (signed int)(((p)->offset_x+(x))*CM2POINTS),\
42  (signed int)(((p)->offset_y-(y))*CM2POINTS)
43 
52 #define PRINTXFIGPREAMBLE(p) fprintf((p)->f," %d %d %d %d %d -1 %d %f ",\
53  (p)->line_style,(p)->line_width,(p)->pen_color,\
54  (p)->fill_color,(p)->depth,(p)->fill_intensity,\
55  (((p)->line_style==SOLID_LINE)?0.0:4.0))
56 
57 /*
58  * Inits the plot structure and start the Xfig file (writes the header)
59  */
60 void InitPlot(const char *name,Tplot *p)
61 {
62 
63  p->f=fopen(name,"w");
64  if (!p->f)
65  {
66  fprintf(stderr,"File %s can not be created in function InitPlot\n",name);
67  exit(1);
68  }
69 
70  p->offset_x=0.0;
71  p->offset_y=0.0;
72 
73  p->line_width=1;
75 
76  p->pen_color=-1; /*default*/
77  p->fill_color=-1; /*default*/
78  p->fill_intensity=-1; /* no fill (20 = full intensity)*/
79  p->depth=50; /*standard depth*/
80 
81  /*print de preamble for the xfig file*/
82  fprintf(p->f,"#FIG 3.2\n");
83  fprintf(p->f,"Landscape\n");
84  fprintf(p->f,"Center\n");
85  fprintf(p->f,"Metric\n");
86  fprintf(p->f,"A4\n");
87  fprintf(p->f,"100.00\n");
88  fprintf(p->f,"Single\n");
89  fprintf(p->f,"-2\n");
90  fprintf(p->f,"1200 2\n");
91 }
92 
93 /*
94  * Changhes the line width for the object that are drawn after calling this
95  * function
96  */
97 void SetLineWidth(unsigned int width,Tplot *p)
98 {
99  p->line_width=width;
100 }
101 
102 /*
103  * Sets a new line style for the new objects
104  */
105 void SetLineStyle(unsigned int style,Tplot *p)
106 {
107  p->line_style=style;
108 }
109 
110 /*
111  * Defines a new coordinate frame for the objects. The offsets are defined from
112  * the top left corner of the sheet (and positive towards the bottom right corner)
113  * This origin modification is useful to avoid the picture to be drawn out of the
114  * sheet.
115  */
116 void SetOrigin(double offset_x,double offset_y,Tplot *p)
117 {
118  p->offset_x=offset_x;
119  p->offset_y=offset_y;
120 }
121 
122 void SetPenColor(int color,Tplot *p)
123 {
124  if ((color>-2)&&(color<32))
125  p->pen_color=color;
126 }
127 
128 void SetFillColor(int color,Tplot *p)
129 {
130  if ((color>-2)&&(color<32))
131  p->fill_color=color;
132 }
133 
134 void SetFillIntensity(unsigned int intensity,Tplot *p)
135 {
136  if (intensity<21)
137  p->fill_intensity=intensity;
138 }
139 
140 
141 void SetDepth(unsigned int depth,Tplot *p)
142 {
143  p->depth=depth;
144 }
145 
146 /*
147  * Draws a circle at position cx, cy and with radius r
148  */
149 void PlotCircle(double cx,double cy,double r,Tplot *p)
150 {
151  fprintf(p->f,"1 3");
152 
154 
155  fprintf(p->f,"1 0.0000 %d %d %d %d %d %d %d %d\n",
156  USER2XFIG(cx,cy,p),
157  (signed int)(r*CM2POINTS),(signed int)(r*CM2POINTS), /*1 cm radius*/
158  USER2XFIG(cx,cy,p),
159  USER2XFIG(-r,cy,p));
160 }
161 
162 /*
163  * Draws a rectangle
164  */
165 void PlotRectangle(double x_left,double y_sup,double x_right,double y_inf,Tplot *p)
166 {
167  fprintf(p->f,"2 2");
168 
170 
171  fprintf(p->f,"0 0 -1 0 0 5\n");
172 
173  fprintf(p->f," ");
174 
175  /*add the five (first repeated) extremes points of the rectangle*/
176 
177  fprintf(p->f,"%d %d ",USER2XFIG(x_left -POINTS2CM,y_inf-POINTS2CM,p));
178  fprintf(p->f,"%d %d ",USER2XFIG(x_right+POINTS2CM,y_inf-POINTS2CM,p));
179  fprintf(p->f,"%d %d ",USER2XFIG(x_right+POINTS2CM,y_sup+POINTS2CM,p));
180  fprintf(p->f,"%d %d ",USER2XFIG(x_left -POINTS2CM,y_sup+POINTS2CM,p));
181  fprintf(p->f,"%d %d\n",USER2XFIG(x_left -POINTS2CM,y_inf-POINTS2CM,p));
182 
183 /*
184  fprintf(p->f,"%d %d ",USER2XFIG(x_left ,y_inf,p));
185  fprintf(p->f,"%d %d ",USER2XFIG(x_right,y_inf,p));
186  fprintf(p->f,"%d %d ",USER2XFIG(x_right,y_sup,p));
187  fprintf(p->f,"%d %d ",USER2XFIG(x_left ,y_sup,p));
188  fprintf(p->f,"%d %d\n",USER2XFIG(x_left ,y_inf,p));
189 */
190 
191  fflush(p->f);
192 }
193 
194 void PlotTriangle(double x1,double y1,double x2,double y2,double x3,double y3,Tplot *p)
195 {
196 
197  fprintf(p->f,"2 3");
198 
200 
201  fprintf(p->f,"0 0 -1 0 0 4\n");
202 
203  fprintf(p->f," ");
204  fprintf(p->f,"%d %d ",USER2XFIG(x1,y1,p));
205  fprintf(p->f,"%d %d ",USER2XFIG(x2,y2,p));
206  fprintf(p->f,"%d %d ",USER2XFIG(x3,y3,p));
207  fprintf(p->f,"%d %d\n",USER2XFIG(x1,y1,p));
208 
209  fflush(p->f);
210 }
211 
212 /*
213  * Draws a line
214  */
215 void PlotLine(double x0,double y0,double x1,double y1,Tplot *p)
216 {
217  fprintf(p->f,"2 1");
218 
220 
221  fprintf(p->f," 0 0 -1 0 0 2 \n %d %d %d %d\n",USER2XFIG(x0,y0,p),USER2XFIG(x1,y1,p));
222 }
223 
224 /*
225  * Draws an arrow with a pre-defined arrow style
226  */
227 void PlotArrow(double x0,double y0,double x1,double y1,Tplot *p)
228 {
229  fprintf(p->f,"2 1");
230 
232 
233  fprintf(p->f," 0 0 -1 1 0 2 \n 1 1 5.00 60.00 120.00 \n %d %d %d %d\n",USER2XFIG(x0,y0,p),USER2XFIG(x1,y1,p));
234 }
235 
236 /*
237  * Writes a string at a given position (with predefined font and size (14 points))
238  */
239 void PlotText(double x,double y,const char *text,Tplot *p)
240 {
241  fprintf(p->f,"4 0 0 50 0 0 14 0.0000 4 150 150 %d %d %s\\001\n",USER2XFIG(x,y,p),text);
242 }
243 
244 /*
245  * Draw X.Y axis naming axis as 'nx' and 'ny'
246  */
247 void PlotAxis(const char *nx,double min_x,double max_x,
248  const char *ny,double min_y,double max_y,
249  double step,
250  Tplot *p)
251 {
252  signed int i;
253 
254  /*print the axis*/
256 
257  PlotArrow(min_x,0.0,max_x,0.0,p);
258  PlotText(max_x+0.5,0.0,nx,p);
259 
260  for(i=(signed int)(min_x/step);i<(signed int)(max_x/step);i++)
261  PlotLine(1.0*i*step,-0.1*step,1.0*i*step,0.1*step,p);
262 
263  PlotArrow(0.0,min_y,0.0,max_y,p);
264  PlotText(0.5,max_y-0.5,ny,p);
265 
266  for(i=(signed int)(min_y/step);i<(signed int)(max_y/step);i++)
267  PlotLine(-0.1*step,1.0*i*step,0.1*step,1.0*i*step,p);
268 
269 }
270 
271 /*
272  * Finishes the plot. It just draws the coordinate axis (the last defined ones) and
273  * closes the file.
274  */
275 void ClosePlot(Tplot *p)
276 {
277  if (p->f!=stdout)
278  fclose(p->f);
279 }
unsigned int line_width
Definition: plot.h:56
void ClosePlot(Tplot *p)
Destructor.
Definition: plot.c:275
void PlotRectangle(double x_left, double y_sup, double x_right, double y_inf, Tplot *p)
Plots a rectangle.
Definition: plot.c:165
int pen_color
Definition: plot.h:58
A 2D plot.
Definition: plot.h:51
int fill_intensity
Definition: plot.h:59
void SetFillColor(int color, Tplot *p)
Changes the current fill color.
Definition: plot.c:128
void SetPenColor(int color, Tplot *p)
Changes the current pen color.
Definition: plot.c:122
unsigned int line_style
Definition: plot.h:55
void SetOrigin(double offset_x, double offset_y, Tplot *p)
Changes the current origin of the plot.
Definition: plot.c:116
void SetFillIntensity(unsigned int intensity, Tplot *p)
Changes the current fill intensity.
Definition: plot.c:134
double offset_y
Definition: plot.h:54
void PlotArrow(double x0, double y0, double x1, double y1, Tplot *p)
Plots a segment with an arrow.
Definition: plot.c:227
Definitions of constants and macros used in several parts of the cuik library.
void SetLineWidth(unsigned int width, Tplot *p)
Changes the current line width.
Definition: plot.c:97
void PlotCircle(double cx, double cy, double r, Tplot *p)
Plots a circle.
Definition: plot.c:149
#define USER2XFIG(x, y, p)
Converts a x-y point in R^2 into a xfig point.
Definition: plot.c:41
Module to generate 2d plots.
#define POINTS2CM
Scale factor between xfig units and centimiters.
Definition: plot.c:30
#define SOLID_LINE
Solid line in 2d.
Definition: plot.h:27
void PlotTriangle(double x1, double y1, double x2, double y2, double x3, double y3, Tplot *p)
Plots a triangle.
Definition: plot.c:194
void InitPlot(const char *name, Tplot *p)
Constructor.
Definition: plot.c:60
#define CM2POINTS
Scale factor between centimiters and xfig units.
Definition: plot.c:23
double offset_x
Definition: plot.h:53
void PlotText(double x, double y, const char *text, Tplot *p)
Plots a text.
Definition: plot.c:239
void PlotLine(double x0, double y0, double x1, double y1, Tplot *p)
Plots a segment.
Definition: plot.c:215
void SetDepth(unsigned int depth, Tplot *p)
Changes the current plot depth.
Definition: plot.c:141
void SetLineStyle(unsigned int style, Tplot *p)
Changes the current line stype.
Definition: plot.c:105
#define PRINTXFIGPREAMBLE(p)
Writes a preamble common to all xfig elements.
Definition: plot.c:52
FILE * f
Definition: plot.h:52
void PlotAxis(const char *nx, double min_x, double max_x, const char *ny, double min_y, double max_y, double step, Tplot *p)
Plots a X-Y axis.
Definition: plot.c:247
int depth
Definition: plot.h:60
int fill_color
Definition: plot.h:57