liblashgame

Pathfinder and path decision making library for 2D tile game
git clone git://holbrook.no/liblashgame.git
Log | Files | Refs

linear.c (2471B)


      1 // function draft for meeting point of two lines
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <time.h>
      6 #include "../lash_game_standard.h"
      7 	
      8 
      9 int main(int argc, char *argv[]) {
     10 
     11 	int i;
     12 	unsigned short test_count = 100;
     13 	clock_t test_clock_total_linear = 0.f;
     14 	clock_t test_clock_total_intersect = 0.f;
     15 	clock_t test_clock_begin = 0.f;	
     16 	
     17 	if (argc < 9) {
     18 		printf("Usage: %s src1_x src1_y target1_x target1_y src2_x src2_y target2_x target2_y\n", argv[0]);
     19 		return 1;
     20 	}
     21 	
     22 	int src1_x = atoi(argv[1]);
     23 	int src1_y = atoi(argv[2]);
     24 	int target1_x = atoi(argv[3]);
     25 	int target1_y = atoi(argv[4]);
     26 	int src2_x = atoi(argv[5]);
     27 	int src2_y = atoi(argv[6]);
     28 	int target2_x = atoi(argv[7]);
     29 	int target2_y = atoi(argv[8]);
     30 	
     31 	lash_game_coords_float_t src1_f;
     32 	lash_game_coords_float_t src2_f;
     33 	lash_game_coords_float_t target1_f;
     34 	lash_game_coords_float_t target2_f;
     35 	lash_game_line_t line1;
     36 	lash_game_line_t line2;
     37 	lash_game_coords_float_t result;
     38 	
     39 	int nointersect = 1;
     40 	
     41 	src1_f.x = (float)src1_x;
     42 	src1_f.y = (float)src1_y;
     43 	src2_f.x = (float)src2_x;
     44 	src2_f.y = (float)src2_y;
     45 	target1_f.x = (float)target1_x;
     46 	target1_f.y = (float)target1_y;
     47 	target2_f.x = (float)target2_x;
     48 	target2_f.y = (float)target2_y;
     49 	
     50 	
     51 	for (i=0; i<test_count; i++) {
     52 		test_clock_begin = clock();
     53 		lash_cartesianToLinear(src1_f, target1_f, &line1);
     54 		test_clock_total_linear += clock() - test_clock_begin; 
     55 	}
     56 	
     57 	lash_cartesianToLinear(src2_f, target2_f, &line2);
     58 
     59 	for (i=0; i<test_count; i++) {
     60 		test_clock_begin = clock();
     61 		nointersect = lash_linearIntersect(line1, line2, &result);
     62 		test_clock_total_intersect += clock() - test_clock_begin; 
     63 	}
     64 	
     65 	printf("Line 1: Src x%d,y%d - Target: x%d,y%d (Variable: %f, Const: %f)\n", src1_x, src1_y, target1_x, target1_y, line1.m, line1.c);
     66 	printf("Line 2: Src x%d,y%d - Target: x%d,y%d (Variable: %f, Const: %f)\n", src2_x, src2_y, target2_x, target2_y, line2.m, line2.c);
     67 	if (nointersect)
     68 		printf("No intersect point\n");
     69 	else
     70 		printf("Intersect point x%f,y%f\n", result.x, result.y);
     71 	printf("Total time linear: %lu ticks @ %li pr sec = %.7f secs\n", (long)(test_clock_total_linear / (float)test_count), CLOCKS_PER_SEC, (double)((test_clock_total_linear / (float)test_count) / CLOCKS_PER_SEC));
     72 	printf("Total time intersect: %lu ticks @ %li pr sec = %.7f secs\n\n", (long)(test_clock_total_intersect / (float)test_count), CLOCKS_PER_SEC, (double)((test_clock_total_intersect / (float)test_count) / CLOCKS_PER_SEC));
     73 	return 0;
     74 }