ARTICLE AD BOX
I came up with this idea while studying math, and I made it with Gemini. The basic idea was I, and Gemini came up with the idea of drawing the rate of change of c, and then it was related to game crash!
When I engineered the speed prompt, I got this code. I ran the test code, and it only improved 1.5 times. I heard it improved 10 times, but I'm not sure about game development, so I can't test it... Can someone re-create the test code?
Here is my algorithm
#ifndef ECCENTRIC_CIRCLE_HPP #define ECCENTRIC_CIRCLE_HPP #include <array> #include <cmath> class EccentricCircle { private: float radius; static constexpr int RESOLUTION = 512; static constexpr int MASK = RESOLUTION - 1; std::array<float, RESOLUTION> c_table; public: EccentricCircle(float r, float k) : radius(r) { const float STEP = (2.0f * 3.1415926535f) / RESOLUTION; for (int i = 0; i < RESOLUTION; ++i) { float theta = i * STEP; float cos_t = std::cos(theta); float sin_t = std::sin(theta); c_table[i] = (k * cos_t) + std::sqrt(1.0f - (k * k * sin_t * sin_t)); } } inline float get_dist_fast(int angle_index) const { return radius * c_table[angle_index & MASK]; } }; #endifhere is main.cpp
#include "EccentricCircle_Optomization.hpp" #include <iostream> int main() { float my_radius = 100.0f; float k_ideal = 0.707106f; EccentricCircle circle(my_radius, k_ideal); std::cout << "--- K-Library Initialization ---" << std::endl; std::cout << "Target K-Value: " << circle.get_K_value() << std::endl; std::cout << "Critical Point (k): " << k_ideal << std::endl; int current_angle = 45; float boundary_dist = circle.get_boundary_distance(current_angle); std::cout << "\n[Runtime Calculation]" << std::endl; std::cout << "Angle: " << current_angle << " deg" << std::endl; std::cout << "Pre-calculated Boundary Distance: " << boundary_dist << std::endl; float object_dist = 85.0f; if (object_dist < boundary_dist) { std::cout << "Result: COLLISION DETECTED!" << std::endl; } else { std::cout << "Result: CLEAR" << std::endl; } return 0; }And here is test.cpp
#include "EccentricCircle_Optimization.hpp" #include <iostream> #include <chrono> #include <cmath> #include <vector> void run_benchmark() { const int iterations = 10000000; float r = 100.0f; float k = 0.7071f; float x = 50.0f, y = 50.0f; EccentricCircle k_circle(r, k); // 1. Standard Method (sqrt) auto start1 = std::chrono::high_resolution_clock::now(); volatile float dummy1; for (int i = 0; i < iterations; ++i) { dummy1 = std::sqrt(x * x + y * y); } auto end1 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> diff1 = end1 - start1; // 2. K-Boundary Method (LUT) auto start2 = std::chrono::high_resolution_clock::now(); volatile float dummy2; for (int i = 0; i < iterations; ++i) { dummy2 = k_circle.get_dist_fast(i); } auto end2 = std::chrono::high_resolution_clock::now(); std::chrono::duration<double> diff2 = end2 - start2; std::cout << "Benchmark Results (" << iterations << " iterations):" << std::endl; std::cout << "Standard sqrt method: " << diff1.count() << "s" << std::endl; std::cout << "K-Boundary LUT method: " << diff2.count() << "s" << std::endl; std::cout << "Speedup: " << diff1.count() / diff2.count() << "x faster" << std::endl; } int main() { run_benchmark(); return 0; }If you want to know a simple mathematical interpretation of this, please check the introduction file on my
GitHub https://github.com/realgitman1/EccentricCircle_algorithm
