⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contour.c

📁 二维矩阵方面的库
💻 C
📖 第 1 页 / 共 2 页
字号:
/*   HEADER:   CUG300;
      TITLE:   Contour ploting demonstration;
       DATE:   4/20/1989;
DESCRIPTION:   "Plot contours using mat.lib functions";
    VERSION:   2.04;
   FILENAME:   CONTOUR.C;
   SEE-ALSO:   MAT_V2D.H;
*/

/*===================================================================*/
/* ROUTINES TO GENERATE CADD CONTOUR LINES FROM INPUT GRID VALUES    */
/*===================================================================*/
/*      0,1       1,1                                                */
/*   m4.-----------.m3            ^ 0,jub                            */
/*     | .       . |              |                                  */
/*     |   .   .   |              |                                  */
/*     |     .m0   |              |                                  */
/*     |    .  .   |              |                                  */
/*     |  .      . |              |                                  */
/*   m1|___________|m2           0,0 --------------> iub,0           */
/*     0,0        1,0                                                */
/*                                                                   */
/*   grid square layout            coordinate system layout          */
/*   -------------------           ------------------------          */
/*===================================================================*/

#include <stdio.h>
#include "mat_v2d.h"
#include <math.h>
#include <stdlib.h>
#include <conio.h>


#define  FALSE       0
#define  TRUE        1
#define  NO_ROWS     24
#define  NO_COLS     72

#define  xchk(x)     if(x<0)puts("x<0");if(x>NO_COLS)puts("x>NO_COLS")
#define  ychk(y)     if(y<0)puts("y<0");if(y>NO_COLS)puts("y>NO_COLS")

void  box          (float x1,float y1,float x2,float y2 );
void  dda          (float x1,float y1,float x2,float y2,char symbol);
void  display      (void);
void  erase        (void);
void  GridSelect   (int iub, int jub, int nc, struct fmat *x,
                    struct fmat *y,struct fmat *d, struct fmat *z);
void ContourSelect (int i,int j, int nc,struct fmat *x,
						  struct fmat *y,struct fmat *d,struct fmat *z,
						  int iub, int jub);
void drawit        (float x1, float x2, float y1, float y2,
                    struct fmat *x, struct fmat *y,int iub,int jub );
void set_graphics  (void);
void file_output   (void);


char  frame[NO_COLS][NO_ROWS];


/*============================================================================*/

main()

/*============================================================================*/

{
int i,i1,j,j1,k,iub,jub,nc,prmerr,num_rows,num_cols,token_size;
struct   fmat *x,*y,*d,*z;
struct   tmat *input_data;
char     filename[20];

/*       Input arrays and indexes                                             */

	printf("Enter contour data filename => ");
   scanf("%s",filename);
   mtcnt(filename,&num_rows,&num_cols,&token_size);    /* Get input file size */
   tdim(input_data,num_rows,num_cols,token_size);      /* Dimension input     */
   mtget(filename,input_data);                         /* array               */

	iub = no_toks(input_data,0);                        /* Get indices from    */
	jub = no_toks(input_data,1);                        /* the number of tokens*/
	nc  = no_toks(input_data,2);                        /* stored in the       */
                                                       /* appropriate input   */
   fdim(d,iub,jub+1);                                  /* data line header    */
   fdim(x,iub,DCLVCT);
   fdim(y,jub,DCLVCT);                                 /* Dimension needed    */
   fdim(z,nc,DCLVCT);                                  /* arrays              */

	for(i=0; i<iub; i++) {                              /* Store x grid loca-  */
      fck(x,i,VCT);                                    /* tion.               */
		f(x,i,VCT) = tf(input_data,0,i,iub);
   }
	for(j=0; j<jub; j++) {                              /* Store y grid loca-  */
      fck(y,j,VCT);                                    /* tions               */
		f(y,j,VCT) = tf(input_data,1,j,jub);
   }
   for(i=0; i<nc; i++) {                               /* Store contour       */
      fck(z,i,VCT);                                    /* levels to be        */
      f(z,i,VCT) = tf(input_data,2,i,nc);              /* plotted             */
   }
   for (j=3,k=jub-1; j<num_rows; j++,k--)  {
      for (i=0; i<iub; i++) {                          /* Store f(x,y) for    */
			fck(d,i,k);                                   /* each grid line      */
			f(d,i,k) = tf(input_data,j,i,iub);            /* intersection        */
      }
   }
   iub--;jub--;

/*       Check input parameters for validity                                  */

   prmerr = FALSE;                                     /* Test for no grid    */
	if (iub<=0 || jub<=0)                               /* intersections.      */
      prmerr = TRUE;                                   /*                     */
   if (nc <=0)                                         /* No contours         */
      prmerr = TRUE;                                   /* requested           */
   for (k = 1; k <= (nc-1); k++)                       /*                     */
   {                                                   /*                     */
      if (f(z,k,VCT) <= f(z,k-1,VCT) )                 /* Duplicate contours  */
         prmerr = TRUE;                                /*                     */
   }                                                   /*                     */
   if (prmerr)                                         /*                     */
   {                                                   /*                     */
      printf ("Error in input parameters");            /* Message if any      */
      return;                                          /* errors found        */
   }                                                   /*                     */
   set_graphics();                                     /* Initialize graphic  */
                                                       /* frame buffer        */
   GridSelect (iub, jub, nc, x, y, d, z );             /* Plot contours       */
                                                       /*                     */
   display();                                          /* Display all         */
                                                       /* contours plotted    */
   file_output();                                      /*                     */
                                                       /* Place contour in    */
                                                       /* an ASCII file       */
}                                                      /*                     */
                                                                              

