graphics圆形碰撞

作者: admin
标签: 平面几何
更新: 8/9/2019, 11:55:58 AM
#include <graphics.h>
#include <cmath>
void control(int& x1,int& y1,int r1, int x2,int y2,int r2){
    if(kbhit()){
        char ch = getch();
        if(ch == 'a') x1-=5;
        if(ch == 'd') x1+=5;
        if(ch == 'w') y1-=5;
        if(ch == 's') y1+=5;
        int deltax = abs(x1-x2),deltay = abs(y1-y2);
        int len = sqrt(deltax*deltax+deltay*deltay);
        if(len<r1+r2){
            xyprintf(100,100,"碰撞了");
        }
    }
}
int main(){
    initgraph(640,480);
    int r1=20,r2=50;
    int x1=20,y1=20,x2=200,y2=200;
    while(is_run()){
        circle(x1,y1,r1);
        circle(x2,y2,r2);
        control(x1,y1,r1,x2,y2,r2);
        delay_fps(60);
        cleardevice();
    }
    closegraph(); 
    return 0;
} 
删除
修改
点击登陆评论