This is a preview
This playground version isn't public and is work in progress.
Area Calculation - Simple Shapes
In this example there are two separate shapes: a rectangle and a circle.
Press RUN MY CODE
to see how areas are calculated using Monte Carlo:
Simple Areas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //To enable optimizations in Codingame
#include <bits/stdc++.h> //All main STD libraries
#include <fstream>
using namespace std;
//Declaring area limits, shapes must be inside (0,0) and (Max_X,Max_Y) box.
const double Max_X = 200.00;
const double Max_Y = 200.00;
const double Reference_Area = Max_X * Max_Y;
/**
@brief Operation types between shapes.
*/
enum UnionType { UNION , INTERSECTION , SUBTRACTION};
/**
@brief Shape class, holds the shape that will be measured
*/
class Shape{
public:
double x,y; //Position
Shape(){x=0.0;y=0.0;}
Shape(double _x, double _y){ x=_x;y=_y;}
virtual string getName() {return "Shape";}
virtual string getJavascript(){return "";}
/**
@brief Checks if a point it's inside the shape or not
@param p_x p_y Coordinates of the point to be tested
@return boolean, true if the point it's inside the shape.
*/
virtual inline const bool isInsideShape (const double& p_x,const double& p_y)
{
return x == p_x && y == p_y;
}
/**
@brief The Area of the shape calculated in a mathematical way.
It's used to measure the deviation of Monte Carlo area vs the real area.
@return double, the Area.
*/
virtual double ReferenceArea() { return 0.00; }
};
/**
@brief A Rectangle, derived class from Shape base class.
*/
class Rectangle:public Shape{
public:
double w,h; //Size
Rectangle(int _x, int _y,int _w, int _h):Shape(_x,_y){ w=_w;h=_h; }
Press desired key combination and then press ENTER.
The accuracy of our calculations are usually above 99.95%, so it's a good approximation.
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content