冰球項目日誌4-yjw

小組討論

咱們組編程主要分紅三個模塊,各自負責本身的編程與測試。ios

楊靜梧:肯定擊球算法編程。輸入:冰球位置,速度大小方向;輸出:撞擊時冰球中心位置。算法

曹迦勒:肯定擊球手速度,位置。輸入:撞擊時冰球中心位置,冰球速度;輸出:擊球手位置,速度方向。編程

李開旭:肯定擊球手軌跡。輸入:擊球手位置,速度大小方向;輸出:軌跡方程。dom

黃競超:整合三個模塊的程序塊。測試

hitpoint肯定算法代碼

/*輸入:冰球(xa,ya);速度大小va;方向a。*/
/*輸出:擊球點座標(x0,y0);*/
/*約束:桌面長寬分別爲x3,y3;冰球半徑r0;擊球手半徑r1;*/

#include <iostream> 
#include <string>
#include <math.h>
#include<stdlib.h>
#include<time.h>
using namespace std;

double xa, ya, va, a;  //輸入冰球信息

double xx1, yy1;   //冰球軌跡與上下壁交點座標
double xx2, yy2;   //冰球軌跡與左壁交點座標

double xhit, yhit;     //擊球點
double xhit1, yhit1;   //軌跡與擊球區域邊界第一個交點
double xhit_sf, yhit_sf;  //軌跡與算法選擇線交點
double xrandom, yrandom;  //隨機肯定點

double x3 = 120, y3 = 64;   //桌面大小
double x1 = 20, y11 = 16, x2 = 60, y2 = 48;    //擊球區左邊界,下邊界,右邊界,上邊界
double y4 = 24, y5 = 40;      //球門範圍


/*計算冰球軌跡與桌面壁的碰撞點*/
void root(double xa, double ya, double va, double a){
    double t1, t2; 
    if (sin(a) > 0) {
        t1 = (y3 - ya) / (va*sin(a));
        yy1 = y3;
    }
    else {
        t1 = ya / (va*sin(a));                    //肯定第一次碰撞的事件t1
        yy1 = 0;                              
    }
    xx1 = xa + t1*va*cos(a);                          //與上下壁碰撞點(xx1,yy1)
    t2 = xx1 / (va*cos(a));
    yy2 = yy1 + t2*va*sin(a);
    xx2 = 0;                                          //與左壁碰撞點(xx2,yy2)
}

/*計算與擊球區域邊界第一個交點*/
void point1() {
    double t;
    double x22, y22;
    t = (x2 - xa) / (va*cos(a));
    y22 = ya + t*va*sin(a);
    if (y22<y2&&y22>y11) {
        xhit1 = x2;
        yhit1 = y22;
    }
    else {
        t = y11 / (va*sin(a));
        x22 = xx1 + t*va*cos(a);
        xhit1 = x22;
        if (sin(a) > 0)
            yhit1 = y2;
        else
            yhit1 = y11;
    }
}

/*判斷是否會進球*/

bool hitin() {
    if (yy2<y5&&yy2>y4)
        return true;
    else
        return false;
}

/*計算與算法選擇線交點(xhit_sf,yhit_sf)*/
void point2() {
    double t;
    t = (x1 - xx1) / (va*cos(a));
    xhit_sf = x1;
    if (sin(a) > 0)
        yhit_sf = y3 - va*sin(a)*t;
    else
        yhit_sf = va*sin(a)*t;                          //(xhit_sf,yhit_sf)
}

/*產生隨機擊球點*/

void pointrandom(){ 
    double num;
    double t;
    srand(time(NULL));
    num = (rand() % 100)*0.01;
    xrandom = x1 + num*(x2 - x1);
    t = (xrandom - x1) / va*cos(a);
    yrandom = yy2 - va*sin(a)*t;
}

/*判斷擊球點*/
void hitpoint() {
    bool sf;
    point1();
    if (xhit1 > x1) {
        xhit = xhit1;
        yhit = yhit1;
    }
    else {
        sf = hitin();
        if (sf) {
            point2();
            xhit = xhit_sf;
            yhit = yhit_sf;
        }
        else {
            pointrandom();
            xhit = xrandom;
            yhit = yrandom;
        }
    }
}

void main() {
    cout << "請輸入冰球當前信息" << endl;
    cout << "x=";
    cin >> xa;
    cout << "y=";
    cin >> ya;
    cout << "va=";
    cin >> va;
    cout << "a=";
    cin >> a;
    root(xa, ya, va, a);
    hitpoint();
    //cout << xx1 << yy1 << xx2 << yy2;
    cout << "(xhit,yhit)=(" << xhit << ","<<yhit << ")";
}

代碼測試

代碼還沒有測試,待測試以後再來補充。spa

相關文章
相關標籤/搜索