inline bool clip_segment(Box const& b, segment_type& s, bool& sp1_clipped, bool& sp2_clipped) const { typedef typename select_coordinate_type<Box, Point>::type coordinate_type; coordinate_type t1 = 0; coordinate_type t2 = 1; coordinate_type const dx = get<1, 0>(s) - get<0, 0>(s); coordinate_type const dy = get<1, 1>(s) - get<0, 1>(s); coordinate_type const p1 = -dx; coordinate_type const p2 = dx; coordinate_type const p3 = -dy; coordinate_type const p4 = dy; coordinate_type const q1 = get<0, 0>(s) - get<min_corner, 0>(b); coordinate_type const q2 = get<max_corner, 0>(b) - get<0, 0>(s); coordinate_type const q3 = get<0, 1>(s) - get<min_corner, 1>(b); coordinate_type const q4 = get<max_corner, 1>(b) - get<0, 1>(s); if (check_edge(p1, q1, t1, t2) // left && check_edge(p2, q2, t1, t2) // right && check_edge(p3, q3, t1, t2) // bottom && check_edge(p4, q4, t1, t2)) // top { sp1_clipped = t1 > 0; sp2_clipped = t2 < 1; if (sp2_clipped) { set<1, 0>(s, get<0, 0>(s) + t2 * dx); set<1, 1>(s, get<0, 1>(s) + t2 * dy); } if(sp1_clipped) { set<0, 0>(s, get<0, 0>(s) + t1 * dx); set<0, 1>(s, get<0, 1>(s) + t1 * dy); } return true; } return false; }
bool polygon_ctrl_impl::on_mouse_button_down(double x, double y) { unsigned i; bool ret = false; m_node = -1; m_edge = -1; inverse_transform_xy(&x, &y); for (i = 0; i < m_num_points; i++) { if(sqrt( (x-xn(i)) * (x-xn(i)) + (y-yn(i)) * (y-yn(i)) ) < m_point_radius) { m_dx = x - xn(i); m_dy = y - yn(i); m_node = int(i); ret = true; break; } } if(!ret) { for (i = 0; i < m_num_points; i++) { if(check_edge(i, x, y)) { m_dx = x; m_dy = y; m_edge = int(i); ret = true; break; } } } if(!ret) { if(point_in_polygon(x, y)) { m_dx = x; m_dy = y; m_node = int(m_num_points); ret = true; } } return ret; }
INT32 Evaluation(UINT8 *board, UINT64 bk, UINT64 wh, UINT32 color, UINT32 stage) { float eval; /* 現在の色とステージでポインタを指定 */ hori_ver1 = hori_ver1_data[color][stage]; hori_ver2 = hori_ver2_data[color][stage]; hori_ver3 = hori_ver3_data[color][stage]; dia_ver1 = dia_ver1_data[color][stage]; dia_ver2 = dia_ver2_data[color][stage]; dia_ver3 = dia_ver3_data[color][stage]; dia_ver4 = dia_ver4_data[color][stage]; edge = edge_data[color][stage]; corner5_2 = corner5_2_data[color][stage]; corner3_3 = corner3_3_data[color][stage]; triangle = triangle_data[color][stage]; parity = parity_data[color][stage]; eval = check_h_ver1(board); eval += check_h_ver2(board); eval += check_h_ver3(board); eval += check_dia_ver1(board); eval += check_dia_ver2(board); eval += check_dia_ver3(board); eval += check_dia_ver4(board); eval += check_edge(board); eval += check_corner5_2(board); eval += check_corner3_3(board); eval += check_triangle(board); eval += check_parity(~(bk | wh), color); eval_sum = eval; eval *= EVAL_ONE_STONE; #ifdef LOSSGAME return -(INT32)eval; #else return (INT32)eval; #endif }
void timer(int num) { //不能在定时器里刷新屏幕,频率太高会显示不出来 if(game_state == GAME_RUNNING) { time_count++; //用于控制运动的速度 //背景移动 count_1++; if(count_1 == 4) { count_1 = 0; background.src_rect[0].y--; if(background.src_rect[0].y == 0) background.src_rect[0].y = 560; } //飞机移动,通过按键控制固定的速度 player.dst_rect.y += player.speed_y; player.dst_rect.x += player.speed_x; //检查飞机是否碰到边缘 check_edge(&player); //子弹运动 move_bullet(bullet); //敌人自动向下运动 int i, j; if(time_count%2 == 0) { for(i = 0; i < MAX_SMALL_ENEMY; i++) { if(small_enemy[i].life > 0 && small_enemy[i].pic_index == 0) { small_enemy[i].dst_rect.y += 2; } //敌人死亡之后,显示爆炸效果的图片 else if(small_enemy[i].life == 0 && small_enemy[i].pic_index != 0) { if(time_count%4 == 0) { //显示被射击之后的图像效果 small_enemy[i].pic_index++; //图像效果显示完之后,消失 if(small_enemy[i].pic_index == small_enemy[i].pic_num) { small_enemy[i].pic_index = 0; //分数增加 score_record += 100; } } } //到底端消失 if(small_enemy[i].dst_rect.y > SCREEN_HEIGHT) { small_enemy[i].life = 0; } } } //---------------中型飞机--------------- if(time_count%4 == 0) { for(i = 0; i < MAX_MID_ENEMY; i++) { if(mid_enemy[i].life > 0 && mid_enemy[i].pic_index == 0) { mid_enemy[i].dst_rect.y += 2; } else if(mid_enemy[i].life >= 0 && mid_enemy[i].pic_index != 0) { if(time_count%8 == 0) { //显示被射击之后的图像效果 if(mid_enemy[i].pic_index == 1 && mid_enemy[i].life > 0) { mid_enemy[i].pic_index = 0; mid_enemy[i].dst_rect.y += 2; } else if(mid_enemy[i].pic_index >= 1 && mid_enemy[i].life == 0) mid_enemy[i].pic_index++; //图像效果显示完之后,消失 if(mid_enemy[i].pic_index == mid_enemy[i].pic_num) { mid_enemy[i].pic_index = 0; score_record += 300; } } } //到底端消失 if(mid_enemy[i].dst_rect.y > SCREEN_HEIGHT) { mid_enemy[i].life = 0; } } } //--------------------大飞机----------------------- if(time_count%4 == 0) { for(i = 0; i < MAX_BIG_ENEMY; i++) { if(big_enemy[i].life > 0 && big_enemy[i].pic_index == 0) { big_enemy[i].dst_rect.y += 2; } else if(big_enemy[i].life >= 0 && big_enemy[i].pic_index != 0) { if(time_count%8 == 0) { //显示被射击之后的图像效果 if(big_enemy[i].pic_index == 1 && big_enemy[i].life > 0) { big_enemy[i].pic_index = 0; big_enemy[i].dst_rect.y += 2; } //死亡后显示被射击的效果 else if(big_enemy[i].pic_index >= 1 && big_enemy[i].life == 0) big_enemy[i].pic_index++; //图像效果显示完之后,消失 if(big_enemy[i].pic_index == big_enemy[i].pic_num) { big_enemy[i].pic_index = 0; score_record += 600; } } } //到底端消失 if(big_enemy[i].dst_rect.y > SCREEN_HEIGHT) { big_enemy[i].life = 0; } } } //将分数转化为字符串,并进行显示 show_score(); //玩家的飞机 if(time_count%2 == 0) { //死亡后显示爆炸效果 if(player.life == 0 && player.pic_index != 0) { if(time_count%4 == 0) { //显示被射击之后的图像效果 player.pic_index++; //图像效果显示完之后,消失 } } //到底端消失 if(player.dst_rect.y > (SCREEN_HEIGHT-player.src_rect[0].h)) { player.life = 0; player.dst_rect.y = 0; } } //飞机飞行动态 if(time_count == 10) { player.src_rect[0].x = 433; player.src_rect[0].y = 331; } else if(time_count > 50 && time_count < 100) { player.src_rect[0].x = 433; player.src_rect[0].y = 0; } else if(time_count == 100) time_count = 0; //自动射击 if(time_count %30 == 0) { // time_count = 0; if(auto_shot_flag) { shot_bullet(&player, bullet); } } } }
graph_t * load_graph (FILE * fdnodes, FILE * fdarcs){ unsigned int i; graph_t *graph; /* punterà al grafo da creare */ char *app, *p, *segna_posto, *line, *flag; /* vari puntatori ausiliari */ edge_t *aux, *helper; /* - dim conterrà l'esatta dimensione della stringa rappresentante il nodo sorgente, - i_sorg e i_dest conterranno gli indici dei nodi sorg/dest dell'arco letto */ int conta = ZERO, dim, i_sorg, i_dest; if( !fdnodes || !fdarcs ){ /* file non esistenti o errore nella precedente apertura?? */ errno = EINVAL; return NULL; } /* ---FASE 1--- : creazione nodi sorgente */ MALLOC_IF(app, sizeof(char), PDIM2) while ( fgets(app, PDIM2 - UNO, fdnodes) ) /* per la correttezza della struttura da allocare, mi serve sapere quanti nodi leggerò */ conta++; free(app); if(conta <= ZERO){ errno = EINVAL; return NULL; } MALLOC_IF(graph, sizeof(graph_t), UNO) if( !(MALLOC(graph->node, sizeof(node_t), conta)) ){ /* allocazione array dei 'conta' nodi */ free(graph); return NULL; } graph->size = conta; rewind(fdnodes); /* inserimento nodi */ for(i = ZERO; i < conta ;i++){ /* char * PDIM2 = (LLABEL+2) considerando anche il '\n' letto e il '\0' inserito dalla fgets */ if( !(MALLOC(app, sizeof(char), PDIM2) ) ){ free_graph(&graph); return NULL; } fgets( app, PDIM2, fdnodes); /* copia in app un intera riga del file (compreso il carattere '\n' : risulterà d'intralcio per la successivi confronti (es. chiamata di is_node); allora lo elimino... */ dim = strlen(app); app[dim - UNO] = TCHAR; /* e al suo posto inserisco '\0' per terminare la stringa */ if( !check_node(&app,UNO) || !( MALLOC( ((graph->node)+i)->label, sizeof(char), dim)) ){ free(app); free_graph(&graph); return NULL; } strncpy( ((graph->node)+i)->label, app, dim); /* copio la stringa fino all'ex '\n' compreso (ora '\0') */ free(app); ((graph->node)+i)->adj = NULL; } /* ---FASE 2--- : creazione archi */ MALLOC_IF(app, sizeof(char), PDIM1) while( (fgets( app, PDIM1 - UNO, fdarcs)) ){ /* start loop per la lettura delle stringhe-arco */ /* 1° PARTE: nodo sorgente */ segna_posto = strchr(app,':'); /* check di correttezza della 1° parte della stringa letta */ if( !segna_posto || !check_edge(app, segna_posto, TRUE) ){ STD_OUT1 } MALLOC_IF(p, sizeof(char), LLABEL+UNO) /* per contenere il nodo sorgente: stavolta basta +1, dato che non viene letto '\n' */ flag = app + (parzial_cpy(p, app, segna_posto)); /* copio la parte relativa al nodo sorgente e mi sistemo dopo i primi ':' */ if( (i_sorg = is_node(graph,p)) < ZERO){ free(p); STD_OUT1 } free(p); /* 2° PARTE: nodo destinazione */ /* mi sistemo sui secondi ':' e lancio un check sulla correttezza della 2° sottostringa (relativa al nodo destinazione) */ segna_posto = strchr(flag,':'); if( !segna_posto || !check_edge(flag, segna_posto, TRUE) ) { STD_OUT1 } MALLOC_IF(p, sizeof(char), LLABEL+UNO) flag = flag + parzial_cpy(p,flag,segna_posto); /* copio la seconda sottostringa e mi sistemo subito dopo i secondi ':' */ /* 3° parte: distanze in km + creazione elemento lista di adiacenza del nodo sorgente; non proseguo se il nodo destinazione non è valido, se esiste già un arco uguale a quello che vogliamo aggiungere oppure se la 3° sottostringa (relativa alla distanza in km) è malformata*/ line = remove_newline(flag); if( ((i_dest = is_node(graph,p)) < ZERO) || ((is_edge(graph,i_sorg,i_dest)) ) || !check_edge(line, segna_posto, FALSE) ){ free(p); free(line); STD_OUT1 }
int add_edge (graph_t *g, char *e){ int isaved_sorg = NEG, isaved_dest = NEG; /* variabili locali che mantengono gli indici del nodo sorgente e del nodo destinazione del grafo g */ char *segna_posto, *app; /* utilizzati rispettivamente per mantenere il riferimento al carattere ":" e alle tre divisioni (realizzate successivamente) della stringa "e" passata alla funzione */ edge_t *aux, *helper; if( !g || !e ){ errno = EINVAL; return NEG; } /* ---FASE 1--- : individuo l'indice del nodo sorgente */ /* delimito il primo pezzo di stringa, fino ai primi ":" (se esistono) e lancio un check sulla correttezza sulla 1° parte di "e" */ segna_posto = strchr(e,':'); if( !segna_posto || !check_edge(e, segna_posto, TRUE) ) { errno = EINVAL; return NEG; } if( !( MALLOC(app, sizeof(char), LLABEL)) ) return NEG; parzial_cpy(app,e,segna_posto); /* copio la prima parte di "e" (quella riguardante il nodo sorgente) in app */ if( ( isaved_sorg = is_node(g,app) ) < ZERO) { /* se trovo una corrispondenza al nodo sorgente richiesto, ne prendo l'indice */ free(app); errno = EINVAL; return NEG; } free(app); /* ---FASE 2---: individuo l'indice del nodo destinazione */ /* mi sistemo sul secondo pezzo di stringa e la delimito */ e = segna_posto + UNO; segna_posto = strchr(e,':'); /* check sulla corretteza della 2° parte di "e" */ if( !segna_posto || !check_edge(e, segna_posto, TRUE) ){ errno = EINVAL; return NEG; } if( !( MALLOC(app, sizeof(char), LLABEL)) ) return NEG; parzial_cpy(app,e,segna_posto); /* come per la FASE1, copio la sottostringa di "e" (riguardante il nodo destinazione) in "app" */ if( ( isaved_dest = is_node(g,app) ) < ZERO){ /* individuo l'indice del nodo destinazione e ne salvo l'indice (se esiste) */ free(app); errno = EINVAL; return NEG; } free(app); /* ---FASE 3--- : individuo la distanza in km */ /* mi sistemo sul terzo ed ultimo pezzo di sottostringa( quella riguardante i km) e lancio un check sulla correttezza della 3° parte della stringa : deve risultare composta da soli numeri [0-9] */ e = segna_posto + UNO; if( !check_edge(e, segna_posto, FALSE) ){ errno = EINVAL; return NEG; } /* ora bisogna controllare se l'arco individuato da "e" esiste già */ if( !(((g->node)+isaved_sorg)->adj) ){ if( !( MALLOC( ((g->node)+isaved_sorg)->adj, sizeof(edge_t), UNO)) ) return NEG; ((g->node)+isaved_sorg)->adj->label = isaved_dest; ((g->node)+isaved_sorg)->adj->next = NULL; sscanf(e,"%lf",&(((g->node)+isaved_sorg)->adj->km)); } /* legge dalla stringa "e" ,converte in double e inserisce nel campo km */ else /* adj con almeno un elemento */ { aux = ((g->node)+isaved_sorg)->adj; if( (is_edge(g,isaved_sorg,isaved_dest)) ){ /* arco già presente nella adj?? */ errno = EINVAL; return NEG; } else /* arco non presente nella adj: si può procedere all'inserzione */ { for(; aux -> next != NULL; aux = aux->next); /* mi sposto in coda: se la adj è vuota il ciclo non parte */ if( !(MALLOC(helper, sizeof(edge_t), UNO)) ) return NEG; aux->next = helper; helper->label = isaved_dest; helper->next = NULL; sscanf(e,"%lf",&(helper->km)); } /* legge dalla stringa "e",converte in double e inserisce nel campo km */ } return ZERO; }
bool VoronoiDiagramChecker::face_ok(HEFace f, bool debug) { HEEdge current_edge = g[f].edge; //std::cout << " face_ok( " << f << " )\n"; HEEdge start_edge= current_edge; double k = g[current_edge].k; if ( !((k==1) || (k==-1)) ) { std::cout << " face_ok " << f << " ERROR:\n"; std::cout << " illegal k-value for edge:"; std::cout << g[ g.source(current_edge)].index << " - "; std::cout << g[ g.target(current_edge)].index ; std::cout << " k= " << k << "\n"; return false; } if (g[f].site!=0) { // guard against null-faces that dont have Site if ( g[f].site->isPoint() ) { if ( !(k==1) ) { std::cout << " face_ok() pointsite, but k!=1 ! \n"; std::cout << " f = " << f << "\n"; return false; } } } int n=0; if (debug) std::cout << " checking face " << f << "\n"; do { if(debug) { std::cout << " edge: " << g[ g.source(current_edge)].index << " - "; std::cout << g[ g.target(current_edge)].index ; std::cout << " edge.face= " << g[ current_edge ].face; std::cout << " edge.k= " << g[ current_edge ].k; std::cout << "\n"; } if (g[current_edge].k != k ) { // all edges should have the same k-value std::cout << " face_ok() g[current_edge].k != k ! \n"; return false; } if ( !current_face_equals_next_face(current_edge) ) {// all edges should have the same face std::cout << " face_ok() !current_face_equals_next_face(current_edge) ! \n"; return false; } if ( !check_edge(current_edge) ) { std::cout << " VoronoiDiagramChecker::face_ok() f= " << f << " check_edge ERROR\n"; std::cout << " edge: " << g[ g.source(current_edge)].index << " t=" << g[ g.source(current_edge)].type; std::cout << " - "; std::cout << g[ g.target(current_edge)].index << " t=" << g[ g.target(current_edge) ].type << "\n"; HEEdge twin = g[current_edge].twin; std::cout << " twin: " << g[ g.source(twin)].index << " t=" << g[ g.source(twin)].type; std::cout << " - "; std::cout << g[ g.target(twin)].index << " t=" << g[ g.target(twin) ].type << "\n"; //std::cout << " twin: " << g[ g.source(twin)].index << " - " << g[ g.target(twin)].index << "\n"; std::cout << " edge-type= " << g[current_edge].type << "\n"; return false; } current_edge = g[current_edge].next; n++; assert( n < 10000 ); // reasonable max } while( current_edge != start_edge); //std::cout << " face ok, edges=" << n-1 << "\n"; return true; }