/*============================================================================*/

void GridSelect  (   int iub, int jub, int nc, struct fmat *x,
                     struct fmat *y,struct fmat *d, struct fmat *z )

/*============================================================================*/
{
int i, j;


/*    Scan the input array top down & left to right                           */



   for  (j = (jub-1); j >= 0; j--)
   {
      for (i = 0; i <= (iub-1); i++)
      {
		ContourSelect (i,j,nc,x,y,d,z,iub,jub);          /* Select contour  to  */
      }                                                /* plot                */
   }
}

/*============================================================================*/

void ContourSelect ( int i,int j, int nc,struct fmat *x,
                     struct fmat *y, struct fmat *d,
                     struct fmat *z, int iub, int jub )

/*============================================================================*/

{

/*   Declare & initialize variables                                           */

float     h [5],    xh [5],     yh[5];

int  	ish[5];
int    caseval,k,i1,j1,m1, m2, m3;
int			m;
static int  im[4] =   { 0, 1, 1, 0 };
static int  jm[4] =   { 0, 0, 1, 1 };
static int  castab [3][3][3] =
                         { { { 0, 0, 8 },
                             { 0, 2, 5 },              /*   (k,i,j)       */
                             { 7, 6, 9 }, },           /*   -- i ---->    */
                                                       /*  | \            */  
                                 { { 0, 0, 4 },        /*      k          */  
                                   { 1, 0, 1 },        /*  j              */  
                                   { 4, 0, 0 }, },     /*  |       \      */  
                                                       /*  v         @    */  
                                        { { 9, 6, 7 },                        
                                          { 5, 2, 0 },                        
                                          { 8, 0, 0 }  }  };                  
                                                                              
float dmin, dmax, x1, y1, x2, y2;                                             
                                                                              
/*   Find the lowest vertex                                                   */
                                                                              
   if ( f(d,i,j) < f(d,i,j+1) )
      dmin = f(d,i,j);                                                        
   else dmin = f(d,i,j+1);
   if ( f(d,i+1,j)  < dmin )
      dmin = f(d,i+1,j);                                                      
   if ( f(d,i+1,j+1) < dmin )
      dmin = f(d,i+1,j+1);                                                    
                                                                              
/*   Find highest vertex                                                      */
                                                                              
   if ( f(d,i,j) > f(d,i,j+1) )
      dmax = f(d,i,j);
      else dmax = f(d,i,j+1);                                                 
   if ( f(d,i+1,j) > dmax)
      dmax= f(d,i+1,j);
   if (f(d,i+1,j+1) > dmax)
       dmax = f(d,i+1,j+1);                                                   
   if ( dmax < f(z,0,VCT) || dmin > f(z,nc-1,VCT) )
       return;  /* No contours to be plotted in grid */

/*   Draw each contour in grid square                                         */

   for (k = 0; k <= (nc-1); k++)
   {
      if (!( f(z,k,VCT) < dmin || f(z,k,VCT) > dmax ))
      {   /* Contour in grid */

/*   Determine line segment case                                              */

         for (m = 4; m >= 0; m--)
         {
            if ( m > 0 )
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -