📄 torus.c
字号:
/******************************************************************************* File Name: torus.c* Purpose: random waypoint model placed on a boundless torus* 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 numberSamples 1000int main(int argc, char *argv[]){ double pi = 2.0*asin(1.0); double twoPi = 2.0*pi; double maxX=0.0, maxY=0.0; double endTime=0.0; double speedMean=0.0, speedDelta=0.0; double pauseMean=0.0, pauseDelta=0.0; double travelMean=0.0, travelDelta=0.0; int moving = false; double speed = 0.0; double direction = 0.0; double travelTime = 0.0; double pauseTime = 0.0; double xLoc = 0.0; double yLoc = 0.0; double xOld = 0.0; double yOld = 0.0; double speedLow, speedRange; double pauseLow, pauseRange; double travelLow, travelRange; double interval; double curtime = 0.0; int wrap = false; if (argc == 10) { maxX = atof(argv[1]); maxY = atof(argv[2]); endTime = atof(argv[3]); speedMean = atof(argv[4]); speedDelta = atof(argv[5]); pauseMean = atof(argv[6]); pauseDelta = atof(argv[7]); travelMean = atof(argv[8]); travelDelta = atof(argv[9]); 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, "#\tspeedMean = %9.2f\n", speedMean); fprintf(stdout, "#\tspeedDelta = %9.2f\n", speedDelta); fprintf(stdout, "#\tpauseMean = %9.2f\n", pauseMean); fprintf(stdout, "#\tpauseDelta = %9.2f\n", pauseDelta); fprintf(stdout, "#\ttravelMean = %9.2f\n", travelMean); fprintf(stdout, "#\ttravelDelta = %9.2f\n", travelDelta); fprintf(stdout, "#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n"); } else { fprintf(stdout, "Usage: torus <max-x> <max-y> <end time>\n"); fprintf(stdout, " <speed mean> <speed delta>\n"); fprintf(stdout, " <pause time> <pause time delta>\n"); fprintf(stdout, " <travel time> <travel time delta>\n"); return -1; } /* this is calculated to get at least numberSamples samples on the longest path across the simulation area at maximum speed */ interval = ((sqrt(maxX*maxX + maxY*maxY))/(speedMean+speedDelta))/numberSamples; srand((int)time(NULL) + (int)getpid()); speedLow = speedMean-speedDelta; pauseLow = pauseMean-pauseDelta; travelLow = travelMean-travelDelta; speedRange = 2*speedDelta; pauseRange = 2*pauseDelta; travelRange = 2*travelDelta; fprintf(stdout, "#\tInitial position:\n"); moving = (rand()>=(RAND_MAX/2))?true:false; if (moving) { speed = ((double)rand()/(double)RAND_MAX)*speedRange + speedLow; direction = ((double)rand()/(double)RAND_MAX)*twoPi; travelTime = ((double)rand()/(double)RAND_MAX)*travelRange + travelLow; pauseTime = 0.0; } else { speed = 0.0; direction = 0.0; travelTime = 0.0; pauseTime = ((double)rand()/(double)RAND_MAX)*pauseRange + pauseLow; } xLoc = ((double)rand()/(double)RAND_MAX)*maxX; yLoc = ((double)rand()/(double)RAND_MAX)*maxY; 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"); /* xLoc = 500.0; yLoc = 500.0; */ fprintf(stdout, "%f %f\n", xLoc, yLoc); while (curtime <= endTime) { if (moving) { if (travelTime <= 0.0) { moving = false; speed = 0.0; direction = 0.0; travelTime = 0.0; pauseTime = ((double)rand()/(double)RAND_MAX)*pauseRange + pauseLow; fprintf(stdout, "%f %f\n", xLoc, yLoc); } else { travelTime -= interval; xOld = xLoc; yOld = yLoc; xLoc += (speed*interval)*cos(direction); yLoc += (speed*interval)*sin(direction); 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 linespoints\n"); fprintf(stdout, "%f %f\n", xLoc, yLoc); } } } else /* not moving */ { if (pauseTime <= 0.0) { moving = true; speed = ((double)rand()/(double)RAND_MAX)*speedRange + speedLow; direction = ((double)rand()/(double)RAND_MAX)*twoPi; travelTime = ((double)rand()/(double)RAND_MAX)*travelRange + travelLow; pauseTime = 0.0; } else { pauseTime -= interval; } } /* if (moving[i]) */ curtime += interval; } /* while (curtime <= endTime) */ fprintf(stdout, "e\n\n"); fprintf(stdout, "set nomultiplot\n"); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -