libxfig.cpp
来自「此文件包含了在linux下实现tpr-tree索引的源代码」· C++ 代码 · 共 600 行
CPP
600 行
//--------------------------------------------------------------- // libxfig.cpp// -----------// libxfig drawing library by Kazuo AMANO.//// Slightly modified by Simonas Saltenis, Aalborg University, 2001// For an original version and the documentation go to // http://www.cc.gunma-u.ac.jp/~amano/kamano.html // /*{\hrulefill}*{\qquad{\bf libxfig.c}\quad version 2.1}{\qquad Kazuo AMANO\quad (kamano@po.iijnet.or.jp)}{\nullThis small library just consists of the followingone global variable and eight drawing functions:\item{}{\tt FILE\ xfig\_out,}\item{}{\tt void\ xfig\_color(double\ x),}\item{}{\tt void\ xfig\_depth(double\ x),}\item{}{\tt void\ xfig\_linewidth(double\ x),}\item{}{\tt void\ xfig\_linestyle(double\ x),}\item{}{\tt void\ xfig\_fillstyle(double\ x),}\item{}{\tt void\ xfig\_2d\_polyline(double *ptr),}\item{}{\tt void\ xfig\_3d\_viewpoint(double *ptr),}\item{}{\tt void\ xfig\_3d\_polyline(double *ptr).}\noindentWe shall briefly explain how to use the variable {\tt xfig\_out}and the above xfig functions.\item{$\bullet$}{\tt FILE\ xfig\_out}\item{}{\tt xfig\_out} is a global variable of {\tt FILE} type.The default value of {\tt xfig\_out} is {\tt stdout}.If you want to output date to a given file, say {\sl example.fig\/},you have only to put\itemitem{}{\tt xfig\_out=fopen("example.fig","w");}\item{}After this command, the following xfig functions simply output datato {\sl example.fig\/}.\item{$\bullet$}{\tt void\ xfig\_color(double\ x)}\item{}For $0\le x\le 1$,this function determines {\tt color}.Here$x=0, {1\over7}, {2\over7}, {3\over7}, {4\over7},{ 5\over7}, {6\over7}, 1$correspond to{\tt white, yellow, magenta, red, cyan, green, blue, black}respectively.\item{$\bullet$}{\tt void\ xfig\_depth(double\ x)}\item{}For $0\le x\le 1$,this function defines {\tt depth}.In fact, larger value means object is deeper than objectswith smaller depth.\item{$\bullet$}{\tt void\ xfig\_linewidth(double\ x)}\item{}For $0\le x\le 1$,this function defines {\tt linewidth}.The line thickness is strictly increasing with respect to $x$.\item{$\bullet$}{\tt void\ xfig\_linestyle(double\ x)}\item{}For $0\le x\le 1$,this function defines {\tt linestyle}.For example,\itemitem{}$x=1.0\ \Rightarrow\ {\rm solid\ line}$\itemitem{}$x=0.5\ \Rightarrow\ {\rm dotted\ line}$\itemitem{}$x=0.0\ \Rightarrow\ {\rm invisible\ line}$\item{$\bullet$}{\tt void\ xfig\_fillstyle(double\ x)}\item{}This function defines {\tt fillstyle} for $0\le x\le 1$.For instance,\itemitem{}$x=1.0\phantom{0}\ \Rightarrow\ {\rm black}$\itemitem{}$x=0.75\ \Rightarrow\ {\rm dark\ grey}$\itemitem{}$x=0.5\phantom{0}\ \Rightarrow\ {\rm grey}$\itemitem{}$x=0.25\ \Rightarrow\ {\rm light\ grey}$\itemitem{}$x=0.0\phantom{0}\ \Rightarrow\ {\rm white}$\item{$\bullet$}{\tt void\ xfig\_2d\_polyline(double *ptr)}\item{}For given points\itemitem{}{\tt (*ptr,*(ptr+1)),\ (*(ptr+2),*(ptr+3)),}$\ \cdots ,\ $ {\tt (*(ptr+2n-2),*(ptr+2n-1))}\item{}of ${\bf R}^2$, function {\tt xfig\_2d\_polyline(double *ptr)} drawsa 2-dimensional polyline which passes through those given points.Here {\tt\ *(ptr+i)}$\in [0,1]\quad (0\le i\le 2n-1)\ $and the last datum {\tt *(ptr+2n)}$\not\in [0,1]$ denotesa terminator of data sequence.So, {\tt xfig\_2d\_polyline()} draws a 2-dimensional polylinein a rectangle $\,[0,1]\times [0,1]\,$ of ${\bf R}^2$.\item{$\bullet$}{\tt void\ xfig\_3d\_viewpoint(double *ptr)}\item{}For a given point\itemitem{}{\tt (*ptr,*(ptr+1),*(ptr+2))}\item{}of ${\bf R}^3$, function {\tt xfig\_3d\_viewpoint(double *ptr)} movesyour eyes from the default position(2.310453, 1.262206, 1.438277)to{\tt (*ptr,*(ptr+1),*(ptr+2))}where you always look at the center of a unit cube$[0,1]\times[0,1]\times[0,1]$.\item{$\bullet$}{\tt void\ xfig\_3d\_polyline(double *ptr)}\item{}For given points\itemitem{}{\tt (*ptr,*(ptr+1),*(ptr+2))}$\ \cdots ,\ $ {\tt (*(ptr+3m-3),*(ptr+3m-2),*(ptr+3m-1))}\item{}of ${\bf R}^3$, function {\tt xfig\_3d\_polyline(double *ptr)} drawsa 3-dimensional polyline which passes through those given points.Here {\tt\ *(ptr+j)}$\in [0,1]\quad (0\le j\le 2m-1)\ $and the last datum {\tt *(ptr+2m)}$\not\in [0,1]$ denotesa terminator of data sequence.So, {\tt xfig\_3d\_polyline()} draws a 3-dimensional polylinein a cube $\,[0,1]\times [0,1]\times [0,1]\,$ of ${\bf R}^3$.}*{\hrulefill}*/#include <stdio.h>#include <stdlib.h>#include <math.h>#include "xfig.h"/*{\hrulefill}*{\qquad{\bf preliminaries}}{\nullThe purpose of this section is to define global variables\item{}{\tt FILE\ xfig\_out},\item{}{\tt int\ xfig\_status},\item{}{\tt int\ Color},\item{}{\tt int\ Depth},\item{}{\tt int\ LineWidth},\item{}{\tt int\ LineStyle},\item{}{\tt int\ FillStyle},\noindentand a fundamental function\item{}{\tt int\ dbl2int(double\ x)}.}*{\hrulefill}*/int figOffsetX = 0; /* where the picture starts */int figOffsetY = 0; /* to support xfig_newpicture */int xfig_status = 0; /* xfig status flag {\hfill}*/FILE *xfig_out = stdout; /* xfig stream pointer {\hfill}*/int Color = -1; /* xfig color {\hfill}*/int Depth = 0; /* xfig depth {\hfill}*/int LineWidth = 1; /* xfig line width {\hfill}*/int LineStyle = 0; /* xfig line style {\hfill}*/int FillStyle = 0; /* xfig fill style {\hfill}*//* double to int {\hfill}*/int dbl2int(double x){ int i, j; if (x > 0) { i = (int) x; j = i + 1; if ((x - (double) i) < ((double) j - x)) return i; else return j; } else { i = (int) x; j = i - 1; if (((double) i - x) < (x - (double) j)) return i; else return j; }}/*{\hrulefill}*{\qquad{\bf xfig elementary functions}}{\nullIn this section, we shall define the following functions:\item{}{\tt void\ xfig\_color(double\ x)}\item{}{\tt void\ xfig\_depth(double\ x)}\item{}{\tt void\ xfig\_linewidth(double\ x)}\item{}{\tt void\ xfig\_linestyle(double\ x)}\item{}{\tt void\ xfig\_fillstyle(double\ x)}\item{}{\tt void\ xfig\_polyline(int *ptr),}\noindentwhere {\tt x} is a real number such that$0\le{\tt x}\le 1$,and {\tt ptr} denotes an integer pointer of plotting data\ $0\sim${\tt PAGESIZE},{\it i.e.\/}, {\tt *ptr, *(ptr+1), *(ptr+2), $\cdots$} is a sequence ofxfig x- and y-coordinates with$0\le x,\ y\le {\tt PAGESIZE}$.Any datum {\tt *(ptr+?)} satisfying either$<0$ or $>{\tt PAGESIZE}$is interpreted as a data sequence terminator.}*{\hrulefill}*//* xfig data check {\hfill}*/int xfig_data_check(int i){ if ((i < 0) || (i > PAGESIZE)) return 0; else return 1;}void xfig_startfile (FILE* file){ xfig_status = 0; figOffsetX = 0; figOffsetY = 0; xfig_out = file; }/* xfig initialization {\hfill}*/void xfig_init(){ fprintf(xfig_out, "#FIG 2.1\n"); fprintf(xfig_out, "80 2\n");}/* xfig color {\hfill}*/void xfig_color(double x){ if ((x < 0.) || (x > 1.)) { Color = -1; return; } if (xfig_status == 0) { ++xfig_status; xfig_init(); } /* $0\le{\tt Color}\le 7$ {\hfill}*/ Color = dbl2int(7. * (1. - x));}/* xfig depth {\hfill}*/void xfig_depth(double x){ if ((x < 0.) || (x > 1.)) return; if (xfig_status == 0) { ++xfig_status; xfig_init(); } /* $0\le{\tt Depth}\le 999$ {\hfill}*/ Depth = dbl2int(999. * x);}/* xfig linewidth {\hfill}*/void xfig_linewidth(double x){ if ((x < 0.) || (x > 1.)) { LineWidth = 1; return; } if (xfig_status == 0) { ++xfig_status; xfig_init(); } /* $0\le{\tt LineWidth}\le 16$ {\hfill}*/ LineWidth = dbl2int(16. * x); if ((LineWidth == 0) && (x > 0.)) LineWidth = 1;}/* xfig linestyle {\hfill}*/void xfig_linestyle(double x){ if ((x < 0.) || (x > 1.)) return; if (xfig_status == 0) { ++xfig_status; xfig_init(); } /* $0\le{\tt LineStyle}\le 11$ {\hfill}*/ LineStyle = dbl2int(11. * (1. - x));}/* xfig fillstyle {\hfill}*/void xfig_fillstyle(double x){ if ((x < 0.) || (x > 1.)) { FillStyle = 0; return; } if (xfig_status == 0) { ++xfig_status; xfig_init(); } /* $1\le{\tt FillStyle}\le 21$ {\hfill}*/ FillStyle = dbl2int(20. * x + 1.);}/* xfig polyline command {\hfill}*/void xfig_polyline_command(){ double x4, x5, x6, x8, x9; x4 = (double) LineWidth; x5 = (double) Color; x6 = (double) Depth; x8 = (double) FillStyle; x9 = (double) LineStyle; /* solid line {\hfill}*/ if (x9 == 0.) { fprintf(xfig_out, "2 1 0 %2.0f %1.0f %3.0f 0 %2.0f %3.3f 7 0 0 \n", x4, x5, x6, x8, 0.); return; } /* invisible line {\hfill}*/ if (x9 == 11.) { fprintf(xfig_out, "2 1 0 0 %1.0f %3.0f 0 %2.0f %3.3f 7 0 0 \n", x5, x6, x8, 0.); return; } /* dotted line {\hfill}*/ fprintf(xfig_out, "2 1 2 %2.0f %1.f %3.0f 0 %2.0f %3.3f 7 0 0 \n", x4, x5, x6, x8, x9);}/* xfig polyline data {\hfill}*/void xfig_polyline_data(int i){ static int j = 0, flag = 0; if ((i < 0) || (i > 9999)) { fprintf(stderr, "Error: unacceptable xfig data\n"); exit(0); } if (flag == 0) { fprintf(xfig_out, "\t"); fprintf(xfig_out, " %d", i); flag = 1; j = i; return; } if ((j == 9999) && (i == 9999)) { fprintf(xfig_out, " 9999\n"); flag = 0; return; } fprintf(xfig_out, " %d", i); j = i;}/* xfig polyline {\hfill}*/void xfig_polyline(int *iptr){ int i, j; i = *iptr++; j = *iptr++; if ((xfig_data_check(i) == 0) || (xfig_data_check(j) == 0)) return; if (xfig_status == 0) { ++xfig_status; xfig_init(); } xfig_polyline_command(); while ((xfig_data_check(i) != 0) && (xfig_data_check(j) != 0)) { xfig_polyline_data(figOffsetX + i); xfig_polyline_data(figOffsetY + j); i = *iptr++; j = *iptr++; } xfig_polyline_data(9999); xfig_polyline_data(9999);}/*{\hrulefill}*{\qquad{\bf xfig\_2d\_polyline}}{\nullWe shall define a function\item{}{\tt void\ xfig\_2d\_polyline(double\ *ptr)},\noindentwhere $0\le${\tt *(ptr+?)}$\le 1$.}*{\hrulefill}*//* draw a 2-dimensional polyline {\hfill}*/void xfig_2d_polyline(const double *dptr){ int i, *buffer, *iptr; for (i = 0; (dptr[i] >= 0.) && (dptr[i] <= 1.) ; ++i); buffer = new int[i+3]; if (buffer == NULL) { fprintf(stderr, "Error: memory allocation failed\n"); exit(0); } iptr = buffer; while (1) { if ((*dptr < 0.) || (*dptr > 1.)) break; if ((*(dptr + 1) < 0.) || (*(dptr + 1) > 1.)) break; *iptr = dbl2int(*dptr * (double) PAGESIZE); ++dptr; ++iptr; *iptr = dbl2int((1. - *dptr) * (double) PAGESIZE); ++dptr; ++iptr; } *iptr = -(int) PAGESIZE; *++iptr = -(int) PAGESIZE; xfig_polyline(buffer); delete[] buffer;}/*{\hrulefill}*{\qquad{\bf xfig\_3d\_polyline}}{\nullWe shall define functions\item{}{\tt void\ xfig\_3d\_viewpoint(double\ *ptr)},\item{}{\tt void\ xfig\_3d\_polyline(double\ *ptr)},\noindentwhere $0\le${\tt *(ptr+?)}$\le 1$.}*{\hrulefill}*//* default viewpoint (X,Y,Z) {\hfill}*/double xfig_X = 2.310453;double xfig_Y = 1.262206;double xfig_Z = 1.438277;/* default distance from (.5,.5,.5) to (X,Y,Z) {\hfill}*/double xfig_R = 2.176939;/* default orthonormal frame U, V, W {\hfill}*/double xfig_U[3] = {-0.388018, 0.921652, 0.000000};double xfig_V[3] = {-0.397239, -0.167239, 0.902348};double xfig_W[3] = {0.831651, 0.350128, 0.431007};/* normalize a vector (dptr[0], dptr[1], dptr[2]) {\hfill}*/void xfig_normalize(double* dptr){ double r; r = sqrt(dptr[0] * dptr[0] + dptr[1] * dptr[1] + dptr[2] * dptr[2]); dptr[0] = dptr[0] / r; dptr[1] = dptr[1] / r; dptr[2] = dptr[2] / r;}/* 3-dimensional viewpoint (dptr[0], dptr[1], dptr[2]) {\hfill}*/void xfig_3d_viewpoint(const double* dptr){ double x, y, z, r, s; x = dptr[0]; y = dptr[1]; z = dptr[2]; r = sqrt((x - .5) * (x - .5) + (y - .5) * (y - .5) + (z - .5) * (z - .5)); if (r < .8660254) return; xfig_X = x; xfig_Y = y; xfig_Z = z; xfig_R = r; xfig_W[0] = xfig_X - .5; xfig_W[1] = xfig_Y - .5; xfig_W[2] = xfig_Z - .5; s = xfig_W[0] * xfig_W[0] + xfig_W[1] * xfig_W[1]; if (s < .000001) { s = .000001; xfig_W[0] += .001; xfig_W[1] += .001; } xfig_U[0] = -xfig_W[1]; xfig_U[1] = xfig_W[0]; xfig_U[2] = 0.; xfig_V[0] = (-xfig_W[0] * xfig_W[2]) / s; xfig_V[1] = (-xfig_W[1] * xfig_W[2]) / s; xfig_V[2] = 1.; xfig_normalize(xfig_W); xfig_normalize(xfig_U); xfig_normalize(xfig_V);}/* space to display {\hfill}*/void xfig_space2display(const double* dptr, int* iptr){ double a, b; a = (dptr[0] - .5) * xfig_U[0] + (dptr[1] - .5) * xfig_U[1] + (dptr[2] - .5) * xfig_U[2]; b = (dptr[0] - .5) * xfig_V[0] + (dptr[1] - .5) * xfig_V[1] + (dptr[2] - .5) * xfig_V[2]; iptr[0] = dbl2int(PAGESIZE * (.5 + a / xfig_R)); iptr[1] = dbl2int(PAGESIZE * (.5 - b / xfig_R));}/* draw a 3-dimensional polyline {\hfill}*/void xfig_3d_polyline(const double* dptr){ int i, *buffer, *iptr; for (i = 0; (dptr[i] >= -.1) && (dptr[i] <= 1.1) ; ++i); buffer = new int[i+3]; if (buffer == NULL) { fprintf(stderr, "Error: memory allocation failed\n"); exit(0); } iptr = buffer; while(1) { if ((*dptr < -.1) || (*dptr > 1.1)) break; if ((*(dptr + 1) < -.1) || (*(dptr + 1) > 1.1)) break; if ((*(dptr + 2) < -.1) || (*(dptr + 2) > 1.1)) break; xfig_space2display(dptr, iptr); dptr += 3; iptr += 2; } *iptr = -(int) PAGESIZE; *++iptr = -(int) PAGESIZE; xfig_polyline(buffer); delete[] buffer;}/* Begin a new picture */ void xfig_new_picture(int x, int y){ figOffsetX = PICMARGIN + (int) ((PAGESIZE + PICMARGIN) * x * 1.1); figOffsetY = PICMARGIN + (int) ((PAGESIZE + PICMARGIN) * y * 1.1);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?