#22291 - sgeos - Thu Jun 17, 2004 4:07 am
Has anyone considered a bounding ellipse? How difficult would this be to implement?
-Brendan
-Brendan
Code: |
#define TRUE 1
#define FALSE 0 struct circle_critter { int x; int y; int r; }; struct ellipse_critter { int x; int y; int w; int h; }; int safe_distance_sq ( int x0, int y0, int x1, int y1 ) { int x_distance_sq; int y_distance_sq; x_distance_sq = x0 - x1; x_distance_sq *= x_distance_sq; y_distance_sq = y0 - y1; y_distance_sq *= y_distance_sq; return x_distance + y_distance + 1; } int radius_collision ( int source_r_sq, int target_r_sq, int safe_sq ) { if ((source_r_sq + target_r_sq) < safe_sq) return TRUE; /* else */ return FALSE; } int circle_collision ( circle_critter *s, // source circle_critter *t // target ) { return radius_collision ( s.r * s.r, t.r * t.r, safe_distance_sq(s.x, s.y, t.x, t.y) ); } int ellipse_r_sq(ellipse_critter *unit, angle_t theta) { int rw_sq; int rh_sq; rw_sq = unit.w * unit.w * cos(theta); rh_sq = unit.h * unit.h * sin(theta); return rw_sq + rh_sq; } int ellipse_collision ( ellipse_critter *s, // source ellipse_critter *t // source ) { angle_t theta; int rs_sq; // source's effective radius squared int rt_sq; // targets's effective radius squared theta = arctan(s.x - t.x, s.y - t.y); rs_sq = ellipse_r_sq(s, theta) rs_tq = ellipse_r_sq(t, theta) return radius_collision ( rs_sq, rt_sq, safe_distance_sq(s.x, s.y, t.x, t.y) ); } |
Code: |
int ellipse_r_sq(ellipse_critter *unit, angle_t theta)
{ int rw_sq; // effective w-radius for this angle (squared) int rh_sq; // effective h-radius for this angle (squared) rw_sq = unit.w * cos(theta); // get w component of effective radius rw_sq *= rw_sq; // square w component rh_sq = unit.h * sin(theta); // get h component of effective radius rh_sq *= rh_sq; // square h component return rw_sq + rh_sq; // combine for total effective radius } |