From cb6eaf92c795869fa21f9b305c483f1c9db4e7ee Mon Sep 17 00:00:00 2001 From: Parric007 Date: Thu, 2 May 2024 09:02:26 +0200 Subject: [PATCH] Dynamic Y Bounds --- main.cpp | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 87bb1b8..f15b187 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,9 @@ #include #include +float lowerX = 0; +float upperX = 20; + double randomFloat() { return (double)(rand()) / (double)(RAND_MAX); @@ -68,6 +71,8 @@ public: }; + + double function(double x) { return pow(x,4)-4*pow(x,3)+18*pow(x,2)-12*x-69; } @@ -83,12 +88,32 @@ bool getIsInside(Point p) { double getRealIntegral(LinearBounds bounds) { return indefiniteIntegral(bounds.get_upper())- indefiniteIntegral(bounds.get_lower()); } +float getUpperLimit() { + float upperLimit = 0; + for(float i = lowerX; i< upperX; i+= 0.025) { + if(function((double)i) > upperLimit) { + upperLimit = (float) (function(i) * 1.025); + } + } + return upperLimit; +} +float getLowerLimit() { + float lowerLimit = 0; + for(float i = lowerX; i< upperX; i+= 0.025F) { + if(function(i) < lowerLimit) { + lowerLimit = (float) (function(i) * 1.025); + } + } + return lowerLimit; +} int main() { + + auto startTime = std::chrono::high_resolution_clock::now(); double pointsInside = 0; double samples = 10000000; - Bounds bounds = Bounds(LinearBounds(0, 20), LinearBounds(-100, 150000)); + Bounds bounds = Bounds(LinearBounds(lowerX, upperX), LinearBounds(getLowerLimit(), getUpperLimit())); Point toTestPoint; for (int i = 0; i< samples; i++) { toTestPoint = bounds.getRandomPoint();