水的流速成抛物线分布,敌船在对岸跑,警船在另一头自动追踪。
大家可以运行一下,欢迎提意见。
头文件应该
#include<math.h>
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<malloc.h>
#include<string.h>
main() { float X,Y,x,y,U,u,ux,c,d,k,v1,v2,t=0.0001,m,mm; void *p1,*p2; int gd=DETECT,gm; initgraph(&gd,&gm,""); m=sqrt(3); mm=20+6*m;
setcolor(4);/*draw the ship*/ arc(20,228,210,330,12); arc(20,216,240,300,12*m); line(20,216+12*m,20,216); line(20,216,mm,222); line(mm,222,20,228); setfillstyle(1,4); floodfill(20,239,4); floodfill(21,222,4); p1=malloc(imagesize(20-6*m,216,mm,240)); getimage(20-6*m,216,mm,240,p1);
setcolor(14);/*draw the except ship*/ arc(80,68,210,330,12); arc(80,56,240,300,12*m); line(80,56+12*m,80,56); line(80,56,60+mm,62); line(60+mm,62,80,68); setfillstyle(1,14); floodfill(80,79,14); floodfill(81,62,14); p2=malloc(imagesize(80-6*m,56,80+6*m,80)); getimage(80-6*m,56,80+6*m,80,p2);
y=d=160;c=50;u=120;v2=20; do { cleardevice(); clrscr(); if(v2+20>u) break; x=60; X=Y=0; setfillstyle(1,10); bar(0,55,640,242);
outtextxy(240,45,"Ship Catching Mode"); outtextxy(240,260,"red is the catcher"); outtextxy(240,280,"whrite is water"); outtextxy(240,300,"blue is excapter"); coordc(20,240,600,0,600,3); coordc(20,240,180,90,180,3); do{x=x+v2*t; k=atan((y-Y)/(x-X)); v1=(-4*c*Y*Y)/(d*d)+(4*c*Y)/d; ux=(-v1*tan(k)*tan(k)+sqrt(u*u+tan(k)*tan(k)*u*u-tan(k)*tan(k)*v1*v1))/(1+tan(k)*tan(k)); U=sqrt(v1*v1+u*u+2*v1*ux); X=X+U*cos(k)*t; Y=Y+U*sin(k)*t;
putimage(20+X-6*m,216-Y,p1,1); putimage(20+x-6*m,56,p2,1); setcolor(10); putimage(20+X-6*m,216-Y,p1,1); putimage(20+x-6*m,56,p2,1);
putpixel(20+x,240-y,1); putpixel(20+X,240-Y,5); putpixel(20+v1,240-Y,15); if(fabs(Y-y)<=0.01&&fabs(X-x)<=0.01) break;}while(1);
v2+=20; printf("Press any key to continue\nPress Esc to end ");}while(getch()!=27);
closegraph(); }