//Sees if opponent made a greedy move int greedy_check(int *move, double range) { int last, i; double distance; last = MAX_NUMBER_OF_MOVES - NUM_MOVES_REMAINING; if(last < 2) return 0; for(i = 0; i < last; i += 2) { if(!next_to_set) { distance = distance_squared(move[i], move[i + 1], move[last - 2], move[last - 1]); distance = distance * distance; if(distance > range) return 1; } else { distance = distance_squared(move[i + 2], move[i + 2 + 1], move[last - 2], move[last - 1]); distance = distance * distance; if(distance > range) return 1; } } return 0; }
inline void compare_yz(DistanceField2D& partial_field, sdf_cell& cell, int64_t y, int64_t z, int64_t y_offset, int64_t z_offset) { sdf_cell other = get_yz(partial_field, y + y_offset, z + z_offset); other.d1 += y_offset; other.d2 += z_offset; if (distance_squared(other) < distance_squared(cell)) { cell = other; } }
Point2 Exact_adaptive_kernel::circumcenter( Point2 const& a, Point2 const& b, Point2 const& c ) { Point2 ba = b - a; Point2 ca = c - a; double bal = distance_squared( a, b ); double cal = distance_squared( a, c ); double denominator = 0.25 / signed_area( b, c, a ); Point2 d( ( ca( 1 ) * bal - ba( 1 ) * cal ) * denominator, ( ba( 0 ) * cal - ca( 0 ) * bal ) * denominator ); return a + d; }
inline void compare_xy(DistanceField2D& partial_field, sdf_cell& cell, int64_t x, int64_t y, int64_t x_offset, int64_t y_offset) { sdf_cell other = get_xy(partial_field, x + x_offset, y + y_offset); other.d1 += x_offset; other.d2 += y_offset; if (distance_squared(other) < distance_squared(cell)) { cell = other; } }
void TouchCalibrationView::touch_complete() { auto next_phase = static_cast<Phase>(toUType(phase) + 1); switch(phase) { case Phase::Calibrate0: case Phase::Verify0: digitizer_points[0] = average; break; case Phase::Calibrate1: case Phase::Verify1: digitizer_points[1] = average; break; case Phase::Calibrate2: case Phase::Verify2: digitizer_points[2] = average; break; default: break; } if( phase == Phase::Calibrate2 ) { const std::array<Point, 3> display_points { { image_calibrate_0.screen_rect().center(), image_calibrate_1.screen_rect().center(), image_calibrate_2.screen_rect().center(), } }; calibration = { digitizer_points, display_points }; } if( phase == Phase::Verify2 ) { const auto calibrated_0 = calibration.translate(digitizer_points[0]); const auto d_sq_0 = distance_squared(calibrated_0, image_verify_0); const auto calibrated_1 = calibration.translate(digitizer_points[1]); const auto d_sq_1 = distance_squared(calibrated_1, image_verify_1); const auto calibrated_2 = calibration.translate(digitizer_points[2]); const auto d_sq_2 = distance_squared(calibrated_2, image_verify_2); if( (d_sq_0 < verify_d_sq_max) && (d_sq_1 < verify_d_sq_max) && (d_sq_2 < verify_d_sq_max) ) { next_phase = Phase::Success; } else { next_phase = Phase::Failure; } } set_phase(next_phase); }
Point2 Exact_adaptive_kernel::offcenter( Point2 const& a, Point2 const& b, Point2 const& c, double offconstant ) { Point2 ba = b - a; Point2 ca = c - a; Point2 bc = b - c; double abdist = distance_squared( a, b ); double acdist = distance_squared( a, c ); double bcdist = distance_squared( b, c ); double denominator = 0.25 / signed_area( b, c, a ); BOOST_ASSERT( denominator > 0.0 ); double dx = ( ca(1) * abdist - ba(1) * acdist ) * denominator; double dy = ( ba(0) * acdist - ca(0) * abdist ) * denominator; double dxoff, dyoff; if ( ( abdist < acdist ) && ( abdist < bcdist ) ) { dxoff = 0.5 * ba(0) - offconstant * ba(1); dyoff = 0.5 * ba(1) + offconstant * ba(0); if ( dxoff * dxoff + dyoff * dyoff < dx * dx + dy * dy ) { dx = dxoff; dy = dyoff; } } else if ( acdist < bcdist ) { dxoff = 0.5 * ca(0) + offconstant * ca(1); dyoff = 0.5 * ca(1) - offconstant * ca(0); if ( dxoff * dxoff + dyoff * dyoff < dx * dx + dy * dy ) { dx = dxoff; dy = dyoff; } } else { dxoff = 0.5 * bc(0) - offconstant * bc(1); dyoff = 0.5 * bc(1) + offconstant * bc(0); if ( dxoff * dxoff + dyoff * dyoff < ( dx - ba(0) ) * ( dx - ba(0) ) + ( dy - ba(1) ) * ( dy - ba(1) ) ) { dx = ba(0) + dxoff; dy = ba(1) + dyoff; } } return Point2( a(0) + dx, a(1) + dy ); }
// subsquare [low,high) double SOMap::BMU_range( const SOM_element &s, int &xval, int &yval, subsquare_type &r) { double min_distance_squared = DBL_MAX; task &my_task = task::self(); int min_x = -1; int min_y = -1; for(int x = r.rows().begin(); x != r.rows().end(); ++x) { for( int y = r.cols().begin(); y != r.cols().end(); ++y) { double dist = distance_squared(s,my_map[x][y]); if(dist < min_distance_squared) { min_distance_squared = dist; min_x = x; min_y = y; } if(cancel_test && my_task.is_cancelled()) { xval = r.rows().begin(); yval = r.cols().begin(); return DBL_MAX; } } } xval = min_x; yval = min_y; return sqrt(min_distance_squared); }
void compute_torus_center (double probe_radius, struct sphere *sphere1,struct sphere *sphere2, double torus_center[3]) { int k; double radius1, radius2, asymmetry, distance12_squared; char message[MAXLINE]; struct cept *ex; distance12_squared = distance_squared (sphere1 -> center, sphere2 -> center); if (distance12_squared <= 0.0) { ex = new_cept (GEOMETRY_ERROR, DEGENERACY, FATAL_SEVERITY); add_function (ex, "compute_torus_center"); add_source (ex, "mstorus.c"); add_message (ex, "coincident atoms"); add_atom (ex, sphere1); add_atom (ex, sphere2); return; } radius1 = sphere1 -> radius; radius2 = sphere2 -> radius; asymmetry = (radius1 + probe_radius) * (radius1 + probe_radius) - (radius2 + probe_radius) * (radius2 + probe_radius); for (k = 0; k < 3; k++) torus_center[k] = 0.5 * (sphere1 -> center[k] + sphere2 -> center[k]) + 0.5 * ( asymmetry / distance12_squared) * (sphere2 -> center[k] - sphere1 -> center[k]); }
int main (int argc, char* argv[]) { // Read inputs Inputs* inputs = read_inputs(); // Create image Image* image = new_image( inputs->box.xMax - inputs->box.xMin, inputs->box.yMax - inputs->box.xMin); // Generate random colors Color colors[inputs->numSites]; for (int i = 0; i < inputs->numSites; i++) { colors[i] = random_color(); } // Bruteforce voronoi for (int i = inputs->box.xMin; i < inputs->box.xMax; i++) { for (int j = inputs->box.yMin; j < inputs->box.yMax; j++) { Point curr = { .x = i, .y = j }; int nearest = 0; for (int k = 0; k < inputs->numSites; k++) { double old_dist = distance_squared(&curr, &inputs->sites[nearest]); double new_dist = distance_squared(&curr, &inputs->sites[k]); if (new_dist < old_dist) { nearest = k; } } set_pixel(i, j, colors[nearest], image); } } // Do line sweep fortune(inputs, image); print_image(image); // Free memory free_image(image); free_inputs(inputs); return 0; }
void get_closest_player(enemy *e){ int i, d2, d2min; e->closest = NULL; d2min = MAX_X * MAX_X; for (i = 0; i < MAX_PLAYERS; i++) if (players[i] && (d2 = distance_squared(e, players[i]) ) < d2min){ d2min = d2; e->closest = players[i]; } }
void RunCommonVectorTests(std::string const& typeName) { RunCommonVectorOrQuaternionTests<T>(typeName); RunPerfTest<T, float>(typeName + " operator* (scalar lhs)", [](T* value, float param) { *value = param * *value; }); RunPerfTest<T, float>(typeName + " operator/ (scalar)", [](T* value, float param) { *value /= param; }); RunPerfTest<T, T>(typeName + " distance", [](T* value, T const& param) { value->x = distance(*value, param); }); RunPerfTest<T, T>(typeName + " distance_squared", [](T* value, T const& param) { value->x = distance_squared(*value, param); }); RunPerfTest<T, T>(typeName + " min", [](T* value, T const& param) { *value = min(*value, param); }); RunPerfTest<T, T>(typeName + " max", [](T* value, T const& param) { *value = max(*value, param); }); RunPerfTest<T, T>(typeName + " clamp", [](T* value, T const& param) { *value = clamp(*value, T::zero(), param); }); RunPerfTest<T, float4x4>(typeName + " transform (float4x4)", [](T* value, float4x4 const& param) { *value = transform(*value, param); }); RunPerfTest<T, quaternion>(typeName + " transform (quaternion)", [](T* value, quaternion const& param) { *value = transform(*value, param); }); }
typename enable_if< typename gtl_and_3< y_pt_ed2, typename is_point_concept< typename geometry_concept<PointType1>::type >::type, typename is_point_concept< typename geometry_concept<PointType2>::type >::type >::type, typename point_distance_type<PointType1>::type>::type euclidean_distance(const PointType1& point1, const PointType2& point2) { return (std::sqrt)( static_cast<double>(distance_squared(point1, point2))); }
void update_point(int *pos, int do_flag, int *move) { int i = pos[0], j = pos[1]; double d = 0.0; //Ignore pixels with stones set if (abs(board[i][j]) != INF) { d = distance_squared(move[0], move[1], i, j); if(d) { if (!do_flag) d = -d; board[i][j] += (1.0 / d); } } }
void calc_squares() { m_sighted = false; for (int count_1{0}; count_1 < 2; ++count_1) { for (int count_2{0}; count_2 < 2; ++count_2) { for (int count_3{0}; count_3 < 2; ++count_3) { m_abs_squares[count_1][count_2][count_3] = distance_squared(m_abs_posits[count_1][count_2][count_3], m_sight_square, m_sighted); // in_view(m_abs_posits[count_1][count_2][count_3], m_sighted); } } } }
double exact_pull(int *pos) { int i = 0; double d = 0.0, pull = 0.0; for (i = 0; i < MAX_NUMBER_OF_MOVES - NUM_MOVES_REMAINING; i++) { d = distance_squared(pos[0], pos[1], moves[i * 2], moves[i * 2 + 1]); if(d) { if (i % 2) pull += (1.0 / d); else pull -= (1.0 / d); } } return pull; }
int calc_radial_damage(const R_3DPoint &r1, float radius, int damage, const bounding_cube &bcube, int &dmg) { R_3DPoint cp; float d; int result = 0; bounding_cube bc = bcube; float rad_sq = radius * radius * hit_scaler; cp = bc.centerPoint(); d = distance_squared(cp,r1); if (d <= rad_sq) { /* woops! */ // dmg = (int) (((float)damage) * d / rad_sq); dmg = (int) (((float)damage) * (1.0 - (d / rad_sq))); result = 1; } return (result); }
float middle_square() { return distance_squared(m_central_posit, m_sight_square, m_sighted); }
inline double distance(const vector& from, const vector& to) noexcept { return std::sqrt(distance_squared(from, to)); }
double distance_squared(area const& a) const { return distance_squared(clamp_inside(a)); }
inline float distance_squared(const differential_geometry& a, const differential_geometry& b) { return distance_squared(a.point(), b.point()); }