📄 torus2.c
字号:
/******************************************************************************* File Name: torus2.c* Purpose: an implementation of the boundless simulation area as described by Haas et al.* Author: Jeff Boleng* Date Created:** Edited 12 July 2006 by Jon Petty: * Changed seed code to rand((int)time(NULL) + (int)getpid())** Copyright (C) 2004 Toilers Research Group -- Colorado School of Mines** Please see COPYRIGHT.TXT and LICENSE.TXT for copyright and license* details.******************************************************************************/#include <stdlib.h>#include <stdio.h>#include <time.h>#include <math.h>#include <unistd.h>#define true 1#define false 0#define min(a,b) (((a) < (b)) ? (a) : (b))#define max(a,b) (((a) > (b)) ? (a) : (b))double randAcceleration(double maxA, double t){ double v = 0.0; v = ((double)rand()/(double)RAND_MAX)*(maxA * t); if (rand()>=(RAND_MAX/2)) { v = -v; } return v;}double randDirection(double maxTheta, double t){ double theta = 0.0; theta = ((double)rand()/(double)RAND_MAX)*(maxTheta * t); if (rand()>=(RAND_MAX/2)) { theta = -theta; } return theta;}int main(int argc, char *argv[]){ double pi = 2*asin(1.0); double maxX=0.0, maxY=0.0; double endTime=0.0; double speedMax=0.0; double accelerationMax=0.0; double angularChangeMax=0.0; double speed = 0.0; double direction = 0.0; double xLoc = 0.0; double yLoc = 0.0; double xOld = 0.0; double yOld = 0.0; double interval; double curtime = 0.0; int wrap = false; if (argc == 8) { maxX = atof(argv[1]); maxY = atof(argv[2]); endTime = atof(argv[3]); speedMax = atof(argv[4]); accelerationMax = atof(argv[5]); angularChangeMax = (atof(argv[6])/180.0) * pi; interval = atof(argv[7]); fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); fprintf(stdout, "#\tmaxX = %9.2f\n", maxX); fprintf(stdout, "#\tmaxY = %9.2f\n", maxY); fprintf(stdout, "#\tendTime = %9.2f\n", endTime); fprintf(stdout, "#\tspeedMax = %9.2f\n", speedMax); fprintf(stdout, "#\taccelerationMax = %9.2f\n", accelerationMax); fprintf(stdout, "#\tangularChangeMax = %9.2f\n", angularChangeMax); fprintf(stdout, "#\tinterval = %9.2f\n", interval); fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"); } else { fprintf(stdout, "Usage: torus2 <max-x> <max-y> <end time>\n"); fprintf(stdout, " <max. speed> <max. acceleration>\n"); fprintf(stdout, " <max. angular change (in degrees)>\n"); fprintf(stdout, " <interval>\n"); return -1; } srand((int)time(NULL) + (int)getpid()); speed = min(max((speed+randAcceleration(accelerationMax, interval)), 0.0), speedMax); direction = direction + randDirection(angularChangeMax,interval); xLoc = ((double)rand()/(double)RAND_MAX)*maxX; yLoc = ((double)rand()/(double)RAND_MAX)*maxY; xLoc = 500.0; yLoc = 500.0; fprintf(stdout, "set xrange[0:%f]\n", maxX); fprintf(stdout, "set yrange[0:%f]\n\n", maxY); fprintf(stdout, "set multiplot\n"); fprintf(stdout, "plot \'-\' notitle with linespoints\n"); fprintf(stdout, "%f %f\ne\n", xLoc, yLoc); fprintf(stdout, "plot \'-\' notitle with lines\n"); fprintf(stdout, "%f %f\n", xLoc, yLoc); while (curtime <= endTime) { speed = min(max((speed+randAcceleration(accelerationMax, interval)), 0.0), speedMax); direction = direction + randDirection(angularChangeMax,interval); xOld = xLoc; yOld = yLoc; xLoc += speed*cos(direction); yLoc += speed*sin(direction); fprintf(stdout, "%f %f\n", xLoc, yLoc); if (xLoc >= maxX) { xLoc = 0.0; wrap = true; } else if (xLoc <= 0.0) { xLoc = maxX; wrap = true; } if (yLoc >= maxY) { yLoc = 0.0; wrap = true; } else if (yLoc <= 0.0) { yLoc = maxY; wrap = true; } if (wrap) { wrap = false; /* plot the place the path wraps */ fprintf(stdout, "%f %f\n", xOld, yOld); fprintf(stdout, "e\n"); /* plot the wrapping exit point in another color */ fprintf(stdout, "plot \'-\' notitle with linespoints 8\n"); fprintf(stdout, "%f %f\n", xOld, yOld); fprintf(stdout, "e\n"); /* plot the wrapping entry point in another color */ fprintf(stdout, "plot \'-\' notitle with linespoints 18\n"); fprintf(stdout, "%f %f\n", xLoc, yLoc); fprintf(stdout, "e\n"); fprintf(stdout, "plot \'-\' notitle with lines\n"); fprintf(stdout, "%f %f\n", xLoc, yLoc); } curtime += interval; } /* while (curtime <= endTime) */ fprintf(stdout, "e\n\n"); fprintf(stdout, "plot \'-\' notitle with linespoints\n"); fprintf(stdout, "%f %f\ne\n", xLoc, yLoc); fprintf(stdout, "set nomultiplot\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -