Exemple #1
0
void
conicarc(int x, int y, int x0, int y0, int x1, int y1, int a, int b)
{
	/* based on Bresenham, CACM, Feb 77, pp 102-3 */
	/* by Chris Van Wyk */
	/* capitalized vars are an internal reference frame */
	long dotcount = 0;
	int osize;
	int	xs, ys, xt, yt, Xs, Ys, qs, Xt, Yt, qt,
		M1x, M1y, M2x, M2y, M3x, M3y,
		Q, move, Xc, Yc;
	int ox1, oy1;
	long	delta;
	float	xc, yc;
	float	radius, slope;
	float	xstep, ystep;

	osize = size;
	setsize(t_size(pstab[osize-1] / drawsize));
	ox1 = x1;
	oy1 = y1;
	if (a != b)	/* an arc of an ellipse; internally, will still think of circle */
		if (a > b) {
			xstep = (float)a / b;
			ystep = 1;
			radius = b;
		} else {
			xstep = 1;
			ystep = (float)b / a;
			radius = a;
		} 
	else {	/* a circular arc; radius is computed from center and first point */	
		xstep = ystep = 1;
		radius = sqrt((float)(sqr(x0 - x) + sqr(y0 - y)));
	}


	xc = x0;
	yc = y0;
	/* now, use start and end point locations to figure out
	the angle at which start and end happen; use these
	angles with known radius to figure out where start
	and end should be
	*/
	slope = atan2((double)(y0 - y), (double)(x0 - x) );
	if (slope == 0.0 && x0 < x)
		slope = 3.14159265;
	x0 = x + radius * cos(slope) + 0.5;
	y0 = y + radius * sin(slope) + 0.5;
	slope = atan2((double)(y1 - y), (double)(x1 - x));
	if (slope == 0.0 && x1 < x)
		slope = 3.14159265;
	x1 = x + radius * cos(slope) + 0.5;
	y1 = y + radius * sin(slope) + 0.5;
	/* step 2: translate to zero-centered circle */
	xs = x0 - x;
	ys = y0 - y;
	xt = x1 - x;
	yt = y1 - y;
	/* step 3: normalize to first quadrant */
	if (xs < 0)
		if (ys < 0) {
			Xs = abs(ys);
			Ys = abs(xs);
			qs = 3;
			M1x = 0;
			M1y = -1;
			M2x = 1;
			M2y = -1;
			M3x = 1;
			M3y = 0;
		} else {
			Xs = abs(xs);
			Ys = abs(ys);
			qs = 2;
			M1x = -1;
			M1y = 0;
			M2x = -1;
			M2y = -1;
			M3x = 0;
			M3y = -1;
		} 
	else if (ys < 0) {
		Xs = abs(xs);
		Ys = abs(ys);
		qs = 0;
		M1x = 1;
		M1y = 0;
		M2x = 1;
		M2y = 1;
		M3x = 0;
		M3y = 1;
	} else {
		Xs = abs(ys);
		Ys = abs(xs);
		qs = 1;
		M1x = 0;
		M1y = 1;
		M2x = -1;
		M2y = 1;
		M3x = -1;
		M3y = 0;
	}


	Xc = Xs;
	Yc = Ys;
	if (xt < 0)
		if (yt < 0) {
			Xt = abs(yt);
			Yt = abs(xt);
			qt = 3;
		} else {
			Xt = abs(xt);
			Yt = abs(yt);
			qt = 2;
		} 
	else if (yt < 0) {
		Xt = abs(xt);
		Yt = abs(yt);
		qt = 0;
	} else {
		Xt = abs(yt);
		Yt = abs(xt);
		qt = 1;
	}


	/* step 4: calculate number of quadrant crossings */
	if (((4 + qt - qs)
	     % 4 == 0)
	     && (Xt <= Xs)
	     && (Yt >= Ys)
	    )
		Q = 3;
	else
		Q = (4 + qt - qs) % 4 - 1;
	/* step 5: calculate initial decision difference */
	delta = sqr(Xs + 1)
	 + sqr(Ys - 1)
	-sqr(xs)
	-sqr(ys);
	/* here begins the work of drawing
   we hope it ends here too */
	while ((Q >= 0)
	     || ((Q > -2)
	     && ((Xt > Xc)
	     && (Yt < Yc)
	    )
	    )
	    ) {
		if (dotcount++ % DX == 0)
			putdot((int)xc, (int)yc);
		if (Yc < 0.5) {
			/* reinitialize */
			Xs = Xc = 0;
			Ys = Yc = sqrt((float)(sqr(xs) + sqr(ys)));
			delta = sqr(Xs + 1) + sqr(Ys - 1) - sqr(xs) - sqr(ys);
			Q--;
			M1x = M3x;
			M1y = M3y;
			 {
				int	T;
				T = M2y;
				M2y = M2x;
				M2x = -T;
				T = M3y;
				M3y = M3x;
				M3x = -T;
			}
		} else {
			if (delta <= 0)
				if (2 * delta + 2 * Yc - 1 <= 0)
					move = 1;
				else
					move = 2;
			else if (2 * delta - 2 * Xc - 1 <= 0)
				move = 2;
			else
				move = 3;
			switch (move) {
			case 1:
				Xc++;
				delta += 2 * Xc + 1;
				xc += M1x * xstep;
				yc += M1y * ystep;
				break;
			case 2:
				Xc++;
				Yc--;
				delta += 2 * Xc - 2 * Yc + 2;
				xc += M2x * xstep;
				yc += M2y * ystep;
				break;
			case 3:
				Yc--;
				delta -= 2 * Yc + 1;
				xc += M3x * xstep;
				yc += M3y * ystep;
				break;
			}
		}
	}


	setsize(osize);
	drawline((int)xc-ox1,(int)yc-oy1,".");
}
Exemple #2
0
void fillpoly(short int *datas, int n, ColorP c) {
	static int dots[SCREENHEIGHT][MAXPTS];
	static int counters[SCREENHEIGHT];
	short int x1, y1, x2, y2;
	int i, j, k, dir = -2;
	double step, curx;

	//    printf("fillpoly starting with n = %i dots\n", n);

	if(n <= 2) {
		//printf("Désolé Vincent, tu m'envoies une ligne ou un point là ;-)\n");
		switch(n) {
		case 0:
			return;
		case 1:
			pixel(datas[0], datas[1], c);
			return;
		case 2:
			line(datas[0], datas[1], datas[2], datas[3], c);
			return;
		}
	}

	// Reinit array counters

	for(i = 0; i < SCREENHEIGHT; i++) {
		counters[i] = 0;
	}

	// Drawing lines

	x2 = datas[n * 2 - 2];
	y2 = datas[n * 2 - 1];

	for(i = 0; i < n; i++) {
		x1 = x2;
		y1 = y2;
		x2 = datas[i * 2];
		y2 = datas[i * 2 + 1];

		//  line(x1, y1, x2, y2, c);
		//  continue;

		if(y1 == y2) {
			//      printf("Horizontal line. x1: %i, y1: %i, x2: %i, y2: %i\n", x1, y1, x2, y2);
			if(!dir)
				continue;
			putdot(x1, y1);
			dir = 0;
			continue;
		}

		step = (double)(x2 - x1) / (y2 - y1);

		//  printf("x1: %i, y1 = %i, x2 = %i, y2 = %i, step: %f\n", x1, y1, x2, y2, step);

		curx = x1;

		if(y1 < y2) {
			for(j = y1; j < y2; j++, curx += step) {
				//    printf("j = %i, curx = %f\n", j, curx);
				putdot((int)(curx + 0.5), j);
			}
			if(dir == -1) {
				//    printf("Adding extra (%i, %i)\n", x1, y1);
				putdot(x1, y1);
			}
			dir = 1;
		} else {
			for(j = y1; j > y2; j--, curx -= step) {
				//    printf("j = %i, curx = %f\n", j, curx);
				putdot((int)(curx + 0.5), j);
			}
			if(dir == 1) {
				//    printf("Adding extra (%i, %i)\n", x1, y1);
				putdot(x1, y1);
			}
			dir = -1;
		}
	}

	x1 = x2;
	y1 = y2;
	x2 = datas[0];
	y2 = datas[1];

	if(((y1 < y2) && (dir == -1)) || ((y1 > y2) && (dir == 1)) || ((y1 == y2) && (dir == 0))) {
		//  printf("Adding final extra (%i, %i)\n", x1, y1);
		putdot(x1, y1);
	}

	// NOTE: all counters should be even now. If not, this is a bad (C) thing :-P

	// Sorting datas

	for(i = 0; i < SCREENHEIGHT; i++) {
		// Very bad sorting... but arrays are very small (0, 2 or 4), so it's no quite use...
		for(j = 0; j < (counters[i] - 1); j++) {
			for(k = 0; k < (counters[i] - 1); k++) {
				if(dots[i][k] > dots[i][k + 1])
					swap(dots[i][k], dots[i][k + 1]);
			}
		}
	}

	// Drawing.

	for(i = 0; i < SCREENHEIGHT; i++) {
		if(counters[i]) {
			//      printf("%i dots on line %i\n", counters[i], i);
			for(j = 0; j < counters[i] - 1; j += 2) {
				//    printf("Drawing line (%i, %i)-%i\n", dots[i][j], dots[i][j + 1], i);
				hline(dots[i][j], dots[i][j + 1], i, c);
#ifdef DEBUGGING_POLYS
				if((!dots[i][j]) || !(dots[i][j + 1])) {
					printf("fillpoly: BLARGH!\n");
					exit(-1);
				}
#endif
			}
		}
	}
}
Exemple #3
0
int genrmt(char *infile, char *outfile)
{
  int i,j;
  FILE *fp;
  double x,t0,t1;
  char *cbuf,*fext;

  /* open file */
  switch(seqmode) {
  case SEQ_MOLPHY: fext=fext_molphy; break;
  case SEQ_PAML: fext=fext_paml; break;
  case SEQ_PAUP: fext=fext_paup; break;
  case SEQ_PUZZLE: fext=fext_puzzle; break;
  case SEQ_PHYML: fext=fext_phyml; break;
  case SEQ_MT: 
  default: fext=fext_mt; break;
  }
  if(infile) {
    fp=openfp(infile,fext,"r",&cbuf);
    printf("\n# reading %s",cbuf);
  } else {
    fp=STDIN;
    printf("\n# reading from stdin");
  }

  /* read file */
  mm=nn=0;
  switch(seqmode) {
  case SEQ_MOLPHY: 
    datmat = fread_mat_lls(fp, &mm, &nn); break;
  case SEQ_PAML: 
    datmat = fread_mat_lfh(fp, &mm, &nn); break;
  case SEQ_PAUP: 
    datmat = fread_mat_paup(fp, &mm, &nn); break;
  case SEQ_PUZZLE: 
    datmat = fread_mat_puzzle(fp, &mm, &nn); break;
  case SEQ_PHYML: 
    datmat = fread_mat_phyml(fp, &mm, &nn); break;
  case SEQ_MT: 
  default: 
    datmat = fread_mat(fp, &mm, &nn); break;  
  }
  if(infile) {fclose(fp);  FREE(cbuf);}
  printf("\n# M:%d N:%d",mm,nn);

  /* allocating buffers */
  datvec=new_vec(mm);
  bn=new_ivec(kk); rr1=new_vec(kk);

  /* calculate the log-likelihoods */
  for(i=0;i<mm;i++) {
    x=0; for(j=0;j<nn;j++) x+=datmat[i][j];
    datvec[i]=x;
  }
  
  /* calculate scales */
  for(i=0;i<kk;i++) {
    bn[i]=(int)(rr[i]*nn); /* sample size for bootstrap */
    rr1[i]=(double)bn[i]/nn; /* recalculate rr for integer adjustment */
  }

  /* open out file */
  if(outfile) {
    /* vt ascii write to file */
    fp=openfp(outfile,fext_vt,"w",&cbuf);
    printf("\n# writing %s",cbuf);
    fwrite_vec(fp,datvec,mm);
    fclose(fp); FREE(cbuf);
    /* rmt binary write to file */
    fp=openfp(outfile,fext_rmt,"wb",&cbuf);
    printf("\n# writing %s",cbuf);
    fwrite_bvec(fp,datvec,mm);
    fwrite_bvec(fp,rr1,kk);
    fwrite_bivec(fp,bb,kk);
    fwrite_bi(fp,kk);
  } else {
    /* rmt ascii write to stdout */
    printf("\n# writing to stdout");
    printf("\n# OBS:\n"); write_vec(datvec,mm);
    printf("\n# R:\n"); write_vec(rr1,kk);
    printf("\n# B:\n"); write_ivec(bb,kk);
    printf("\n# RMAT:\n");
    printf("%d\n",kk);
  }


  /* generating the replicates by resampling*/
  for(i=j=0;i<kk;i++) j+=bb[i];
  printf("\n# start generating total %d replicates for %d items",j,mm);
  fflush(STDOUT);
  t0=get_time();

  for(i=0;i<kk;i++) {
    repmat=new_lmat(mm,bb[i]);
    scaleboot(datmat,repmat,mm,nn,bn[i],bb[i]);
    if(outfile) {
      fwrite_bmat(fp,repmat,mm,bb[i]);
      putdot();
    } else {
      printf("\n## RMAT[%d]:\n",i); write_mat(repmat,mm,bb[i]);
    }
    free_lmat(repmat,mm);
  }

  t1=get_time();
  printf("\n# time elapsed for bootstrap t=%g sec",t1-t0);

  if(outfile) {
    fclose(fp); FREE(cbuf);
  }

  /* freeing buffers */
  free_vec(bn); free_vec(rr1); free_vec(datvec); free_mat(datmat);

  return 0;
}
Exemple #4
0
int main( int argc, char *argv[]){

    if(argc < 3) {
		printf("Usage: %s filename.img] Size x.x MB\n", argv[0]);
        return 1;
    }

	int i, j;
	unsigned long last_cnt;

	strcpy(drv_type,"hd");
	strcpy(filename,argv[1]);
	cylinders = (atof(argv[2])*2.1)/1024/1024;// << 1);
	printf("creating FAT12 image size = %3.2f MB's\n",cylinders/2);
	// print start string
	//printf(strtstr);

	//do {
	//	printf("Make Floppy or Hard Drive image? (fd/hd) [fd]: ");
	//	gets(drv_type);
	//	if (!strlen(drv_type)) { strcpy(drv_type, "fd"); break; }
	//} while (strcmp(drv_type, "fd") && strcmp(drv_type, "hd"));

	//printf("                          As filename [%c.img]: ", (drv_type[0] == 'h') ? 'c' : 'a');
	//gets(filename);
	//if (!strlen(filename)) strcpy(filename, (drv_type[0] == 'h') ? "c.img" : "a.img");

	//if (drv_type[0] == 'h') {
	//	do {
	//		printf("                   Size (meg) (1 - 1024) [10]: ");
	//		gets(strbuff);
	//		if (!strlen(strbuff))
	//			i = 10;
	//		else
	//			i = atoi(strbuff);
	//	} while ((i < 1) || (i > 1024));
		//cylinders = (10 << 1);  // very close at 16 heads and 63 spt
		heads = 16;
		spt = 63;
		fat_size = 12;
	//} else {
	//	do {
	//		printf("   Sectors per track (8, 9, 15, 18, 36)  [18]: ");
	//		gets(strbuff);
	//		if (!strlen(strbuff))
	//			i = 18;
	//		else
	//			i = atoi(strbuff);
	//	} while ((i != 8) && (i != 9) && (i != 15) && (i != 18) && (i != 36));
	//	if (i == 36) spclust = 2;
	//	spt = i;
	//	do {
	//		printf("                           Heads: (1, 2)  [2]: ");
	//		gets(strbuff);
	//		if (!strlen(strbuff))
	//			i = 2;
	//		else
	//			i = atoi(strbuff);
	//	} while ((i != 1) && (i != 2));
	//	heads = i;
	//	do {
	//		printf("                    Cylinders: (40, 80)  [80]: ");
	//		gets(strbuff);
	//		if (!strlen(strbuff))
	//			i = 80;
	//		else
	//			i = atoi(strbuff);
	//	} while ((i != 40) && (i != 80));
	//	cylinders = i;
	//	fat_size = 12;
	//}
	sectors = (cylinders * heads * spt);

	//do {
	//	printf("                     Sectors per Cluster [% 2i]: ", spclust);
	//	gets(strbuff);
	//	if (!strlen(strbuff))
	//		i = spclust;
	//	else
	//		i = atoi(strbuff);
	//} while (i > 255);
	//spclust = i;

	//do {
	//	printf("                          Number of FAT's [2]: ");
	//	gets(strbuff);
	//	if (!strlen(strbuff))
	//		fats = 2;
	//	else
	//		fats = atoi(strbuff);
	//} while (fats > 2);
	//
	//do {
	//	printf("                               FAT Size: [%02i]: ", fat_size);
	//	gets(strbuff);
	//	if (!strlen(strbuff))
	//		i = fat_size;
	//	else
	//		i = atoi(strbuff);
	//} while ((i != 12) && (i != 16) && (i != 32));
	//fat_size = i;

	switch (fat_size) {
	case 12:
		//if ((sectors / (unsigned long) spclust) > 4086L) {
		//	printf(" *** Illegal Size disk with FAT 12 *** \n");
		//	exit(0x12);
		//}
		spfat = (unsigned short) ((unsigned short)((float) sectors * 1.5) / (512 * spclust)) + 1;

		// actual count bytes on last fat sector needed as zeros (???)
		last_cnt = ((sectors - ((fats * spfat) + 17)) / spclust);
		last_cnt = ((unsigned long) ((float) last_cnt * 1.5) % 512);

		break;
	case 16:
		if ((sectors / (unsigned long) spclust) > 65526L) {
			printf(" *** Illegal Size disk with FAT 16 *** \n");
			exit(0x12);
		}
		spfat = (unsigned short) ((sectors << 1) / (512 * spclust)) + 1;

		// actual count bytes on last fat sector needed as zeros (???)
		last_cnt = ((sectors - ((fats * spfat) + 17)) / spclust);
		last_cnt = ((unsigned long) (last_cnt << 1) % 512);
		
		break;
	default:
		spfat = (unsigned short) ((sectors << 2) / (512 * spclust)) + 1;

		// actual count bytes on last fat sector needed as zeros (???)
		last_cnt = ((sectors - ((fats * spfat) + 17)) / spclust);
		last_cnt = ((unsigned long) (last_cnt << 2) % 512);
	}
	
	//printf("\n       Creating file:   [%s]"
	//	   "\n           Cylinders:    %i"
	//	   "\n               Sides:    %i"
	//	   "\n       Sectors/Track:    %i"
	//	   "\n       Total Sectors:    %lu"
	//	   "\n                Size:    %3.2f (megs)"
	//	   "\n     Sectors/Cluster:    %i"
	//	   "\n               FAT's:    %i"
	//	   "\n         Sectors/FAT:    %i"
	//	   "\n            FAT size:    %i",
	//	   filename, cylinders, heads, spt, sectors,
 //      (float) ((float) sectors / 2000.0), spclust, fats, spfat, fat_size);

	// create BPB
	bpb.jmps[0] = 0xEB; bpb.jmps[1] = 0x3C;
	bpb.nop = 0x90;
	memcpy(bpb.oemname, "MKDOSFS ", 8);	//    char oemname[8];    // OEM name
	bpb.nBytesPerSec = 512;		//  unsigned short nBytesPerSec;  // Bytes per sector
	bpb.nSecPerClust = spclust;	//  unsigned  char nSecPerClust;  // Sectors per cluster
	bpb.nSecRes = 1;			//  unsigned short nSecRes;       // Sectors reserved for Boot Record
	bpb.nFATs = fats;			//  unsigned  char nFATs;         // Number of FATs
	bpb.nRootEnts = 224;		//  unsigned short nRootEnts;     // Max Root Directory Entries allowed
	if (sectors < 65536) {		//  unsigned short nSecs;		  // Number of Logical Sectors
		bpb.nSecs = (unsigned short) sectors;
		bpb.nSecsExt = 0; 		//  unsigned  long nSecsExt;      // This value used when there are more
	} else {
		bpb.nSecs = 0;
		bpb.nSecsExt = sectors;	//  unsigned  long nSecsExt;      // This value used when there are more
	}
	bpb.mDesc = 0xF9;			//  unsigned  char mDesc;         // Medium Descriptor Byte (we have it set as floppy 1.44)
	bpb.nSecPerFat = spfat; 	//  unsigned short nSecPerFat;    // Sectors per FAT
	bpb.nSecPerTrack = spt;		//  unsigned short nSecPerTrack;  // Sectors per Track
	bpb.nHeads = heads;			//  unsigned short nHeads;        // Number of Heads
	bpb.nSecHidden = 0;			//  unsigned  long nSecHidden;    // Number of Hidden Sectors
	bpb.DriveNum = 0;			//  unsigned  char DriveNum;      // Physical drive number
	bpb.nResByte = 0;			//  unsigned  char nResByte;      // Reserved (we use for FAT type (12- 16-bit)
	bpb.sig = 0x29;				//  unsigned  char sig;           // Signature for Extended Boot Record
	bpb.SerNum = 0;				//  unsigned  long SerNum;        // Volume Serial Number
	memcpy(bpb.VolName, "NO LABEL    ", 11); // char VolName[11]; // Volume Label
	sprintf(strbuff, "FAT%2i   ", fat_size);
	memcpy(bpb.FSType, strbuff, 8);		 // char FSType[8];   // File system type
	memset(bpb.filler, 0, 448);                                         // first, clear it out
	memcpy(bpb.filler, boot_code, sizeof(boot_code));                   // then place code
	memcpy(bpb.filler+sizeof(boot_code), boot_data, sizeof(boot_data)); // then place data
	bpb.boot_sig = 0xAA55;

	if ((fp = fopen(filename, "wb")) == NULL) {
		printf("\nError creating file [%s]", filename);
		return 0x01;
	}

	printf("\n\nWorking[");

	// write the BPB
	putdot();
	write_sectors(fp, &bpb, 1);
	sectors--;

	// write the FAT(s)
	for (i=0; i<fats; i++) {
		memset(buffer, 0, 512);
		switch (fat_size) {
		case 32:
			buffer[7] = 0xFF;
			buffer[6] = 0xFF;
			buffer[5] = 0xFF;
			buffer[4] = 0xFF;
		case 16:
			buffer[3] = 0xFF;
		case 12:
			buffer[0] = 0xF9;
			buffer[1] = 0xFF;
			buffer[2] = 0xFF;
		}
		putdot();
		write_sectors(fp, &buffer, 1);
		sectors--;
		memset(buffer, 0, 512);
		for (j=0; j<spfat-2; j++) {
			putdot();
			write_sectors(fp, &buffer, 1);
			sectors--;
		}
		// write last sector of FAT filling in unused entries
		// (last_cnt isn't exact, but it is pretty close.  I just
		//  didn't want to spend the time to get it exact :)
    memset(buffer, 0, (unsigned short) last_cnt);
    memset(buffer + last_cnt, 0xFF, (unsigned short) (512-last_cnt));
		putdot();
		write_sectors(fp, &buffer, 1);
		sectors--;
	}

	// write the root
	memset(buffer, 0, 512);
	for (i=0; i<14; i++) {
		putdot();
		write_sectors(fp, &buffer, 1);
		sectors--;
	}

	// write data area
	memset(buffer, 0, 512);
	while (sectors--) {
		putdot();
		write_sectors(fp, &buffer, 1);
	}

	printf("]Done");

	// close the file
	fclose(fp);

	return 0x00;

}